View Single Post
Old 29-04-2004, 22:27   #1 (permalink)
Datapunter
Pro Punter
 
Datapunter's Avatar
 
Join Date: 23 Oct 2003
Location: Westdorpe
Age: 43
Posts: 5,522
Default Lesson 3 - Language, a bit more about objects

Lesson 3 ( Word document )

Punters Lounge JAVA programming course.
Lesson 3 - Learning the language, a bit more about objects.

3.1 Scoreboard object

Imagine a scoreboard as an object. In our case let's take a football scoreboard. It can contain 2 values being the score for each team. Its declaration looks like this:

File: Scoreboard.java

public class Scoreboard {
    public int home = 0;
    public int away = 0;

    public Scoreboard(int home, int away)
    {
      this.home = home;
      this.away = away;
    }
}


Now in our program we can use this line to create a scoreboard object:

Scoreboard Localgame = new Scoreboard(0,0);

This line consists of 3 parts: Declaration, Instantiation and Initialisation.

Scoreboard Localgame is the declaration telling Java that the name Localgame is going to be used for an object of the type Scoreboard. That's all, you tell Java that you intend to use that name for that type of object.

new Scoreboard will actually create an object of the type Scoreboard. So the new command will take the class Scoreboard as example or template and from that create a new object of the type Scoreboard.

Scoreboard(0,0) is a call to a constructor inside the object Scoreboard. In the above code it's the last 3 lines. This will initialise the newly formed object and set its initial values to 0.


So the line Scoreboard Localgame = new Scoreboard(0,0); creates a new object of the type Scoreboard gives it the name Localgame and sets its initial values to 0.

3.2 Scoreboard object, state

How do we get at an object values or state ? By calling it by name like this:

Localgame.home and Localgame.away

This way we use the object Localgame and inside that object the integers home and away.


Let's put this in a program. First you need a file called Scoreboard.java containing the above code. That is the program or class for a scoreboard object. Then we create our program called Lesson3.java. When we compile and run our program Lesson3.java Java will recognise that Scoreboard is used and automatically include that in the overall compilation. So the only thing we need to do to compile is: CJAVA>JAVAC Lesson3.java
(Remember there is a difference between upper/lower case characters!)


File Lesson3.java

public class Lesson3 {
public static void main(String[ ] arguments) {

    Scoreboard Localgame = new Scoreboard(0,0);

    System.out.println(Localgame.home);
    System.out.println(Localgame.away);
}
}


When we run our second program it displays 0 twice.
During the course of our program we can set the values of our scoreboard like this:

Localgame.home = 1;
Localgame.away = 2;


Full program becomes:

public class Lesson3 {
public static void main(String[ ] arguments) {

    Scoreboard Localgame = new Scoreboard(0,0);

    Localgame.home = 1;
    Localgame.away = 2;

    System.out.println(Localgame.home);
    System.out.println(Localgame.away);
}
}


When we now run the program it will display a 1 and 2.


3.3 Scoreboard object, method

Now let's include a method. Suppose we want our scoreboard to tell us the total number of goals scored. The Scoreboard class becomes this:

File: Scoreboard.java

public class Scoreboard {
    public int home = 0;
    public int away = 0;

    public Scoreboard(int home, int away)
    {
      this.home = home;
      this.away = away;
    }

    public int goals()
    {
      return home + away;
    }
}


The method adds the value of home and away and returns this to the calling program as an integer value.

public int goals()

declares the method named goals and tells the compiler it will return a value of the type int. The brackets are empty but required. We'll come to that later.

return home + away;

This will execute the + operator adding the values of home and away and the return statement will return the result to the calling program.

public class Lesson3 {
public static void main(String[ ] arguments) {

    Scoreboard Localgame = new Scoreboard(0,0);

    Localgame.home = 1;
    Localgame.away = 2;

    System.out.println(Localgame.home);
    System.out.println(Localgame.away);

    System.out.println(Localgame.goals());
}
}



Will display 1, 2 and 3 as values.


3.4 Printing text to the screen

Later in the course we will look at text. For now here's how to display text on the screen:

System.out.println("This is text");

Normally you cannot combine integers and text. You can however combine integers and text using the System.out.println method. It will recognise both and handle them separately. Like this:

System.out.println("Home team goals : " + Localgame.home);

Will display following text on the screen:

Home team goals : 1






Assignment 3.1 ( Lesson31.java )
Modify the Lesson3 so it displays Under or Over based on the value of the number of goals.


Assignment 3.2 ( Lesson32.java )
Modify the Lesson3 so it displays the words Home win or Away win or Draw game based on the values of the scoreboard.


Assignment 3.3 ( Lesson33.java )
Tkae the previous program and make it look at bit nicer, like this:
Today's game ended in a Home Win with a total of 3 goals scored
Make sure that when you change only the score at the beginning of the program, like 0 - 0 it displays the change at the end:
Today's game ended in a Draw with 0 goals for each team


Assignment 3.4 ( Lesson34.java )
Combine the Lesson2 and the Lesson3 programs into Lesson34 such that a Home win will be a winning bet and an Away win or Draw is a losing bet.

So a draw score displays: Draw game , bank = 900
And an Away win display: Away win , bank = 900
And a home win displays: Home win , bank = 1200

So the end result looks like this for a 1 - 0 Home win:
1
0
Today's game ended in a Home Win with a total of 1 goals scored
Home win , Bank = 1200
Datapunter is offline