Posted on Leave a comment

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
[code lang=”java”]/*
* 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);

}

}

[/code]
[code lang=”java”]
/*
* 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);

}

}

[/code]

Posted on Leave a comment

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.

[code lang=”java”]/*
* 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();
}
}
}

}
[/code]

Posted on Leave a comment

The first assignment – CollectNewsPaperKarel

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

[code lang=”java”]
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();

}
}

[/code]