Showing posts with label methods. Show all posts
Showing posts with label methods. Show all posts

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!

Dec 17, 2007

Your Basic Blend

Hello there!

Alright so now we get to some of the basic code structure of Java!

First we have the source file.
- Inside your source file you put your classes.
-Inside your classes you put your methods.
-Inside your classes you put your statements.

A source file is a file with a .java extension.
Then inside your source file you have (this would be in your text editor):
(P.S. these statements should be tabbed but blogger takes them out. I'll get on it)

public class Heater {
void heat() {
1stStatement;
2ndStatement;
}
}

So let's make the traditional initiation program "Hello World".

We know it must have a source file, class(es) , method(s) and statement(s).

file HelloWorld.java (in text editor save it as a .java)

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

Alright let's break that down...

public class HelloWorld {
-the public means it can be accessed by all
-class means it is a class...
-HelloWorld is the name of the class
-you must open the class with a brace {

public static void main (String[] args) {
- once again public means it can be accessed by all (as opposed to private which we'll get to)
- static we'll do later
- void means it is a method with no return type
- main() is the name of the method, the main() method always runs first!
- (String[] args), this is a line that creates an "array" of strings and were calling that array args and is a parameter of main(). This allows for information to be entered into the program from the command line as far as I know.
- you must open the method with a brace {

System.out.print("Hello World");
-System.out.print puts the string(characters, letters, numbers strung together) that is in the parenthesis () and quotes "" into the command line.
- you must end a statement with a semicolon ;

Then you finish up by closing the method then the class.

Now we want to run this.
First we must compile this into a .class file.
Navigate to the folder your file is in in command prompt (unsure for mac)
Type dir to check that the .java file is in the directory.
Then now that we have the Javac command because of the last post type:
javac HelloWorld.java

If there are no errors in your program this will compile. Otherwise your going to get some odd messages. I'll put some common errors and errors I made when I first did this.

- Java is case sensitive, capitols matter!
- All statements must end with a semicolon!
- Your string must be within quotes!
- You must open classes and methods with opening braces!
- You must close classes and methods with closing braces!

Alright so if you have your .class file now you can type (still in the same directory):
java HelloWorld
and voila! You get the uber-exiting message Hello World.

Now that you have that you can edit your original code to make that say whatever you want. If you want the message to appear on new lines you just have to put in another identical statement with a different string. Have fun!