Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
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
56 changes: 40 additions & 16 deletions FindRange.java
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 */
}
}

/*

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:

If the user enters only one value before the sentinel, the program should report
that value as both the largest and smallest

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

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.

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

* 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 );
}
}


54 changes: 38 additions & 16 deletions Hailstone.java
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 {

Choose a reason for hiding this comment

The 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);


}
}
}
}

62 changes: 44 additions & 18 deletions ProgramHierarchy.java
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.*;

Choose a reason for hiding this comment

The 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:

  1. Someone reading your code will be confused how you came up with them
  2. If requirements change your program will be really hard to update. For example imagine for this exercise the requirements changed and we decide that the value for HEIGHT (50) and WIDTH (150) should be changed to HEIGHT = 60 and WIDTH = 180

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() {

Choose a reason for hiding this comment

The 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

public void run() {
    drawProgramBox();
    drawConsoleLine();
    drawConsoleBox();
    drawGraphicsLine();
    drawGraphicsBox();
    drawDialogLine();
    drawDialogBox();
}

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);
}
}

87 changes: 55 additions & 32 deletions Pyramid.java
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

Choose a reason for hiding this comment

The 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);
}


}


76 changes: 61 additions & 15 deletions PythagoreanTheorem.java
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 "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use ConsoleProgram printing method so this should be
println("Please enter the length of the...") instead of
System.out.println("Please enter the length of the...")

+ "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);


}
}








Loading