English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm having a little trouble in my java class. This is the assignment:

Create a Dice class.
Requirements:
-At least two instance fields
-At least two constructors (in addition to the default)
-At least four main methods

One die will return a random number using the user-specified number of sides and the low number on the die (the other numbers go up by ones from the low number).

Another die will simulate a six-sided colored die that produces a random number from 1-6 (and those numbers are assigned to certain colors to return to the user).

This is a simple text program, no graphics. Can someone help me at least get started if not help chuck out a lot of the code? I have been on top of everything until now. I'm not so sure how all of this 'create your own method' works.

2007-02-01 12:23:59 · 1 answers · asked by Anonymous in Computers & Internet Programming & Design

1 answers

You'll want to start with something along these lines:

(i'm a c# kid so my java may be a little off)

/* first die class*/
public class Die {
private int sides;
private int lowNum;

//default constructor
public Die() {
sides = 6;
this.lowNum = 1;
}

//constructor that takes a number of sides
public Die(int sides) {
this.sides = sides;
this.lowNum = 1;
}

//constructor that takes a number of sides, and the low-number
public Die(int sides, int low) {
this.sides = sides;
this.lowNum = low;
}

//'roll' the die
public int Roll() {
return rand(this.lowNum,this.sides); //rand between the low-num and the number of sides
}
}

/*******/

This is not a complete solution, but hopefully can get you started. I think what you'll want to do for the 2nd 'die' class is to inherit from the first, and override any operations that are different.

Good luck!

2007-02-01 13:04:15 · answer #1 · answered by fixedinseattle 4 · 0 0

fedest.com, questions and answers