14622

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment

Defining and Using Multiple Classes

1. What is the output from the following code fragement?


Accountacct;
acct=newAccount();
acct.setInitialBalance(250);
acct.add(20);
System.out.println("Balance:"
+acct.getCurrentBalance());

2. Write a code fragment to declare and create two Account objects


named acc1 and acct2. Initialize the balance to $300 and $500, respectively.
Set the name of owner for both accounts to John Doe.

Constructors

1. Which of the following constructors are invalid?


publicintClassA(intone){
...
}

publicClassB(intone,inttwo){
...
}
voidClassC(){
...
}

2. What is the main purpose of a constructor?


3. Complete the following constructor :
classTest{
privatedoublescore;
publicTest(doubleval){
//assignthevalueofparameterto
//thedatamember
}
}

Information Hiding and Visibility

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.

2. Is there any problem with the following declarations?


classQuestion{
privatefinalintMAX=20;
...
}

3. Modify the Dice class so its instances will generate a number between
5 and 15, inclusively.
Local Variables

1. How is a local variable different from an instance variable?


2. Rewrite the following method using local variables:
publicinttotalCharge(intamt){
return(balance(int)Math.round(amt*1.5));
}

Calling Methods of the Same Class

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

1. Whats wrong with the following declaation?

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)

2. If the add method is defined thus


publicvoidadd(Fractionfrac){
inta,b,c,d;
a=this.getNumerator();//getthisfractions
b=this.getDenominator();//numanddenom
c=frac.getNumerator();//getfracsnum
d=frac.getDenominator();//anddenom
setNumerator(a*b+c*b);//updatesthis
setDenominator(b*d);//fractionsnumanddenom
}

why is it wrong to use the method as


f3=f1.add(f2);

3. Write statements to assign the sum of fractions f1 and f2 to fraction f3 using the add

method defined in Quick Check question 2 above.


Overloaded Methods and Constructors
1. Are there any conflicts in the following three constructors for ClassX to be valid?
publicClassX(intX){
...
}
publicClassX(floatX){
...
}
publicClassX(intY){
...
}

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;
}

Call-by-Value Parameter Passing

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);
}
}

You might also like