-
Notifications
You must be signed in to change notification settings - Fork 14
My second assignment #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
dad17da
46fafc4
1a28096
35a9653
ea3eba6
66aa2f8
a4acfaa
28dc698
1399455
48e22c8
37ab060
d4af33d
9f6b722
7ba4240
1fd374d
158ca2b
6a643c6
0326903
fa40728
4ce811a
956dd05
4b9377d
197cb52
8dd9d03
28578ea
e7f996a
c703d88
a21d61c
d26930c
bec7dca
76efb9c
8aa2436
8d3ddaf
220928d
d9e3dfe
bed295e
1f12354
40afa7b
22df0ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,48 @@ | ||
| /* | ||
| * File: FindRange.java | ||
| * Name: | ||
| * Section Leader: | ||
| * -------------------- | ||
| * This file is the starter file for the FindRange problem. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class FindRange extends ConsoleProgram { | ||
| public void run() { | ||
| /* You fill this in */ | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * File: FindRange.java | ||
| * Name: | ||
| * Section Leader: | ||
| * -------------------- | ||
| * This file is the starter file for the FindRange problem. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class FindRange extends ConsoleProgram { | ||
| public void run() { | ||
| /* | ||
| * This program finds the range of | ||
| * any set of numbers until the Sentinel | ||
| * value is reached | ||
| */ | ||
| println("This program finds the minimum and"); | ||
| println("maximum values in a given set of numbers"); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| int min = 0; | ||
| int max = 0; | ||
| println("Enter a value"); | ||
| while (true){ | ||
|
|
||
| int val = readInt(); | ||
| if (val == SENTINEL){ | ||
| break; | ||
|
|
||
| } | ||
| else if(val < min){ | ||
| min = val; | ||
| } | ||
| else if(val > max){ | ||
| max = val; | ||
| } | ||
| } | ||
| println("The minimum value is " +min); | ||
| println("The maximum value is " +max); | ||
| } | ||
| private static final int SENTINEL = 20; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be 0. |
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,43 @@ | ||
| /* | ||
| * File: Hailstone.java | ||
| * Name: | ||
| * Section Leader: | ||
| * -------------------- | ||
| * This file is the starter file for the Hailstone problem. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class Hailstone extends ConsoleProgram { | ||
| public void run() { | ||
| /* You fill this in */ | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * File: Hailstone.java | ||
| * Name: | ||
| * Section Leader: | ||
| * -------------------- | ||
| * This file is the starter file for the Hailstone problem. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great solution. |
||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class Hailstone extends ConsoleProgram { | ||
| public void run() { | ||
| /* The program computes the Hailstone's sequence of any number | ||
| * entered by the user | ||
| */ | ||
|
|
||
| println("..."); | ||
| println("Enter any number to get the Hailstone sequence: "); | ||
| int x = readInt(); | ||
| int y = 0; | ||
|
|
||
| while (x != 1) { | ||
|
|
||
| if (x % 2 == 0) { | ||
|
|
||
| println( " " +x+ " is even, so i take half: " + x/2 ); | ||
| x /= 2; | ||
| y++; | ||
|
|
||
| } | ||
| else{ | ||
| println( " " +x+ " is odd, so i compute 3x + 1: " + (3 * x + 1) ); | ||
| x = 3 * x + 1; | ||
| y++; | ||
|
|
||
| } | ||
|
|
||
| } | ||
| println("..."); | ||
| println ("It took " + y + " steps to arrive at 1"); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
| /* | ||
| * File: ProgramHierarchy.java | ||
| * Name: | ||
| * Section Leader: | ||
| * --------------------------- | ||
| * This file is the starter file for the ProgramHierarchy problem. | ||
| */ | ||
|
|
||
| import acm.graphics.*; | ||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
| public class ProgramHierarchy extends GraphicsProgram { | ||
| public void run() { | ||
| /* You fill this in. */ | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * File: ProgramHierarchy.java | ||
| * Name: | ||
| * Section Leader: | ||
| * --------------------------- | ||
| * This file is the starter file for the ProgramHierarchy problem. | ||
| */ | ||
|
|
||
| import acm.graphics.*; | ||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose you didn't have enough time to code this part. :( |
||
|
|
||
| public class ProgramHierarchy extends GraphicsProgram { | ||
| public void run() { | ||
| /* You fill this in. */ | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,30 @@ | ||
| /* | ||
| * File: Pyramid.java | ||
| * Name: | ||
| * Section Leader: | ||
| * ------------------ | ||
| * This file is the starter file for the Pyramid problem. | ||
| * It includes definitions of the constants that match the | ||
| * sample run in the assignment, but you should make sure | ||
| * that changing these values causes the generated display | ||
| * to change accordingly. | ||
| */ | ||
|
|
||
| import acm.graphics.*; | ||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
| public class Pyramid extends GraphicsProgram { | ||
|
|
||
| /** Width of each brick in pixels */ | ||
| private static final int BRICK_WIDTH = 30; | ||
|
|
||
| /** Width of each brick in pixels */ | ||
| private static final int BRICK_HEIGHT = 12; | ||
|
|
||
| /** Number of bricks in the base of the pyramid */ | ||
| private static final int BRICKS_IN_BASE = 14; | ||
|
|
||
| public void run() { | ||
| /* You fill this in. */ | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent solution. |
||
| import acm.graphics.*; | ||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
| public class Pyramid extends GraphicsProgram { | ||
| private static final int BRICKS_IN_BASE = 32; | ||
| private static final int BRICK_WIDTH = 20; | ||
| private static final int BRICK_HEIGHT = 14; | ||
| public void run() { | ||
| /* You fill this in. */ | ||
| double x = (getWidth() - BRICKS_IN_BASE * BRICK_WIDTH) / 2; | ||
| double y = getHeight() - BRICK_HEIGHT; | ||
| for (int row = 0; row < BRICKS_IN_BASE; row++) { | ||
| createRow(x, y, (BRICKS_IN_BASE - row)); | ||
| y -= BRICK_HEIGHT; | ||
| x += BRICK_WIDTH / 2; | ||
| } | ||
| } | ||
| private void createRow(double x, double y, int bricks) { | ||
| for (int i = 0; i < bricks; i++) { | ||
| createBrick((x + i * BRICK_WIDTH), y); | ||
| } | ||
| } | ||
|
|
||
| private void createBrick(double x, double y) { | ||
| GRect rect = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT); | ||
| add(rect); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,28 @@ | ||
| /* | ||
| * File: PythagoreanTheorem.java | ||
| * Name: | ||
| * Section Leader: | ||
| * ----------------------------- | ||
| * This file is the starter file for the PythagoreanTheorem problem. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class PythagoreanTheorem extends ConsoleProgram { | ||
| public void run() { | ||
| /* You fill this in */ | ||
| } | ||
| } | ||
| /* | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent solution. |
||
| * File: PythagoreanTheorem.java | ||
| * Name: | ||
| * Section Leader: | ||
| * ----------------------------- | ||
| * This file is the starter file for the PythagoreanTheorem problem. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class PythagoreanTheorem extends ConsoleProgram { | ||
| public void run() { | ||
| /* You fill this in */ | ||
| println("*********************************************************"); | ||
| println("Enter the values of A and B to compute Pythagoras theorem"); | ||
| int a = readInt ("A = "); | ||
| println(""); | ||
| int b = readInt ("B = "); | ||
|
|
||
| int c = ((a*a) + (b*b)); | ||
|
|
||
| double x = Math.sqrt (c); | ||
| println(""); | ||
| println("The answer is " +x); | ||
| println("*********************************************************"); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,42 @@ | ||
| /* | ||
| * File: Target.java | ||
| * Name: | ||
| * Section Leader: | ||
| * ----------------- | ||
| * This file is the starter file for the Target problem. | ||
| */ | ||
|
|
||
| import acm.graphics.*; | ||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
| public class Target extends GraphicsProgram { | ||
| public void run() { | ||
| /* You fill this in. */ | ||
| } | ||
| } | ||
| /* | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great solution. Just a few technical issues. |
||
| * File: Target.java | ||
| * Name: | ||
| * Section Leader: | ||
| * ----------------- | ||
| * This file is the starter file for the Target problem. | ||
| */ | ||
|
|
||
| import acm.graphics.*; | ||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
| public class Target extends GraphicsProgram { | ||
| public void run() { | ||
| /* | ||
| * This program draws an archery target | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These values should have been declared as constants outside of the run method. So something like: |
||
| double inch2Pix = 72; | ||
|
|
||
| double maxiRadius = 1 * inch2Pix; | ||
| double mediRadius = 0.65 * inch2Pix; | ||
| double miniRadius = 0.30 * inch2Pix; | ||
|
|
||
| double x = getWidth()/2; | ||
| double y = getHeight()/2; | ||
|
|
||
| GOval maxiTarget = new GOval(x - maxiRadius, y - maxiRadius, 2 * maxiRadius, 2 * maxiRadius); | ||
| maxiTarget.setFilled(true); | ||
| maxiTarget.setColor(Color.RED); | ||
| add(maxiTarget); | ||
|
|
||
| GOval mediTarget = new GOval(x - mediRadius, y - mediRadius, 2 * mediRadius, 2 * mediRadius); | ||
| mediTarget.setFilled(true); | ||
| mediTarget.setColor(Color.WHITE); | ||
| add(mediTarget); | ||
|
|
||
| GOval miniTarget = new GOval(x - miniRadius, y - miniRadius, 2 * miniRadius, 2 * miniRadius); | ||
| miniTarget.setFilled(true); | ||
| miniTarget.setColor(Color.RED); | ||
| add(miniTarget); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The operation to draw the circles should have been done in a method since they all share code. So something like this: |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your program doesn't work correctly because you have an incorrect value of the SENTINEL. You set it to 20 instead of 0. Also it misses the following two edge cases:
and