-
Notifications
You must be signed in to change notification settings - Fork 14
My second assignment #9
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
4c027cb
2d45bcc
b648df8
bf6ffda
aa25b3c
a08eeaa
900ce41
5cdb374
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,61 @@ | ||
| /* | ||
| * 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 { | ||
|
|
||
| /* You fill this in */ | ||
|
|
||
| //specifies the value for the sentinel | ||
| private static final int SENTINEL = 0; | ||
|
|
||
| //runs the program | ||
| public void run() { | ||
|
|
||
| println("This program finds the largest and smallest numbers."); | ||
|
|
||
| //initializes the minimum value | ||
| int min = 0; | ||
|
|
||
| //intializes the maximum value | ||
| int max = 0; | ||
|
|
||
| //iterates using while loop when the condition is true | ||
| while(true){ | ||
|
|
||
| //prompts the user to enter a value | ||
| int val = readInt("enter val: "); | ||
|
|
||
| //checks if the first value entered is 0 | ||
| if(val == SENTINEL) { | ||
| break; | ||
| } | ||
|
|
||
| //checks if the value is greater than the maximum value | ||
| //and then assigns it as the largest | ||
| if(val > max){ | ||
| max = val; | ||
| //checks if the value is less than the minimum value | ||
| //and then assign it as the smallest | ||
| } else if (val < min) { | ||
| min = val; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| //prints out the smallest value | ||
| println("smallest: " + min); | ||
|
|
||
| //prints out the largest value | ||
| println("largest: " + max); | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,30 @@ | ||
| /* | ||
| * 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 | ||
|
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!!! |
||
| * 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 */ | ||
| println("Enter a number:"); | ||
|
|
||
| int n = readInt("enter n: "); | ||
|
|
||
| while (n > 1) { | ||
| if ((n % 2) == 0){ | ||
| n = (n / 2); | ||
| println("n is even, so I take half: " + n); | ||
| } | ||
| else if ((n % 2)== 1) { | ||
| n = (3 * n) + 1; | ||
| println("n is odd, so i make 3n + 1: " + n); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,65 @@ | ||
| /* | ||
| * 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.*; | ||
|
|
||
| public class ProgramHierarchy extends GraphicsProgram { | ||
|
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. You should breakdown your program into smaller understandable methods. So the run method should have looked like this And then you would have gone ahead and implement each method. |
||
|
|
||
|
|
||
| private static final int HEIGHT = 50; | ||
| private static final int WIDTH = 150; | ||
|
|
||
| public void run() { | ||
| int x; | ||
| int y; | ||
|
|
||
| x = (getWidth() - WIDTH) / 2; | ||
| y = (getHeight() - HEIGHT) /2; | ||
|
|
||
| GRect Rect = new GRect(x, y, WIDTH, HEIGHT); | ||
| add(Rect); | ||
|
|
||
| GLabel label1 = new GLabel("Program", (x + 50), (y + 30)); | ||
| add(label1); | ||
|
|
||
|
|
||
| GLine Line1 = new GLine((x + 75), (y+50), (x+75), (y+100)); | ||
| add(Line1); | ||
|
|
||
| GRect Rect2 = new GRect(x, (y+100), WIDTH, HEIGHT); | ||
| add(Rect2); | ||
|
|
||
| GLabel label2 = new GLabel("ConsoleProgram", (x + 30), (y + 130)); | ||
| add(label2); | ||
|
|
||
| GLine Line2 = new GLine((x - 100), (y + 100), (x + 75), (y + 50)); | ||
| add(Line2); | ||
|
|
||
| GRect Rect3 = new GRect((x - 175), (y + 100), WIDTH, HEIGHT); | ||
| add(Rect3); | ||
|
|
||
| GLabel label3 = new GLabel("GraphicProgram", (x - 145), (y + 130)); | ||
| add(label3); | ||
|
|
||
| GLine Line3 = new GLine((x + 75), (y+50), (x+250), (y+100)); | ||
| add(Line3); | ||
|
|
||
| GRect Rect4 = new GRect((x+175), (y+100), WIDTH, HEIGHT); | ||
| add(Rect4); | ||
|
|
||
| GLabel label4 = new GLabel("DialogProgram", (x + 210), (y + 130)); | ||
|
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. Generally you should avoid the use of constant numbers in your program. You have values like (210, 130, 175, 145...etc) throughout your code and this is not a good idea because:
If this happens your program will fail to draw the right diagram. To make it work you will have to go back and start changing all the numbers throughout your program. It should be possible to come up with a solution that works without you doing further modification. The ability of a program to be able to adapt to changes like that is called scalability. So in this case your solution is not scalable enough. |
||
| add(label4); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,51 @@ | ||
| /* | ||
| * 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. Great work on this one. Now this is a scalable solution (unlike your solution in ProgramHierarchy.java). Now if we decide to change BRICKS_IN_BASE to 20 (instead of 14) and BRICK_WIDTH to 35 (instead of 30) . Your program still draws the right pyramid without you having to go and change things within the code. |
||
| * 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. */ | ||
| double x = (getWidth() - BRICKS_IN_BASE * BRICK_WIDTH) / 2; | ||
| double y = getHeight() - BRICK_HEIGHT; | ||
|
|
||
| for (int row = 0; row < BRICKS_IN_BASE; row++){ | ||
| drawRow(x, y, (BRICKS_IN_BASE - row)); | ||
| y -= BRICK_HEIGHT; | ||
| x += BRICK_WIDTH / 2; | ||
| } | ||
| } | ||
|
|
||
| private void drawRow(double x, double y, int bricks){ | ||
| for (int i = 0; i < bricks; i++){ | ||
| drawBrick((x + i * BRICK_WIDTH), y); | ||
| } | ||
| } | ||
| private void drawBrick(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 */ | ||
| } | ||
| } | ||
| /* | ||
| * File: PythagoreanTheorem.java | ||
| * Name: | ||
| * Section Leader: | ||
| * ----------------------------- | ||
| * This file is the starter file for the PythagoreanTheorem 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. Excellent work. |
||
| import acm.program.*; | ||
|
|
||
| public class PythagoreanTheorem extends ConsoleProgram { | ||
| public void run() { | ||
| /* You fill this in */ | ||
| println("Enter values to compute Pythagorean theorem."); | ||
| int a = readInt("enter a: "); | ||
|
|
||
| int b = readInt("enter b: "); | ||
|
|
||
| a = a * a; | ||
| b = b * b; | ||
|
|
||
| double c = Math.sqrt(a + b); | ||
| println("The answer is " + c + " . "); | ||
|
|
||
|
|
||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,45 @@ | ||
| /* | ||
| * 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. */ | ||
| } | ||
| } | ||
| /* | ||
| * File: Target.java | ||
|
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 radius and pixel values should have been constants similar to what the starter code for the pyramid question had brick width, brick height and number of bricks on the base as constants. So you should have had something like this: Also again this solution is not scalable (If the assignment requirements changed to have different radiuses your program will not work) and I don't think it uses the right measurements provided by the question (specifically the radiuses: 1.0, 0.65 and 0.3) |
||
| * 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. */ | ||
| double a = (getWidth() - 72) / 2; | ||
| double b = (getHeight() - 72) / 2; | ||
|
|
||
| GOval outtercircle = new GOval(a, b, 72, 72); | ||
|
|
||
| outtercircle.setFilled(true); | ||
| outtercircle.setColor(Color.RED); | ||
|
|
||
| add(outtercircle); | ||
|
|
||
| double i = (getWidth() - 46.8) / 2; | ||
| double j = (getHeight() - 46.8) / 2; | ||
|
|
||
| GOval midcircle = new GOval(i, j, 46.8, 46.8); | ||
|
|
||
| midcircle.setFilled(true); | ||
| midcircle.setColor(Color.white); | ||
| add(midcircle); | ||
|
|
||
| double x = (getWidth() - 21.6) / 2; | ||
| double y = (getHeight() - 21.6) / 2; | ||
|
|
||
| GOval innercircle = new GOval(x, y, 21.6, 21.6); | ||
| innercircle.setFilled(true); | ||
| innercircle.setColor(Color.RED); | ||
|
|
||
| add(innercircle); | ||
|
|
||
| } | ||
| } | ||
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.
Program doesn't work accurately. It fails the following two cases described in the assignment:
So for example if someone enters 5 followed by 0 the program should report:
smallest: 5
largest: 5
Your program instead reports
smallest: 0
largest: 5
Also
So if for example the user enters 0 the program should report:
No values has been entered.
Your program instead reports:
Smallest: 0
Largest: 0