Jan 7, 2008

Getting Object Oriented

Alright! So this post is all about objects! JAVA itself is an object-oriented language meaning that pretty much everything revolves around objects. Objects are instances of something that have a current state and can do things. Such as my computer, it is on and it is inputting what I’m typing. So really objects can be about anything. Now how do we make ourselves an object?

We have already made code that has the main elements in java:
classes
main() method
variables
statements

Now let’s start building ourselves a small program and we’ll make it about a computer.

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

}
}

Now we are going to give our computer it’s state, or in other words what it is currently with what are called instance variables.

public class Computer{
boolean isOn;
String typeOfComputer;
String currentScreen;

public static void main(String[] args) {

}
}

Now we are going to add a couple of methods. Methods are what we use to control the instance variables.

public class Computer{
boolean isOn;
String typeOfComputer;
String currentScreen;

public static void main(String[] args) {

}

public void turnOn() {

}
public void changeType() {

}
public void changeCurrentScreen() {

}
}

turnOn(), changeType() and changeCurrentScreen() are our methods but they don’t do anything... So we’ll give them something simple to do that corresponds with their name.

public void turnOn() {
isOn = true;
}
public void changeType() {
int x = (int) Math.random() * 3 + 1;
if (x == 1){
typeOfComputer = "Mac";
} else if(x == 2){
typeOfComputer = "Alienware";
} else if (x == 3){
typeOfComputer = "Super Computer";
}

}
public void changeCurrentScreen() {
int x = (int) Math.random() * 2 + 1;
if (x == 1) {
currentScreen = "Home screen";
} else if(x == 2) {
currentScreen = "Google";
}
}

The turnOn() method now turns on our computer
changeType() method now randomly changes the type of computer
changeCurrentScreen()method now randomly changes our computer screen

Couple tidbits that need to be explained...
int x = (int) Math.random() * 3 + 1;

within the method we are declaring some integer x, we’ll talk about it more later on (not today) but x only exists within the method it is declared then it dies. You can’t use it elsewhere unless you declare it again.

The (int) casts the random number we make between 1 and 4 into an integer, it truncates it therefore giving us 1, 2 or 3.

Now it’s nice and all having these methods but how do we use them? We have to call/invoke them.

So we’ll try this...

public static void main(String[] args) {
turnOn();
changeType();
changeCurrentScreen();
}

Won’t work...

The computer does not know what you are talking about. We have the code for our method to control our object but we have no object and we haven’t actually made those controls we have just wrote the code for them (I’ll admit that sounds a little confusing for the moment).

To create our Computer object we need a line of code that looks like what follows:

Computer myComputer = new Computer();

When we create this myComputer object it creates everything within the class so we have also made the methods/controls for this object and this object must be created before we can do anything to it so we'll put it first in the main() method.
This is a statement that creates a new Computer object called myComputer. All these statements generally follow this format:

ClassName objectName = new ClassName();

ClassName is the name of the class that you want to create, the object that you want to create.
ClassName() is called a constructor and you need those brackets, we’ll do more with constructors later.

Now we can fix our code and tell the computer what we want it to do and what we want it to do it to.

public static void main(String[] args) {
Computer myComputer = new Computer();
myComputer.turnOn();
myComputer.changeType();
myComputer.changeCurrentScreen();
}

Then there is just one last thing we need to do with our program. We did all of these changes to myComputer but what do we have to show for it? Bupkiss. So we’ll make another method to display our info.

public void display() {
System.out.println("Is the computer on? " + isOn);
System.out.println("What type of computer is it? " + typeOfComputer);
System.out.println("What is the current screen? " + currentScreen);

}

Remember that the “+” concatenates strings and the variables together.

Then you just have to invoke this method after you have changed the instance variables and your good.

public static void main(String[] args) {
Computer myComputer = new Computer();
myComputer.turnOn();
myComputer.changeType();
myComputer.changeCurrentScreen();
myComputer.display();
}

So our whole code ends up being:

public class Computer {
boolean isOn;
String typeOfComputer;
String currentScreen;

public static void main(String[] args) {
Computer myComputer = new Computer();
myComputer.turnOn();
myComputer.changeType();
myComputer.changeCurrentScreen();
myComputer.display();
}

public void turnOn() {
isOn = true;
}
public void changeType() {
int x = (int) Math.random() * 3 + 1;
if (x == 1){
typeOfComputer = "Mac";
} else if(x == 2){
typeOfComputer = "Alienware";
} else if (x == 3){
typeOfComputer = "Super Computer";
}

}
public void changeCurrentScreen() {
int x = (int) Math.random() * 2 + 1;

if (x == 1) {
currentScreen = "Home screen";
} else if(x == 2) {
currentScreen = "Google";
}
}
public void display() {
System.out.println("Is the computer on? " + isOn);
System.out.println("What type of computer is it? " + typeOfComputer);
System.out.println("What is the current screen? " + currentScreen);

}
}

That was a lot but it covers a massive concept, or at least a small part of a large concept. Hope this is understandable, if not tell me and I'll see what I can do!

Good night!

No comments: