-
Notifications
You must be signed in to change notification settings - Fork 14
Assignment 2 (Programming Methodology)- Omigie Osayamen #1
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
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,3 +1,4 @@ | ||
|
|
||
| /* | ||
| * File: Hailstone.java | ||
| * Name: | ||
|
|
@@ -10,7 +11,24 @@ | |
|
|
||
|
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. |
||
| public class Hailstone extends ConsoleProgram { | ||
| public void run() { | ||
| /* You fill this in */ | ||
| println("This program finds the largest and smallest numbers"); | ||
|
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 assume you copied this from the Find Range problem because it is not true that this program finds the largest and smallest numbers. |
||
| int n = readInt("?:"); | ||
| int cnt = 0; | ||
| while (true) { | ||
| if (isEven(n)) { | ||
| println(n + " is even, so I take half: " + n / 2); | ||
| n = n / 2; | ||
| } else { | ||
| println(n + " is odd, so I make 3n+1: " + (3 * n + 1)); | ||
| n = (3 * n) + 1; | ||
| } | ||
| cnt++; | ||
| if (n==1) break; | ||
| } | ||
| println("the process took " +cnt+ " steps to reach 1"); | ||
| } | ||
| } | ||
|
|
||
| private boolean isEven(int n) { | ||
| return n % 2 == 0; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,53 @@ | |
| * This file is the starter file for the ProgramHierarchy problem. | ||
| */ | ||
|
|
||
| /* | ||
| * 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. Excellent solution. |
||
| public class ProgramHierarchy extends GraphicsProgram { | ||
| public class ProgramHierarchy extends GraphicsProgram { | ||
| public static final int BOX_WIDTH = 150; | ||
| public static final int BOX_HIGHT = 50; | ||
| public static final int SPACE = 20; | ||
|
|
||
| public void run() { | ||
| /* You fill this in. */ | ||
| int y; | ||
| int x; | ||
|
|
||
| int x_top = (getWidth() - BOX_WIDTH) / 2; | ||
| int y_top = (getHeight() - 3 * BOX_HIGHT) / 2 - BOX_HIGHT; | ||
| GRect box_top = createLabelledBox("Program", x_top, y_top); | ||
|
|
||
| y = (getHeight() - 3 * BOX_HIGHT) / 2 + (BOX_HIGHT); | ||
| x = (getWidth() - 3 * BOX_WIDTH - 2 * SPACE) / 2; | ||
| GRect box = createLabelledBox("GraphicsProgram", x, y); | ||
| add(new GLine(x_top + box_top.getWidth() / 2, y_top + box_top.getHeight(), x + box.getWidth() / 2, y)); | ||
|
|
||
| x = x + BOX_WIDTH + SPACE; | ||
| createLabelledBox("ConsoleProgram", x, y); | ||
| add(new GLine(x_top + box_top.getWidth() / 2, y_top + box_top.getHeight(), x + box.getWidth() / 2, y)); | ||
|
|
||
| x = x + BOX_WIDTH + SPACE; | ||
| createLabelledBox("DialogProgram", x, y); | ||
| add(new GLine(x_top + box_top.getWidth() / 2, y_top + box_top.getHeight(), x + box.getWidth() / 2, y)); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| private GRect createLabelledBox(String content, int x, int y) { | ||
| GLabel label = new GLabel(content); | ||
| // label.setFont("Times-72"); | ||
|
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. Commented out code should be deleted. |
||
| GRect box = new GRect(BOX_WIDTH, BOX_HIGHT); | ||
| add(box, x, y); | ||
| add(label, x + (box.getWidth() - label.getWidth()) / 2, y + (box.getHeight() + label.getAscent() / 2) / 2); | ||
| return box; | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,19 +14,27 @@ | |
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
|
|
||
| public class Pyramid 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. Excellent solution. |
||
|
|
||
| /** Width of each brick in pixels */ | ||
| private static final int BRICK_WIDTH = 30; | ||
| private static final int BRICK_WIDTH = 50; | ||
|
|
||
| /** Width of each brick in pixels */ | ||
| private static final int BRICK_HEIGHT = 12; | ||
| /** Height of each brick in pixels */ | ||
| private static final int BRICK_HEIGHT = 30; | ||
|
|
||
| /** Number of bricks in the base of the pyramid */ | ||
| private static final int BRICKS_IN_BASE = 14; | ||
|
|
||
| public void run() { | ||
| /* You fill this in. */ | ||
|
|
||
| public void run() { | ||
| double y = (getHeight() - BRICK_HEIGHT); | ||
| for (int i = 0; i < BRICKS_IN_BASE ; i++) { | ||
| double x = (getWidth() - BRICK_WIDTH*(BRICKS_IN_BASE-i)) / 2; | ||
| for (int j = 0; j < BRICKS_IN_BASE-i ; j++) { | ||
| pause(10); | ||
| add(new GRect(BRICK_WIDTH , BRICK_HEIGHT), x+(BRICK_WIDTH*j), y-(BRICK_HEIGHT*i)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,12 @@ | |
| import acm.program.*; | ||
|
|
||
| public class PythagoreanTheorem 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. Excellent solution. |
||
|
|
||
| public void run() { | ||
| /* You fill this in */ | ||
| println("Enter Values to compute Pythagorean theorem"); | ||
| int a = readInt("a:"); | ||
| int b = readInt("b:"); | ||
| double c = Math.sqrt( (a*a) + (b*b)); | ||
| println("c =" + c); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,27 @@ | |
| import acm.program.*; | ||
| import java.awt.*; | ||
|
|
||
|
|
||
| public class Target 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. Program works well...just a few coding techniques need to be changed. |
||
| public void run() { | ||
| /* You fill this in. */ | ||
|
|
||
| double radius_1 = 72; | ||
|
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 should have been declared as constants outside of the run method, also you should have named them outer_radius, middle_radius and inner_radius, that way someone knows which radius corresponds to which circl. So something like |
||
| double radius_2 = radius_1*0.65; | ||
| double radius_3 = radius_1*0.3; | ||
| this.addCenteredCircle(this.createCircle(radius_1, 1)); | ||
| this.addCenteredCircle(this.createCircle(radius_2, 2)); | ||
| this.addCenteredCircle(this.createCircle(radius_3, 3)); | ||
| } | ||
|
|
||
| private void addCenteredCircle(GOval circle) { | ||
| add(circle, (getWidth()-circle.getHeight())/2,(getHeight()-circle.getHeight())/2); | ||
| } | ||
|
|
||
| private GOval createCircle(double radius, int circle_number){ | ||
| GOval circle = new GOval(radius,radius); | ||
| circle.setFilled(true); | ||
| circle.setColor(circle_number%2==0? Color.WHITE : Color.RED ); | ||
| return circle; | ||
|
|
||
| } | ||
| } | ||
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.
Great solution. However you missed one edge case:
If the user enters the sentinel on the very first input line, then no values have been
entered, and your program should display a message to that effect.