Java programming
This blog is created with the help of the CS106a program in Stanford University,USA.This program has helped me in the study of Java in a large extent. So, thank you Stanford and Mr.Mehran Sahami.
Sunday, May 29, 2011
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.
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);
}
}
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");
}
}
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
* 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.
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.
Subscribe to:
Posts (Atom)