The Dice Object
We have also learned how to write tiny objects that consist only of
data. A person object for example looks like:
//Declare a class that is called person
//Make it public so anyone can use it.
public class person
{ string firstname;
string lastname;
int age;
}
In this case, we have grouped all of the data that is concerned with a
person together in a class. Then we can declare arrays or variables of that
person type. Let’s look at a simple example of an object that has methods AND
data:
Add other objects to this object
import
java.io.*;
Start the class for the methods_in_methods object
Make it public – available to others
public
class methods_in_methods
{
A Main
method is the starting point for running an object
public static void main(String args[])
{ System.out.println(k(1));
}
This is
another method you have made
public static int k(int m)
{ m++;
return m;
}
}
Now, you know how to make your own full blown class.
We are going to
make a dice class. Its data is going to be the side of the dice that is up. The
methods that it needs are roll (to get a new value on top) and drawdice (to draw
it on an applet).
import
java.awt.*;
import java.applet.*;
Start the class for the dice object
Make it public – available to others
public
class dice
{
The data in the class, private so no one can see it
private int up;
private Component
surface;
The dice constructor
It
gets the surface (applet drawing place) so it can draw there too
public dice (Component surface2)
{
up
= (int) Math.random () * 6;
surface
= surface2;
}
The method that allows the user to roll the dice
public int roll ()
{
up
= (int) ((Math.random () * 6)+1);
return up;
}
The method that allows the user to draw the dice on the applet’s
surface
public void drawdice (int x, int y)
{
Graphics
g = surface.getGraphics ();
if
(up == 6)
{
g.setColor (Color.white);
g.fillRect (x, y, 20, 20);
g.setColor (Color.black);
g.drawRect (x, y, 20, 20);
g.fillOval (x + 5, y + 5, 3, 3);
g.fillOval (x + 15, y + 5, 3, 3);
g.fillOval (x + 5, y + 15, 3, 3);
g.fillOval (x + 15, y + 15, 3, 3);
g.fillOval (x + 10, y + 5, 3, 3);
g.fillOval (x + 10, y + 15, 3, 3);
}
else
if (up == 5)
{
g.setColor (Color.white);
g.fillRect (x, y, 20, 20);
g.setColor (Color.black);
g.drawRect (x, y, 20, 20);
g.fillOval (x + 5, y + 5, 3, 3);
g.fillOval (x + 15, y + 5, 3, 3);
g.fillOval (x + 5, y + 15, 3, 3);
g.fillOval (x + 15, y + 15, 3, 3);
g.fillOval (x + 10, y + 10, 3, 3);
}
else if (up == 4)
{
g.setColor (Color.white);
g.fillRect (x, y, 20, 20);
g.setColor (Color.black);
g.drawRect (x, y, 20, 20);
g.fillOval (x + 5, y + 5, 3, 3);
g.fillOval (x + 15, y + 5, 3, 3);
g.fillOval (x + 5, y + 15, 3, 3);
g.fillOval (x + 15, y + 15, 3, 3);
}
else
if (up == 3)
{
g.setColor (Color.white);
g.fillRect (x, y, 20, 20);
g.setColor (Color.black);
g.drawRect (x, y, 20, 20);
g.fillOval (x + 5, y + 5, 3, 3);
g.fillOval (x + 15, y + 15, 3, 3);
g.fillOval (x + 10, y + 10, 3, 3);
}
else
if (up == 2)
{
g.setColor (Color.white);
g.fillRect (x, y, 20, 20);
g.setColor (Color.black);
g.drawRect (x, y, 20, 20);
g.fillOval (x + 7, y + 7, 3, 3);
g.fillOval (x + 12, y + 12, 3, 3);
}
else
{
g.setColor (Color.white);
g.fillRect (x, y, 20, 20);
g.setColor (Color.black);
g.drawRect (x, y, 20, 20);
g.fillOval (x + 10, y + 10, 3, 3);
}
}
}
Now, we need a second class to use this class. It is the game or
whatever we are coding. You have coded lots of these kinds of classes because
you have been using all kinds of prewritten objects without knowing it. Here is
an example:
import java.applet.*;
import java.awt.*;
A class to run the dice object
It extends the Applet class so it can have init, paint and action
public class RunDice extends Applet
{
Declare a dice object – it will have all of the aspects of the dice
you made.
dice Dice1;
dice Dice2;
Button cmdstart;
public void init ()
{
cmdstart
= new Button ("Roll Dice");
add
(cmdstart);
The dice constructor is called. It runs the code in your object.
We pass it ‘this’ which is the applet object we are working in.
This lets the dice object draw on the applet too.
Dice1=
new dice(this);
Dice2=new dice(this);
}
public boolean action (Event e, Object o)
{
if
(e.target == cmdstart)
{
The dice1 is rolled and it gets a new up value.
int x=Dice1.roll();
The dice2 is rolled and it gets a new up value.
int y=Dice2.roll();
showStatus("Dice 1: "+x+" Dice 2: "+y);
The drawdice method is called and the two die are drawn on the screen.
We pass the drawdice method the position to draw each dice.
Dice1.drawdice(100,50);
}
return
true;
}
}
Section 2 Questions
1. There are two files involved in this example.
(a)
What was in each one?
(b)
Did we need to have two files? Explain why or why not.
(c)
Did we have two files when you used buttons and labels? Explain.
2. Using the Dice Object: Find the code that does each of the following.
(a)
How would you declare a dice object?
(b)
How would you construct a dice object?
(c)
How would you roll a dice object?
(d)
How would you draw a dice object?
(e)
Which file do all of the above appear in? Why?
3. Dissecting the Dice Object: Find the answers to the following in the
code.
(a)
What is the Dice Object’s class called?
(b)
What is the Dice Object’s constructor called?
(c)
What are the names of the instance variables in the dice class? What is
each one used for?
(d)
What are the names of the methods in the dice class? What is each one
used for?
(e)
Are the instance variables private or public?
(f)
Are the methods private or public?