-
Notifications
You must be signed in to change notification settings - Fork 14
THIS MY SECOND ASSIGNMENT #10
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
218a835
b1e8ed5
2f593a8
c6e09ef
8bf7a66
a86ef12
e5bf01c
ab5a72b
244efeb
abf16dd
7277b39
eb7b84b
ea919e4
aeae166
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| eclipse.preferences.version=1 | ||
| org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
| org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate | ||
| org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 | ||
| org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
| org.eclipse.jdt.core.compiler.compliance=1.6 | ||
| org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
| org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
| org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
| org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
| org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
| org.eclipse.jdt.core.compiler.source=1.6 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,40 @@ | ||
| /* | ||
| * 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.*; | ||
| import java.util.*; | ||
|
|
||
| public class FindRange extends ConsoleProgram { | ||
| private static final int SENTINEL = 0; | ||
| public void run() { | ||
| println("the smalest and the largest values"); | ||
| int max=0; | ||
| int min=0; | ||
| int val; | ||
|
|
||
| while(true ){ | ||
| val =readInt (" ?"); | ||
|
|
||
| if (val==SENTINEL){ | ||
| break; | ||
| } | ||
| if (val>max){ | ||
| max=val; | ||
| } | ||
| else if(val<min){ | ||
| min=val; | ||
| } | ||
| } | ||
|
|
||
| println("This is the maximum number"+max ); | ||
|
|
||
| println("This is the minun number"+min ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,38 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
|
|
||
| public class Hailstone extends ConsoleProgram { | ||
|
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. Your program does not print out the number of steps it takes to reach 1 as per the assignment requirements. Also your print out statements are hard to read. |
||
| public void run() { | ||
| /* You fill this in */ | ||
|
|
||
| int n; | ||
| int i; | ||
| println("Enter a number number "); | ||
|
|
||
| n =readInt(" Number "); | ||
| while (n>1){ | ||
| if((n%2)==0){ | ||
| n=(n/2); | ||
| println("this is even I make halfn/2" +n); | ||
|
|
||
| } | ||
|
|
||
| else if ((n%2)==1){ | ||
|
|
||
| n=((3*n)+1); | ||
|
|
||
| println(" this is ond I make 3*n +1" +n); | ||
|
|
||
|
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,44 @@ | ||
| /* | ||
| * 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.*; | ||
|
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 (30, 130, 175, 75...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. |
||
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
| public class ProgramHierarchy extends GraphicsProgram { | ||
|
|
||
| private static final int HEIGHT = 50; | ||
| private static final int WIDTH = 150; | ||
| public void run() { | ||
|
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 |
||
| 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); | ||
| GRect Rect4 = new GRect((x+175), (y+100), WIDTH, HEIGHT); | ||
| add(Rect4); | ||
| GLabel label4 = new GLabel("DialogProgram", (x + 210), (y + 130)); | ||
| add(label4); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,55 @@ | ||
| /* | ||
| * 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. */ | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * File: Pyramid.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. Great solution. Now this is a scalable solution because now when I change BRICK_WIDTH, BRICK_HEIGHT and BRICK_IN_BASE your program still draws a pyramid at the center of the screen. |
||
| * 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. */ | ||
|
|
||
| /* 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,61 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| import acm.program.*; | ||
| import java.util.Scanner; | ||
|
|
||
|
|
||
| public class PythagoreanTheorem extends ConsoleProgram { | ||
|
|
||
| public static void main(String[] args){ | ||
|
|
||
| double legA, legB; | ||
|
|
||
|
|
||
| //Declare a String object to store the uer's name, | ||
|
|
||
|
|
||
| //Create a Scanner object for reading in the user's input | ||
| Scanner input= new Scanner(System.in); | ||
|
|
||
|
|
||
| //Prompt the user for the lengths of the legs, | ||
| //and store those two pieces of information in legA and legB. | ||
| System.out.println("Please enter the length of the " | ||
|
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 use ConsoleProgram printing method so this should be |
||
| + "first leg of the right triangle:"); | ||
| legA = input.nextDouble(); | ||
|
|
||
| System.out.println("Please enter the length of the " | ||
| + "second leg of the right triangle:"); | ||
| legB = input.nextDouble(); | ||
|
|
||
| //System.out.println(legA + " " + legB); | ||
|
|
||
| //Declare variables for the quantities to be calculated. | ||
| double hypotenuse; | ||
|
|
||
| //Calculate the hypotenuse using the Pythagorean Theorem: | ||
| //a^2 + b^2 = c^2 | ||
| //c = Math.sqrt(a^2 + b^2) | ||
| hypotenuse = Math.sqrt((legA*legA) + (legB*legB)); | ||
|
|
||
| //Report the results for hypotenuse. | ||
| System.out.println(); | ||
| System.out.println("the hypotenuse of your " + hypotenuse); | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
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