ch06
ch06
Classes
Using the RandomGenerator Class
while (true) {
total = rollTwoDice();
if (total == point) {
println(“You made your point. You win.”;
break;
} else if (total == 7) {
println(“That’s a 7. You lose.”);
break;
}
The javadoc documentation system
• Unlike earlier languages that appeared before WWW,
Java was designed to operate in web-based
environment.
• One of the most important ways in which Java works
together with web is the design of its documentation
system, called javadoc.
• The Javadoc application reads Java source files and
generates documentation for each class.
• You can see the complete documentation for the ACM
Java Libraries from
http://jtf.acm.org/javadoc/student/
Sample javadoc pages
acm.util
Class RandomGenerator
This class implements a simple random number generator that allows clients to
generate pseudorandom integers, doubles, booleans, and colors. To use it,
the first step is to declare an instance variable to hold the random generator
as follows:
Method Summary
RandomGenerator getInstance()
Returns a RandomGenerator instance that . . .
boolean nextBoolean(double p)
Returns a random boolean value with specified . . .
Method Detail
public RandomGenerator()
Returns a RandomGenerator instance that can be shared among several classes.
/**
* Returns whether the student is paid up.
* @return Whether the student is paid up
*/
public boolean isPaidUp() {
return paidUp;
}
Overriding
Overriding toString defined in Object class
public String toString() {
return studentName + “ (#” + studentID + “)”;
}
• toString, which converts the value of an object into
string, is used by println.
• The + operator automatically converts int to String.
• By redefining (overriding) toString, println can print the
value of an object Student in more readable form, such
as
Hermione Granger (#314159)
Using the Student class
• Once you have defined the Student class, you can then
use its constructor to create instances of that class. For
example, you could use the following code to create two
Student objects
Student choseOne = new Student(“Harry Potter”, 123456);
Student topStudent = new Student(“Hermione Granger”, 314159);
• You can then use the standard receiver syntax to call
methods on these objects.
topStudent.setCredits(97);
choseStudent.getName();
Rational numbers
/**
* Creates a new Rational from the integer argument.
* @param n The initial value
*/
public Rational(int n) {
this(n, 1);
}
The Rational class constructor
/**
* Creates a new Rational with the value x/y.
* @param x The numerator of the rational number
* @param y The denominator of the rational number
*/
public Rational(int x, int y) {
int g = gcd(Math.abs(x), Math.abs(y) {
num = x / g;
den = Math.abs(y) / g;
if (y < 0) num = -num;
}
/**
* Adds the rational number r to this one and returns the sum.
* @param r The rational number to be added
* @return The sum of the current number and r
*/
public Rational add(Rational r) {
return new Rational(this.num * r.den + r.num * this.den,
this.den * r.den);
}
/** Create a new FilledRect with the specified bounds and color. */
public FilledRect(double x, double y, double width, double height, Color color) {
this(x, y, width, height);
setColor(color);
}
Rules for inherited constructors