Consider the following Balance and bankAccount classes for the first five questions:
public class Balance {
private double amt;
public Balance() {amt=0;}
public void add(double a) {amt+=a;}
public void subtract(double a) throws negativeBalanceException {
if (a>amt)
throw new negativeBalanceException ();
amt -=a;
}
public double balance() {return amt;}
}
public class bankAccount {
private int accountNumber;
private String name;
private Balance b;
public bankAccount(int an, String n) {
accountNumber=an;
name=n;
b= new Balance();
}
public void deposit (double am) {
b.add(am);
}
public void withdrawal (double am) throws negativeBalanceException {
b.subtract(am);
}
public double currentBalance() {return b.balance();}
}
(30 Points) 1. Examine the following
code segments for legal and illegal uses of the classes Balance and
bankAccount, their objects, and their components in an application that imports
them. Assume that Balance
x= new Balance();
and bankAccount y= new
bankAccount(1234,"Jones");
already exist in an
application. Clearly mark each as valid or invalid, when invalid, give a
brief explanation.
(20 Points) 2. Write the sequence of instructions that would:
a. Declare two bankAccount variables name them x and y.
b. Create a bankAccount object with your name and an account number of your choice as its arguments and bind it to both of these variables (i.e. both x and y need to reference this object).
c. Deposit 1000 dollars into the account, once using x and a second time using y.
d. Withdraw 10 dollars from the account, once using x and a second time using y. Take care of catching the exception.
e. Display
the balance left in the account with x or y. What do you expect
as output?
(10 Points) 3. Assume that bankAccount x = new
bankAccount(1234,"Jones");
already exists in an
application and we have already put some money in Jones's bank account object (x).
Write the instruction(s) that would remove all the money in his account.
Here is the skeleton of class Tank that you developed for lab#2:
public
class Tank {
protected double capacity_;
protected double content_;
public Tank()
public Tank(double init_cap)
public double content ()
public double capacity ()
public void add(double amount) throws
TankOverFlowException, IllegalArgumentException
public void remove(double amount) throws
TankUnderFlowException,
IllegalArgumentException
(10 Points) 4. Write a method percentageFull to be added to the list of existing methods of Tank class. This method provides us with the percentage of fullness for a Tank. A double is returned which is arrived at from content divided by capacity multiplied by 100.0.
(10 points) 5. Assuming that we are developing an application that has the
following:
Tank t= new Tank(100);
Write the instructions in this application that would loop through and add 1.0 gallon of liquid to the tank during each iteration until it is at least half full. You must use the percentageFull method developed in the previous question.
(10 Points) 6. True/False:
(10 Points) 7. Here is the skeleton of class light used in assignment #1.
import java.awt.*;
public class light extends Canvas {
protected Color onColor_=Color.red;
protected Color offColor_=Color.lightGray;
protected boolean on_=true;
protected int size_=360;
public light ()
public light (int s, boolean on, Color onColor, Color offColor)
public int lightSize ()
public boolean on()
public void change ()
public void paint(Graphics g)
}
Suppose we are writing an applet.