Pages

Sunday, May 29, 2011

Pyramid Solution



Pyramid problem through Java

Write a GraphicsProgram subclass that draws a pyramid consisting of bricks arranged in horizontal rows, so that the number of bricks in each row decreases by one as you move up the pyramid, as shown in the following sample run:



The pyramid should be centered at the bottom of the window and should use constants for the following parameters:
BRICK_WIDTH The width of each brick (30 pixels)
BRICK_HEIGHT The height of each brick (12 pixels)
BRICKS_IN_BASE The number of bricks in the base (12)
The numbers in parentheses show the values for this diagram, but you must be able to change those values in your program and still produce a reasonable picture.

Wednesday, May 25, 2011

My first Java Graphics Program!!

import acm.program.*;
import acm.graphics.*;
import java.awt.*;

public class jav extends GraphicsProgram
{
public void run()
{
GLabel lab=new GLabel("Hello World",50,50);//Creates a text label
add(lab);
GRect rect=new GRect(10,10,30,30);//Creates a rectangle, rect
rect.setFilled(true);
rect.setFillColor(Color.red);//Fills red color inside the rect
add(rect);
GOval ov=new GOval(10,10,30,30);//Creates an oval ov(inside rect)
ov.setFilled(true);
ov.setFillColor(Color.green);//green color inside ov
add(ov);
GLine li=new GLine(60,60,80,80);//Creates a new Line, li
li.setColor(Color.cyan);//cyan colored line
add(li);
}
}



Hello World Program!!!!!!

This is my first Java program. This program prints "Hello...World" in the console.

import acm.program.*;
public class jav extends ConsoleProgram
{
public void run()
{
println("Hello...World");
}
}

Java Programming Basics thorugh CS106a

In the following posts, we are gonna start on our journey through Java as instructed by Mr.Hasami in the CS106a program in Stanford University.

Tuesday, May 24, 2011

Midpoint solution

/ * 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.*/

import stanford.karel.*;

public class MidpointFindingKarel extends SuperKarel {

public void run()
{
int i;
i=0;
while(frontIsClear())
{
move();
i++;
}
i++;
turnAround();
for(int j=0;j

Midpoint

If Karel starts in the world



it should end with Karel standing on a beeper in the following position:


Note that the final configuration of the world should have only a single beeper at the midpoint of 1st Street. Along the way, Karel is allowed to place additional beepers wherever it wants to, but must pick them all up again before it finishes.

In solving this problem, you may count on the following facts about the world:

o Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers
in its bag.
o The initial state of the world includes no interior walls or beepers.
o The world need not be square, but you may assume that it is at least as tall as it is wide.
o Your program, moreover, can assume the following simplifications:
o If the width of the world is odd, Karel must put the beeper in the center square. If the
width is even, Karel may drop the beeper on either of the two center squares.
o It does not matter which direction Karel is facing at the end of the run.

There are many different algorithms you can use to solve this problem. The interesting part of this particular problem is to come up with a strategy that works.

Solution to the checkerboard problem

/ * When you finish writing it, the CheckerboardKarel class
*should draw a checkerboard using beepers.
*You should make sure that your program works for all of the
* sample worlds supplied in the starter folder.
*/

import stanford.karel.*;

public class CheckerboardKarel extends SuperKarel
{
int j=1;
public void run()
{
putBeeper();
turnLeft();
movement();
while(j==1)
{
if(facingNorth())
{
if(rightIsClear())
{
sidewayright();
movement();
}
if(leftIsBlocked())
j=0;
}
if(facingSouth())
{
if(leftIsClear())
{
sidewayleft();
movement();
}
if(rightIsBlocked())
j=0;
}
}
}
private void movement()
{
while(frontIsClear())
{
move();
if(frontIsClear())
{
move();
putBeeper();
}
}
}
private void sidewayright()
{
turnRight();
if(beepersPresent())
{
move();
turnRight();
if(frontIsClear())
{
move();
putBeeper();
}
}
else
{ move();
putBeeper();
turnRight();
}
}
private void sidewayleft()
{
turnLeft();
if(beepersPresent())
{
move();
turnLeft();
if(frontIsClear())
{
move();
putBeeper();
}
}
else
{
move();
putBeeper();
turnLeft();
}
}
}

Checkerboard Problem

This problem has a nice decomposition structure along with some interesting algorithmic issues. As you think about how you will solve the problem, you should make sure that your solution works with checkerboards that are different in size from the standard 8x8 checkerboard shown .

Odd-sized checkerboards are tricky, and you should make sure that your program generates the required pattern in a 5x3 world as well. Another special case you need to consider is that of a world which is only one column wide or one row high. The starter folder contains several sample worlds that test these special cases, and you should make sure that your program works for each of them.

Monday, May 23, 2011

How to do the earthquake problem

/* The StoneMasonKarel subclass ,when finished solves the "earthquake"
* problem.
*/

import stanford.karel.*;

public class StoneMasonKarel extends SuperKarel
{
public void run()
{
while(frontIsClear())
{
job();
}
job();
}
private void job()
{
ascend();
turnAround();
descend();
}
private void ascend()
{
turnLeft();
while(frontIsClear())
{
move();
}
}
private void descend()
{
while(frontIsClear())
{
if(beepersPresent())
move();
else
putBeeper();
}
if(noBeepersPresent())
putBeeper();
turnLeft();
for(int i=0;i<4;i++)
move();
}

}

Karel helps in Earthquake rehab

Karel has been hired to repair the damage done to the Quad in the 1989 earthquake. In particular, Karel is to repair a set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches, as follows:


Your program should work on the world shown above, but it should be general enough to handle any world that meets certain basic conditions as outlined at the end of this problem. There are several example worlds in the starter folder, and your program should work correctly with all of them.

When Karel is done, the missing beepers in the columns should be replaced, so that the final picture resulting from the world shown above would look like this:

Newaspaper Collection Karel Solution

/*The requirement 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 SuperKarel
{
public void run()
{
move();
turnRight();
move();
turnLeft();
move();
move();
pickBeeper(); //Picked up the Beeper
turnAround();
for(int i=0;i<3;i++) //On the way back
{
move();
}
turnRight();
move();
turnRight();
}
}

Newspaper collection by Karel

This is a simple story-problem in Karel’s world. Suppose that Karel has
settled into its house.

Karel starts off in the northwest corner of its house as shown. The problem you need to get Karel to solve is to collect the newspaper—represented (as all objects in Karel’s world are) by a Beeper—from outside the doorway and then to return to its initial position.

This exercise is extremely simple and exists just to get you started. You can assume that every part of the world looks just as it does in the diagram. The house is exactly this size, the door is always in the position shown, and the beeper is just outside the door. Thus, all you have to do is write the sequence of commands necessary to have Karel

1. Move to the newspaper,
2. Pick it up, and
3. Return to its starting point.

In your solution, include a private method for each of the steps shown in the outline.

Karel and his deeds

In following posts, I plan to show and discuss about some of the deeds of Karel in the "world" .

I will post some questions and will follow up with their solutions.

Friday, May 20, 2011

Karel The Robot

Karel, The Robot is a simple program done in Karel, a programming language similar to Java, named after Mr.Karel Capek, who is a Czech writer who actually introduced the word "Robot".

Karel is a very useful utility for the easy understanding of the basic principles of Java.

Using this very simple yet useful application, we can learn many basic commands and methods in Java like the for loop, while loop and the if condition which is very much the same as compared to that C++ and C.

Basically Karel has four commands or methods as they are called. They are turnLeft, move, putBeeper and pickBeeper. We control Karel and navigate through a "world". "World" can be defined according to our needs and may contain certain walls or beepers which Karel could collect and place it in other spots around the world.Karel may have 0 to infinity beeepers in his bags at a particular time.

The following is a simple example of Karel syntax (a ";" indicates end of instruction, can be omitted if current instruction is followed by an END).

BEGINNING-OF-PROGRAM

DEFINE turnright AS //The method to define the function turnRight()
BEGIN
turnleft;
turnleft;
turnleft
END

BEGINNING-OF-EXECUTION //Body of the program
ITERATE 3 TIMES
turnright;
move;
END-OF-EXECUTION

END-OF-PROGRAM