Kosjfn Sdgn
Kosjfn Sdgn

Reputation: 1

Java class definiton?

So I'm fairly new to the language, and my teacher just gave us this asignment where we create a game using 5 classes. He gave us the class names, and told us to go off of the code in our text book.

From the assignment sheet:
1. Create a Pig Game for exactly two players, a user is one and the computer is the other. You will have to create exactly five classes and they are:
a. Die
b. Pair of Die
c. Player
d. PigGame or PigReferee
e. PlayPig (this will contain the main driver)

Code given:

import java.util.Random;

public class Die {
    private final int MIN_FACES = 4;

    private static Random generator = new Random();
    private int  numFaces; //number of sides on the die
    private int faceValue; //current value showing on the die

    //-----------------------------------------------------------------------------------|
    //  Defaults to a six-sided die. Initial face value is 1.                            |
    //-----------------------------------------------------------------------------------|
    public Die(){
        numFaces = 6;
        faceValue = 1;
    }

    //-----------------------------------------------------------------------------------|
    //Explicitly sets the size of the die. Defaults to a size of six if the parameter is |
    //invalid. Initial face value is 1.                                                  |
    //-----------------------------------------------------------------------------------|
    public Die(int faces){
        if (faces < MIN_FACES){
            numFaces = 6;
        }
        else{
            numFaces = faces;
        }
        faceValue = 1;
    }

    //-----------------------------------------------------------------------------------|
    //  Rolls the die and returns the result.                                            |
    //-----------------------------------------------------------------------------------|
    public int roll(){
        faceValue = generator.nextInt(numFaces) + 1;
        return faceValue;
    }

    //-----------------------------------------------------------------------------------|
    //  Returns the current faceValue.                                                   |
    //-----------------------------------------------------------------------------------|
    public int getFaceValue(){
        return faceValue;
    }
}

So my question is, is Die the only current class, or does the "public int roll" count as a class as well. What makes a class? Thanks, Dizzy

Upvotes: 0

Views: 180

Answers (2)

Rohan Kandwal
Rohan Kandwal

Reputation: 9326

Die is only your class here. Just like you made the Die class you will have to make the other classes. Then you will have to use what is called Inheritance. The main class will be i suppose PlayPig. Inheritance is very important topic in java and i don't know how your teacher gave you a project without explaining it. Anyhow here are some links for your problem.

1st link 2nd link 3rd link

Upvotes: 0

KPD
KPD

Reputation: 5883

Die is your only current class. You can tell because the keyword class is used when it is defined. public int roll() is a method inside of the Die class.

You can also normally tell what is a class is because each class will have their own .java file named after that class. So your Die class should be defined in a Die.java file. The other four classes you need to create will also each be in their own .java files.

Upvotes: 1

Related Questions