This section is a log of work and assignments for iTunes U courses that I am taking. 

Friday
Dec072012

CS106A: Programming Methodology Assignment Problem Set 1


import stanford.karel.*;

public class defendingDemocracy extends Karel {

	public void run() {
		move();
		while (frontIsClear()){
			checkVote();
			nextVote();
		}
	}
	
	//assumes Karel in center of vote
	private void checkVote(){
		cleanVote();
	}
	
	private void cleanVote(){
		//Voter intends yes
		if (beepersPresent()){
			cleanAndPlace();
			goTopChad();
			cleanAndPlace();
			goBottomChad();
			cleanAndPlace();
			goCenterChad();
		} 
	
		//Voted intends no
		if (noBeepersPresent()) {
			cleanChad();
			goTopChad();
			cleanChad();
			goBottomChad();
			cleanChad();
			goCenterChad();
		}
		
	}
	
		private void goCenterChad() {
			turnAround();
			move();
			turnRight();
		}
	
		private void goTopChad() {
			turnLeft();
			move();
		}
		
		private void goBottomChad() {
			turnAround();
			move();
			move();
		}
		
		private void cleanChad() {
			while (beepersPresent()){
				pickBeeper();
			}		
		}
		
		private void cleanAndPlace() {
			//Check for hanging chad
				cleanChad();
				putBeeper();
		}
	
		//Assumes Karel in in center facing east
		private void nextVote(){
			move();
			if (frontIsClear()){
				move();
			}
		}
		
		private void turnAround() {
			for (int i=0; i < 2; i++) {
				turnLeft();
			}
		}
			
		private void turnRight () {
			for (int i=0; i < 3; i++) {
				turnLeft();
			}
		}
	}

	

 
Wednesday
Dec052012

CS106A: Programming Methodology Assignment 1.4


import stanford.karel.*;

public class MidpointFindingKarel extends SuperKarel {

	int height, width;
	
	// You fill in this part

	public void run() {
		count();
		centerBeeper();
	}
	
	public void count() {
		
		height = 0;
		width = 0;
		
		// count width
		width = width + 1;
		while(frontIsClear()){
			move();
			width = width + 1;
		}
		
//		// return home
//		turnAround();
//		for(int i = 1; i < width; i++){
//			move();
//		}
		
	}
	
	public void centerBeeper() {
		
		turnAround();
		
		for(int i = 0; i < (width/2); i++){
			move();
		}
		
		putBeeper();
		
		turnAround();
	}
	
}
Wednesday
Dec052012

CS106A: Programming Methodology Assignment 1.3


import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel {

	public void run() {
		if (frontIsClear()){
		horizontalPlacement();
		while (frontIsClear()){
			nextRow();
			horizontalPlacement();
		}
		} else {
			verticalPlacement();
		}
	}
	
	public void verticalPlacement() {
		turnLeft();
		while (frontIsClear()){
			if (frontIsClear()){
				putBeeper();
				move();
			}
			if (frontIsClear()){
				move();
			}
			}
	}
	
	public void horizontalPlacement() {
		while (frontIsClear()){
		if (frontIsClear()){
			putBeeper();
			move();
		}
		if (frontIsClear()){
			move();
		}
		}
		
		if (facingEast()){
			turnLeft();
		} else if (facingWest()){
			turnRight();
		}
	}

	public void nextRow() {
		move();
		turnRight();
		if (frontIsBlocked()){
			turnAround();
		}
	}
	
	
}
Wednesday
Dec052012

CS106A: Programming Methodology Assignment 1.2

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel {

	public void run() {
		turnLeft();
		repairColumn();
		while (frontIsClear()){
		nextColumn();
		repairColumn();
		}
	}

	private void repairColumn() {
		
	while (frontIsClear())	{
		if (noBeepersPresent()){
			putBeeper();
		}
		move ();	
	}
	if (noBeepersPresent()){
		putBeeper();
	}
	
	//Descend the column
	turnAround();
	while(frontIsClear()){
		move();
	}
	turnLeft();
	
	}
	
	private void nextColumn() {	
		//Move to next column
		move();
		move();
		move();
		move();
		turnLeft();
	}
	

	
}
Wednesday
Dec052012

CS106A: Programming Methodology Assignment 1.1


/*
 * File: CollectNewspaperKarel.java
 * --------------------------------
 * At present, the CollectNewspaperKarel subclass does nothing.
 * Your job in the assignment is to add the necessary code to
 * instruct Karel to walk to the door of its house, pick up the
 * newspaper (represented by a beeper, of course), and then return
 * to its initial position in the upper left corner of the house.
 */

import stanford.karel.*;

public class CollectNewspaperKarel extends Karel {
	
	// You fill in this part
	public void run() {
		turnRight();
		move();
		turnLeft();
		move();
		move();
		move();
		pickBeeper();
		turnAround();
		move();
		move();
		move();
	}
	
private void turnRight() {
	turnLeft ();
	turnLeft ();
	turnLeft ();
}

private void turnAround() {
	turnLeft ();
	turnLeft ();
}

}