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

		}
}