14622
14622
14622
Constructors
publicClassB(intone,inttwo){
...
}
voidClassC(){
...
}
1. If the data member speed is private, is the following statement valid in a client
program?
Robotaibo;
aibo=newRobot();
doublecurrentSpeed=aibo.speed;
2. Suppose you wrote down important information such as your bank account number,
student registration ID, and so forth, on a single sheet of paper. Will this sheet be
declared private, and kept in your desk drawer, or public, and placed next to the
dorms public telephone?
Class Constants
1. Declare two class constants named MIN_BALANCE and MAX_BALANCE whose data
types are double.
3. Modify the Dice class so its instances will generate a number between
5 and 15, inclusively.
Local Variables
1. Suppose a class Alpha includes a method called compute that accepts no arguments.
Define another method of Alpha named myMethod that calls the compute method.
2. Why should duplication of code be avoided?
Returning an Object from a Method
classQuestion{
Personstudent;
publicvoidgetStudent(){
returnstudent;
}
...
}
2. Define a Vehicle class. It has a data member owner of type Person. Include an
accessor to retrieve the owner person and a mutator to set the owner.
The Reserved Word this
1. Write a single statement to express the following operations on fractions using the
methods from the Fraction class:
f5=(f1+f2)/(f3f4)
3. Write statements to assign the sum of fractions f1 and f2 to fraction f3 using the add
2. Define a Student class. A Student has a name. Define two constructors, one with no
argument and another with the name as its argument. Initialize the name to a default
value Unknown for the zero-argument constructor.
3. Rewrite the following constructors, so the first one calls the second one:
publicClassOne(intalpha){
this.alpha=alpha;
this.beta=0;
}
publicClassOne(intalpha,intbeta){
this.alpha=alpha;
this.beta=beta;
}
1. What is the name of the scheme used in Java to pass arguments to a method?
2. What is the output from the following code?
classQuestion{
privateintone;
publicvoidmyMethod(intone){
this.one=one;
one=12;
}
}
classTest{
publicstaticvoidmain(String[]arg){
intone=30;
Questionq=newQuestion();
q.myMethod(one);
System.out.println(one);
}
}