My Weblog

Just another WordPress.com weblog

Java : Making my first object

object1

I will create object based on the UML above

First = Write a class (Dog)

package dogtestdrive;
public class Dog {

    int size;          // Instance Variable
    String breed;      // Instance Variable
    String name;       // Instance Variable

    void bark() {
        System.out.println ("Ruff! Ruff!"); // a Method
    }
}


2. Write a tester (TestDrive) class

package dogtestdrive;
public class Main {
    public static void main(String[] args) { // main method ( we're gonna put
                                             // code in it in the next step
                                            // TODO code application logic here
    }
}

In the tester, make an object and access the object’s variables and methods

package dogtestdrive;
public class Main {
    public static void main(String[] args) { // main method ( we're gonna put
                                             // code in it in the next step
                                            // TODO code application logic here

        Dog d = new Dog(); // Make a Dog object
        d.size = 40; // Use the dot operator (.) to set the size of the Dog
        d.bark(); // and to call it's bark() method
    }
}

Output

run:
Ruff! Ruff!
BUILD SUCCESSFUL (total time: 0 seconds)

		

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: