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

	
}

Karel the Robot Assignment 1 Problem 2

It looks like I got the assignment solved. It looks a bit like spaghetti code but it works on the default world.

/*
 * File: StoneMasonKarel.java
 * --------------------------
 * The StoneMasonKarel subclass as it appears here does nothing.
 * When you finish writing it, it should solve the "repair the quad"
 * problem from Assignment 1.  In addition to editing the program,
 * you should be sure to edit this comment so that it no longer
 * indicates that the program does nothing.
 */

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

	public void run(){
		//iterate 13 times(checking 13 columns)
		for(int i=0;i<13;i++){
			check();
			fixColumn();
			keepmoving();
		}
                //pseudocode
		//go up the column
		//check if there is a beeper
		//if there is one
		//go back to the start and
		//fill in the beeper
		//go check the next column
		//repeat
	}

// move to the wall
	private void movetowall(){
		while(frontIsClear()){
			move();
		}
	}

//keep karel moving through the next column
	private void keepmoving(){
		if(frontIsBlocked()){
			if(facingNorth()){
				backtostart();
			}
		}
			
		if(facingNorth()){
			turnRight();
			move();
			//turnRight();
		}
		if(facingSouth()){
			turnLeft();
			move();
			turnLeft();
		}
		if(facingWest()){
			turnAround();
			move();
		}
		
	}

//skip a beeper
	private void skipbeeper(){
		if(beepersPresent()){
			move();
		}
	}

//fix the broken column
	private void fixColumn(){
		backtostart();
		while(frontIsClear()){
			skipbeeper();
			while(noBeepersPresent()){
				
				if(frontIsClear()){
					while(noBeepersPresent()){
						putBeeper();
						move();
					}
				} else {
					if(noBeepersPresent()){
						putBeeper();
					}
					turnAround();
					movetowall();
				}		
			}
		}		
	}

	//move to the next column
	private void nextcolumn(){
		turnLeft();
		move();
		turnLeft();

	}
	
	//back to starting position
	private void backtostart(){
		turnAround();
		movetowall();
		turnAround();
	}
	
	//check if this a column that needs fixing
	private void check(){
		if(facingEast()){
			turnLeft();
		}
		while(noBeepersPresent()){
			if(frontIsClear()){
				move();
			} else {
				turnAround();
				movetowall();
				nextcolumn();
				backtostart();
			}
		}
	}

}

The first assignment – CollectNewsPaperKarel

Posting my solution to the problem number 1 of assignment 1 of CS106a. Hoping for some feedback to this.

import stanford.karel.*;

public class CollectNewspaperKarel extends SuperKarel {

		public void run(){
			MoveToNewsPaper();
			pickBeeper();
			MoveBackToStart();
		}


		private void MoveToNewsPaper(){
			while(frontIsClear()){
				move();
			}
			turnRight();
			move();
			turnLeft();
			move();
			
		}

		private void MoveBackToStart(){
			turnAround();
			move();
			turnRight();
			move();
			turnLeft();
			while(frontIsClear()){
				move();
			}
			turnAround();

		}
}