sandhu
sandhu

Reputation: 117

Basic java for creating object

I m new to java,

Now i m learning java from this link

When i come to know about objects and class,

First part:

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    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("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

Here created one class name "Bicycle" without main method, and it contains fields and methods.

second part:

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

        // Create two different 
        // Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on 
        // those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
} 

Here it contains main method and created two objects, but

why here created another one class "BicycleDemo"

I just confused, It is simple question,

If anyone explain me, As beginners like me, it will help.

Thanks,

Upvotes: 0

Views: 1398

Answers (6)

Ankit Rathore
Ankit Rathore

Reputation: 46

Here BicycleDemo is a controller class. It has a main function which is an entry point. From this class you can create any number of objects for any class. You could also add main function in your Bicycle Class. Writing separate beans is best practices

Upvotes: 1

smac89
smac89

Reputation: 43068

Since everything in Java is an object and has to be declared in a class, there is no way to get around first declaring a class to do the most simple thing. HelloWorld.java program:

public class HelloWorld {
    public static void main(String []args) {
        System.out.println("Hello World");
    }
}

Of course another option will be to simply copy the main method from the Demo class into your Bicycle class and this will still work

public class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    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("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }

    public static void main(String[] args) {

        // Create two different 
        // Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on 
        // those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

Upvotes: 0

almightyGOSU
almightyGOSU

Reputation: 3739

BicycleDemo contains the public static void main (String [] args) method, which is like the entry point for all Java programs.

Essentially, the BicycleDemo acts as a driver class for you to test the functions of the Bicycle class.

Take a look at this to understand why is there a BicycleDemo


Combined into one BicycleDemo.java

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

        // Create two different Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) { cadence = newValue; }

    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("cadence:" + cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

Option 2: main method inside Bicycle

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) { cadence = newValue; }

    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("cadence:" + cadence + " speed:" + 
             speed + " gear:" + gear);
    }

    public static void main(String[] args) {

        // Create two different Bicycle objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Invoke methods on those objects
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

Upvotes: 1

warn974
warn974

Reputation: 80

Bicycle is the class where you define your bicycle, that permit you to create lot of bicycle when you instantiate it.

BicycleDemo contains the main method of your program, commonly call 'entry point'. In this class you write what your program will do with your bicycles.

Upvotes: 1

Rajesh
Rajesh

Reputation: 2155

BicycleDemo is created to show you how to create an object, to explain encapsulation concept. Now you have access to different Bicycles.

Upvotes: 1

Ritesh  Karwa
Ritesh Karwa

Reputation: 2254

The simplest answer is that if you put everything into one class, you have to worry about everything at once when you're writing new code. This may work for small projects, but for huge applications (we're talking hundreds of thousands of lines), this quickly becomes next to impossible.

To solve this issue, you break up pieces of functionality into their own classes and encapsulate all the logic. Then when you want to work on the class, you don't need to think about what else is going on in the code. You can just focus on that small piece of code. This is invaluable for working efficiently, however it's hard to appreciate without working on applications that are huge.

Of course there are countless other benefits to breaking your code into smaller pieces: the code is more maintainable, more testable, more resusable, etc., but to me the largest benefit is that it makes massive programs manageable by reducing the amount of code you need to think about at one time.

Upvotes: 1

Related Questions