From 3a2c14d76cf3d2631b3c8db44b2de94999550843 Mon Sep 17 00:00:00 2001 From: osayamen Date: Mon, 5 Jun 2017 03:14:49 +0100 Subject: [PATCH] completed --- .settings/org.eclipse.jdt.core.prefs | 12 ++++++++ FindRange.java | 18 ++++++++++- Hailstone.java | 22 +++++++++++-- ProgramHierarchy.java | 46 ++++++++++++++++++++++++++-- Pyramid.java | 20 ++++++++---- PythagoreanTheorem.java | 7 ++++- Target.java | 21 ++++++++++++- 7 files changed, 132 insertions(+), 14 deletions(-) create mode 100644 .settings/org.eclipse.jdt.core.prefs diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..ef8a789 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/FindRange.java b/FindRange.java index b662530..e494803 100755 --- a/FindRange.java +++ b/FindRange.java @@ -9,8 +9,24 @@ import acm.program.*; public class FindRange extends ConsoleProgram { + + private static final int SENTINEL = 0; public void run() { - /* You fill this in */ + println("This program finds the largest and smallest numbers"); + int a = readInt("?:"); + int min = a; + int max = a; + if (a!=SENTINEL){ + while (true){ + a = readInt("?:"); + if (a==SENTINEL) break; + min = (amax) ? a : max; + } + } + println("Smallest: "+min); + println("Largest: "+max); + } } diff --git a/Hailstone.java b/Hailstone.java index 2c7a16e..481f642 100755 --- a/Hailstone.java +++ b/Hailstone.java @@ -1,3 +1,4 @@ + /* * File: Hailstone.java * Name: @@ -10,7 +11,24 @@ public class Hailstone extends ConsoleProgram { public void run() { - /* You fill this in */ + println("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; + } +} diff --git a/ProgramHierarchy.java b/ProgramHierarchy.java index b666b6d..b043113 100755 --- a/ProgramHierarchy.java +++ b/ProgramHierarchy.java @@ -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.*; -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"); + 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; + + } +} diff --git a/Pyramid.java b/Pyramid.java index cf32252..062b85c 100755 --- a/Pyramid.java +++ b/Pyramid.java @@ -14,19 +14,27 @@ import acm.program.*; import java.awt.*; + public class Pyramid extends GraphicsProgram { /** 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)); + } + } } } diff --git a/PythagoreanTheorem.java b/PythagoreanTheorem.java index 5b4f30d..b7ae3c1 100755 --- a/PythagoreanTheorem.java +++ b/PythagoreanTheorem.java @@ -9,7 +9,12 @@ 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("a:"); + int b = readInt("b:"); + double c = Math.sqrt( (a*a) + (b*b)); + println("c =" + c); } } diff --git a/Target.java b/Target.java index 44c1f34..d7c3270 100755 --- a/Target.java +++ b/Target.java @@ -10,8 +10,27 @@ import acm.program.*; import java.awt.*; + public class Target extends GraphicsProgram { public void run() { - /* You fill this in. */ + + double radius_1 = 72; + 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; + } }