Linggo, Hulyo 24, 2011

My Java Projects at Home

package com.accenture.sample;

public class Bicycle {

    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;
   
    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
   
    // the Bicycle class has four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
   
    public void setGear(int newValue) {
        gear = newValue;
    }
   
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
   
    public void speedUp(int increment) {
        speed += increment;
    }
   
}
----------------------------------------------------------------------------

package com.accenture.sample;

public class MyBicycle extends Bicycle {
   
    // the MountainBike subclass has one field
    public int seatHeight;

    // the MountainBike subclass has one constructor
    public MyBicycle(int startHeight, int startCadence, int startSpeed, int startGear) {
        super(startCadence, startSpeed, startGear);
        seatHeight = startHeight;
    }   
   
    // the MountainBike subclass has one method
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }   

}
----------------------------------------------------------------------------

package com.accenture.sample;

public class ObjectSample {
   
    public static void main(String[] args) {

       
        Ball basketBall = new Ball();
        Ball volleyBall = new Ball();

        basketBall.getVolume(250);
        basketBall.getRadius(200);
        basketBall.printState();

        volleyBall.getVolume(150);
        volleyBall.getRadius(100);
        volleyBall.printState();
    }
}

class Ball {;
   
    public int radius;
    public int volume;
    String shape = "circle";

    public void getRadius(int newDiameter) {
        radius = newDiameter / 2;
    }

    public void getVolume(int newVolume) {
        volume = newVolume;
    }

    public void printState() {
        System.out.println("Volume: " + volume + " " + "Radius: " + radius
                + " " + "Shape: " + shape);
    }

}

interface BallMethods{
   
    public void getRadius(int newDiameter);

    public void getVolume(int newVolume);

    public void printState();   
}

---------------------------------------------------------------------------




package com.accenture.sample;

public class TestCoding {

    static int ctr = 0;

    public static void main(String[] args) {
        disp1();
        disp2();
        disp3();
       
        boolean check;
       
        check = isGreater(200,1000);
        System.out.println("Is First Number Greater Than Second Number? : " + check);
       
    }

    // Example For Loop
    static void disp1() {
        for (ctr = 0; ctr < 10; ctr++) {
            System.out.println("DM1: Counting... " + (ctr + 1));
        }
    }

    // Example While Loop
    static void disp2() {
        ctr = 0;
        while (ctr < 10) {
            System.out.println("DM2: Counting... " + (ctr + 1));
            ctr++;
        }
    }
   
    // Example Do-While Loop
    static void disp3() {
        ctr = 0;
        do {
            System.out.println("DM3: Counting... " + (ctr + 1));
            ctr++;
        } while (ctr < 10);
    }
   
    //Example Method Accepting Values and Returning Values
    static boolean isGreater(int a, int b) {
        boolean answer = (a > b) ? true : false;
        return answer;
    }
}

Walang komento:

Mag-post ng isang Komento