FindRange.java solution – Assignment 2

/*
 * File: FindRange.java
 * Name: 
 * Section Leader: 
 * --------------------
 * This file is the starter file for the FindRange problem.
 */

import acm.program.*;

public class FindRange extends ConsoleProgram {
	private static final int SENTINEL = 0;

	public void run() {
		//the smallest number
		int smallest = 0;
		
		//the largest 
		int largest = 0;
		//intro
		println("The program finds the largest and smallest numbers.");
		
		//get the first number
		int number = readInt("? ");
		//if there is only one number - make it the smallest and th
		// the largest
		smallest=number;
		largest=smallest;

		//loop and a half for the second number
		//keep looping until the sentinel is given
		while(number!=SENTINEL){
			//get the second digit
			int number1 = readInt("? ");
			
			// breaking out of the loop
			if(number1==SENTINEL){
				break;
			}	
			
			//find the smallest number
			if(number1<smallest ){
				smallest=number1;
			}
			
			//find the largest number
			if(number1>largest){
				largest=number1;
			}
		}
		
		//if the first number is a 0
		// else print the smallest and largest
		if(number == SENTINEL){
			println("Please enter a number not 0.");
		} else {
			println("smallest: "+smallest);
			println("largest: "+largest);
		}

	}
}

Hailstone solution -Assignment 2

/*
 * 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() {
		//index for the number of loops to do
		int index =0;
		
		//query for the number
		int number = readInt("Enter a number: ");
		
		//loop until the number ==1
		while(number!=1){
			//increment the loop
			index++;
			
			//if number is even take half
			if(number%2==0){
				println(number+" is even so I take half: "+(number/=2));
			} else {
				//else if odd make it 3n+1
				println(number+" is odd, so I make 3n +1: "+(number = (number*3)+1));
			}
		}
		//print the number of loops
		println("The process took "+index+" to reach 1.");
	}
}

Solution to PythagoreanTheorem – Assignment 2

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

Solution to ProgramHeirarchy – Assignment 2

/*
 * 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 {	
	// width of the box
	private static final int BOX_WIDTH = 175;
	
	//height of the box
	private static final int BOX_HEIGHT = 45;
	
	public void run() {
               
		//middle point of the window
		double middle = getWidth()/2;
		
		//height window middle
		double height_center = getHeight()/2;
		
		//middle of the box itself
		double box_mid = BOX_WIDTH/2;
		
		// half the box height
		double box_half = BOX_HEIGHT/2;
		
		// base of top square = height_center -40
		double base_top= height_center -40 ;
		
		//roof of bottom boxes
		double roof_bottom = height_center + 40 ;
		
		/*
		 * The boxes here
		 */
		
		// top rect
		GRect top = new GRect(middle-box_mid,base_top -BOX_HEIGHT,BOX_WIDTH,BOX_HEIGHT);
		add(top);
		
		//left rect 
		GRect left = new GRect(middle/2 -box_mid,roof_bottom,BOX_WIDTH,BOX_HEIGHT);
		add(left);
		
		// mid rect
		GRect mid = new GRect(middle-box_mid,roof_bottom,BOX_WIDTH,BOX_HEIGHT);
		add(mid);

		//right rect
		GRect right = new GRect(middle + middle/2 - box_mid,roof_bottom,BOX_WIDTH,BOX_HEIGHT);
		add(right);
		
		/*
		 * the lines connecting here
		 */
		
		//leftLine
		GLine leftLine = new GLine(middle/2,roof_bottom,middle,base_top);
		add(leftLine);
		
		//midline
		GLine midLine = new GLine(middle,base_top,middle,roof_bottom);
		add(midLine);
		
		//rightLine
		GLine rightLine = new GLine(middle,base_top,middle+middle/2,roof_bottom);
		add(rightLine);
		
		/*
		 * the labels
		 */
		
		// TopLabel
		GLabel topLabel = new GLabel("Program");
		topLabel.move(middle-topLabel.getWidth()/2, base_top+topLabel.getAscent()/2 -BOX_HEIGHT/2);
		add(topLabel);
		
		//LeftLabel
		GLabel leftLabel = new GLabel("GraphicsProgram");
		leftLabel.move(middle/2-leftLabel.getWidth()/2, roof_bottom+leftLabel.getAscent()/2+BOX_HEIGHT/2);
		add(leftLabel);

		//Middle Label
		GLabel midLabel = new GLabel("ConsoleProgram");
		midLabel.move(middle-midLabel.getWidth()/2, roof_bottom+midLabel.getAscent()/2 +BOX_HEIGHT/2);
		add(midLabel);

		//rightLabel
		GLabel rightLabel = new GLabel("DialogProgram");
		rightLabel.move(middle+middle/2-rightLabel.getWidth()/2, roof_bottom+rightLabel.getAscent()/2 +BOX_HEIGHT/2);
		add(rightLabel);
			
	}
}


My Pyramid solution for Assignment 2

/*
 * 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;
	private static final int BRICKS_IN_BASE = 20;


	public void run() {
		// get the middle of the window
		double middle = getWidth() /2 ;
		
		// the starting base_width
		double base_width = BRICKS_IN_BASE * BRICK_WIDTH;
		
		// where we put the first block of the row
		double start_base = middle - (base_width/2);
		
		//getting to the bottom layer
		double bottom = getHeight();
		
		// cannot change the constant, so we'll assign another variable for
		// the columns, add 1 to get to the top
		int blk_up = BRICKS_IN_BASE+1;
		
		// the non static brick_base variable
		int blck_side = BRICKS_IN_BASE;
		
		
		// the first loop is for the column 
		for(int i=1;i<blk_up;i++){
			
			// second loop is for the row
			for(int j=0;j<blck_side;j++){
				
				// y coordinate position for the bricks
				double y = bottom -(i * BRICK_HEIGHT);
				
				// x coordinate for the bricks
				double x = start_base + (j * BRICK_WIDTH);
				
				//draws the bricks
				GRect myrect = new GRect(x,y,BRICK_WIDTH,BRICK_HEIGHT);
				add(myrect);
			}
			// subtract 1 brick from the row
			blck_side -=1;
			
			// get a new base width
			base_width = blck_side *BRICK_WIDTH;
			
			//get the new position for the start of the new row
			start_base = middle - (base_width/2);
		}
		// subtract 1 from the column
                // I don't think this is necessary
		//blk_up-=1;	 
	}
}


Target solution updated

Added “setColor” to set the color of the borders. Thanks to my wife ,she notice that the borders need to be changed.

Screen Shot 2013-11-17 at 2.47.54 AM

target
target

Updated code

/*
 * 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() {
		
		int center_x = getWidth() /2;
		int center_y = getHeight() /2;
		int out_radius = 72;
		int mid_radius = (int) (out_radius * 0.65);
		int inn_radius = (int) (out_radius * 0.3);
		
		// outer circle
		GOval out = new GOval(center_x-(out_radius/2),center_y-(out_radius/2),out_radius,out_radius);
		out.setFilled(true);
		out.setColor(Color.RED);
		out.setFillColor(Color.RED);
		add(out);
		
		//middle circle
		GOval mid = new GOval(center_x-(mid_radius/2),center_y-(mid_radius/2),mid_radius,mid_radius);
		mid.setFilled(true);
		mid.setColor(Color.WHITE);
		mid.setFillColor(Color.WHITE);
		add(mid);
		
		//inner circle
		GOval inn = new GOval(center_x-(inn_radius/2),center_y-(inn_radius/2),inn_radius,inn_radius);
		inn.setFilled(true);
		inn.setColor(Color.RED);
		inn.setFillColor(Color.RED);
		add(inn);
		
		
		}

	
}

/*
 * 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() {
		
		int center_x = getWidth() /2;
		int center_y = getHeight() /2;
		int out_radius = 72;
		int mid_radius = (int) (out_radius * 0.65);
		int inn_radius = (int) (out_radius * 0.3);
		
		// outer circle
		GOval out = new GOval(center_x-(out_radius/2),center_y-(out_radius/2),out_radius,out_radius);
		out.setFilled(true);
		out.setFillColor(Color.RED);
		add(out);
		
		//middle circle
		// 
		GOval mid = new GOval(center_x-(mid_radius/2),center_y-(mid_radius/2),mid_radius,mid_radius);
		mid.setFilled(true);
		mid.setFillColor(Color.WHITE);
		add(mid);
		
		//inner circle
		GOval inn = new GOval(center_x-(inn_radius/2),center_y-(inn_radius/2),inn_radius,inn_radius);
		inn.setFilled(true);
		inn.setFillColor(Color.RED);
		add(inn);
		
		
		}

	
}

MidPointFindingKarel – My Solution

I had a pretty challenging two weeks of solving this problem. I finally found a solution. I am so relieved.


/*
 * File: MidpointFindingKarel.java
 * -------------------------------
 * When you finish writing it, the MidpointFindingKarel class should
 * leave a beeper on the corner closest to the center of 1st Street
 * (or either of the two central corners if 1st Street has an even
 * number of corners).  Karel can put down additional beepers as it
 * looks for the midpoint, but must pick them up again before it
 * stops.  The world may be of any size, but you are allowed to
 * assume that it is at least as tall as it is wide.
 */


import stanford.karel.*;

public class MidpointFindingKarel extends SuperKarel {
	public void run(){

		/*
		 * Pseudocode
		 * 1.laybeepers
		 * 2.pick them up one end to the other
		 *
		 * Decomposition
		 *  * solves one problem
		 *  * 1-15 lines
		 *  * good names
		 *  * comments /preconditions
		 *  *          /postconditions
		 *
		 */
		LayBeepers();
		BackToStart();

		/* FindTheMiddle
		 * here is the method that
		 * does the work
		 */
		FindTheMiddle();
	}

	/*
	 * LayBeeper
	 * put beepers from one end to the other
	 * pre: unlimited beepers, front is clear
	 * post: wall in front, beeper where iam standing
	 */
	private void LayBeepers(){
		while(frontIsClear()){
			putBeeper();
			move();
		}
		putBeeper();
	}

	/* BackToStart
	 * moves Karel to the starting position
	 * pre: front is blocked
	 * post:front is clear
	 */
	private void BackToStart(){
		turnAround();
		MoveToWall();
		turnAround();
	}

	/* MoveToWall
	 * keep moving until karel hits a wall
	 * pre: front clear
	 * post : front blocked
	 */
	private void MoveToWall(){
		while(frontIsClear()){
			move();
		}
	}

	/* FindTheMiddle
	 * Finds the midpoint of a line
	 * Pre: beepers present in front
	 * post: no more beepers in front
	 *
	 */
	private void FindTheMiddle(){
		if(beepersPresent()){
			pickBeeper();
			}
		while(noBeepersPresent()){
			GotoTheOtherEnd();
			if(frontIsClear()){
				checkabeeper();
			}
			pickBeeper();
		}
	}

	/*
	 * Gootheotherend
	 * traverse the line of beepers
	 */
	private void GotoTheOtherEnd(){
		FirstBeeper();
		EndingBeeper();
	}

	/* FirstBeeper
	 * go to the first beeper
	 */
	private void FirstBeeper(){
		if(noBeepersPresent()){
			move();
		}
	}

	/* endingbeeper
	 * check the lastbeeper
	 *
	 */
	private void EndingBeeper(){
		while(frontIsClear()){
			move();
		}
		turnAround();

		while(noBeepersPresent()){
			move();
		}
	}

	/* checkabeeper
	 * checks if there is still a beeper for the last
	 * time
	 */
	private void checkabeeper(){
		if(beepersPresent()){
			move();
			if(beepersPresent()){
				MoveBack();
			} else {
				MoveBack();
				putBeeper();
			}
		}
	}

        /* Moveback
         *
         */
	private void MoveBack(){
		turnAround();
		move();
		turnAround();
	}
}