Tuesday 11 August 2015

Java Tutorial : What is a Class[State and Methods]?


Click here to watch in Youtube :
https://www.youtube.com/watch?v=__WoZY3kHAI&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : What is a Class[State and Methods]? 

Car.java
/**
 * Car class is the blueprint from which individual car objects are created.
 */
public class Car
{
    int gear = 1;
    int speed = 0;

    void changeGear(int newValue)
    {
        gear = newValue;
    }

    void speedUp(int increment)
    {
        speed = speed + increment;
    }

    void applyBrakes(int decrement)
    {
        speed = speed - decrement;
    }

    void printStates()
    {
        System.out.println( "gear:" + gear+" speed:" + speed );
    }

}
CarDemo.java
public class CarDemo
{
    public static void main(String[] args)
    {

        System.out
                .println("Change Maruthi Alto K10 Car gear to 2 and increase the speed to 100");

        Car maruthiAltok10 = new Car();

        maruthiAltok10.changeGear(2);
        maruthiAltok10.speedUp(100);

        maruthiAltok10.printStates();

        System.out.println("------------------------------");

        System.out
                .println("Change Swift Car gear to 3 and increase the speed to 200");

        Car swift = new Car();

        swift.changeGear(3);
        swift.speedUp(200);

        swift.printStates();

        System.out
        .println("Change Swift Car gear to 1 and apply break and decrease the speed from 200 to 150");
        
        swift.changeGear(1);
        swift.applyBrakes(50);

        swift.printStates();

    }
}
Output
Change Maruthi Alto K10 Car gear to 2 and increase the speed to 100
gear:2 speed:100
------------------------------
Change Swift Car gear to 3 and increase the speed to 200
gear:3 speed:200
Change Swift Car gear to 1 and apply break and decrease the speed from 200 to 150
gear:1 speed:150

To Download CarDemoStateAndMethods Project Click the below link
https://sites.google.com/site/javaee4321/java/CarDemoStateAndMethods.zip?attredirects=0&d=1

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment