Total Pageviews

Tuesday, March 27, 2012

(java) SKILL BUILDER : FROG RACING

/**
 * This class implements different behaviors of frog racers.
 *
 * @author Your Shadow
 * @version  1.0.0
 */
public class FrogBehaviors {

    /**
     * Models a frog that allways jumps forward one cell.
     *
     * @param frogRacer  the frog that performs this behavior.
     */
    static public void forwardRacer(Frog frogRacer) {
   

frogRacer.jumpForward(1);
    }

    /**
     * Models a frog that:
     * 50 percent of the time jumps forward two cells and
     * 50 percent of the time stays in the same cell.
     *
     * @param frogRacer  the frog that performs this behavior.
     */
    static public void stayOrJumpRacerOne(Frog frogRacer) {
   
        int value = Frog.randomValue(0, 1);

        if (value == 0) {
            frogRacer.jumpForward(2);
        } else {
            // do nothing
        }
    }

    /**
     * Models a frog that:
     * 33 percent of the time jumps forward two cells,
     * 33 percent of the time jumps forward one cell, and
     * 33 percent of the time stays in the same cell.
     *
     * @param frogRacer  the frog that performs this behavior.
     */
    static public void stayOrJumpRacerTwo(Frog frogRacer) {
   
        int value = Frog.randomValue(0,2);

        if(value == 0)
            {
            frogRacer.jumpForward(2);
            }
        else if(value == 1)
            {
                    frogRacer.jumpForward(1);
            }
             else if(value==2)
            {
                    frogRacer.jumpForward(0);
            }
           
       
    }

    /**
     * Models a frog that:
     * 50 percent of the time jumps forward three cells, and
     * 50 percent of the time jumps backward two cells.
     *
     * @param frogRacer  the frog that performs this behavior.
     */
    static public void backwardOrForwardRacerOne(Frog frogRacer) {
        int value = Frog.randomValue(0, 1);

        if (value == 0) {
            frogRacer.jumpForward(3);
        } else if(value ==1) {
            frogRacer.jumpBackward(2);
        }
       
    }

    /**
     * Models a frog that:
     * 25 percent of the time jumps forward four cells,
     * 25 percent of the time jumps forward one cells
     * 25 percent of the time jumps backward one cells, and
     * 25 percent of the time jumps backward two cells.
     *
     * @param frogRacer  the frog that performs this behavior.
     */
    static public void backwardOrForwardRacerTwo(Frog frogRacer) {
   
        int value = Frog.randomValue(0,4);

        if(value == 0)
            {
            frogRacer.jumpForward(4);
            }
        else if(value == 1)
            {
                    frogRacer.jumpForward(1);
            }
             else if(value==2)
            {
                    frogRacer.jumpBackward(1);
            }
        else if(value==3)
            {
                    frogRacer.jumpBackward(2);
            }
       
    }
}

Tuesday, March 20, 2012

SKILL BUILDER DEFINING METHODS

/**
 * Extends the functions of the original <code>Turtle</code> class.
 *
 * @author Your name
 * @version  1.0.0
 */
public class ExtendedTurtle extends Turtle {

    /**
     * Paints its current location and moves to the front location.
     *
     * @param colorName  a string that specifies the color name.
     */
    public void paintAndMove(String  colorName) {
   
        paint(colorName);
        move();
    }

    /**
     * Turns the turtle to the opposite direction.
     */
    public void turn180()
{

turnLeft();
turnLeft();
   
       
    }

    /**
     * Paints and moves four locations.
     *
     * @param colorName  a string that specifies the color name.
     */
    public void paintAndMove4(String  colorName) {

paintAndMove(colorName);
paintAndMove(colorName);
paintAndMove(colorName);
paintAndMove(colorName);
       
    }

    /**
     * Paints a 2 x 2 square and returns to its original position.
     *
     * @param colorName  a string that specifies the color name.
     */
    public void paintSmallSquare(String  colorName) {

    paintAndMove(colorName);
turnRight();
paintAndMove(colorName);
turnRight();
paintAndMove(colorName);
turnRight();
paintAndMove(colorName);
turnRight();
   
       
       
    }

    /**
     * Paints a 4 x 4 square and returns to its original position.
     *
     * @param colorName  a string that specifies the color name.
     */
    public void paintLargeSquare(String  colorName) {


      paintAndMove(colorName);
          paintAndMove(colorName);
          paintAndMove(colorName);
          turnRight();

          paintAndMove(colorName);
          paintAndMove(colorName);
          paintAndMove(colorName);
          turnRight();
         
          paintAndMove(colorName);
          paintAndMove(colorName);
          paintAndMove(colorName);
          turnRight();

          paintAndMove(colorName);
          paintAndMove(colorName);
          paintAndMove(colorName);
          turnRight();
move();
turnRight();
paintAndMove(colorName);
          paintAndMove(colorName);
          paintAndMove(colorName);

  turnLeft();
   turnLeft();
move();
   paintAndMove(colorName);
          paintAndMove(colorName);
 
     turnRight();
move();
turnRight();
paintAndMove(colorName);
paintAndMove(colorName);
paintAndMove(colorName);
    }
}

Thursday, March 15, 2012

JAVA PROGRAM TO PRINT TABLE

class Table
{
public static void main(String args[])
 {
int a=2,b=1,c;

for(b=1;b<=10;b++)
{
c=2*b;
System.out.println("2 *"+b+"="+c);
}
}
}

JAVA PROGRAM TO FIND GREATER OF 2 NUMBERS

class Greater
{
public static void main(String args[])
 {
int a=20,b=30;
if(a>b)

System.out.println("A is greater than B");

else
System.out.println("B is greater than A");
}
}

Tuesday, March 13, 2012

java program class

class Student
{
public int a,b,c,d;
public Student(int w,int x,int y,int z)
{
a=w;
b=x;
c=y;
d=z;
}
public void calAverage()
{
System.out.println((a+b+c+d)/4);
}
}




========================================================


class Test
{
public static void main(String args[])
{
Student xyz = new Student (40,40,40,40);
xyz.calAverage();
}
}