Exercises 11.2

 

1.      No

2.      Yes Ð subclass: Squares

3.      No

4.      No

5.      No

6.      Yes Ð subclass: Professors

7.      Yes Ð subclass: Employees

8.      Yes Ð subclass: Computers

9.      No

10. No

11. No

 

12.

     

 

 

13.

     

 

 


14.   

    

 

 

 

15.

 

    

 

 

 

 

16.

    

 

 


17.   

   

 

 

18.

 

   

 

 

 

19.

    

 

 


20.

 

   

 

 

21.

 

   

 

 

 

22.

    

 

 


 

23.

(a)

/** License.java provides a class to model licenses in general.

 */

 

import ann.easyio.*;

 

public class License extends Object

{

 /** License constructor

  *  Receive:       int number, String name, int age

  *  Postcondition: A License object is constructed with myName == name

  *                 &&  myAge && age && myIdNumber == number.

  */

  public License(String name, int age, int number)

  {

    myName = name;

    myAge = age;

    myIdNumber = number;

  }

 

 //--- Accessors ---

  public String getName()  { return myName; }

  public int getAge()      { return myAge; }

  public int getIdNumber() { return myIdNumber; }

 

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a License

  */

  public String toString()

  {

    return getName()

           + "\n" + getAge() + " years old"

           + "\nID: " + getIdNumber();

  }

 

  //--- Attribute variables ---

  private String myName;

  private int myAge;

  private int myIdNumber;

}

 


(b)

/** HuntingLicense.java provides a class to model hunting licenses.

 */

 

import ann.easyio.*;

 

public class HuntingLicense extends License

{

 /** HuntingLicense constructor

  *  Receive:       int number, String name, int age, String prey

  *  Postcondition: A HuntingLicense object is constructed with

  *                 myName == name &&  myAge && age

  *                  && myIdNumber == number && myPrey == prey.

  */

  public HuntingLicense(String name, int age, int number, String prey)

  {

    super(name, age, number);

    myPrey = prey;

  }

 

 //--- Accessor ---

  public String getPrey()  { return myPrey; }

 

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a License

  */

  public String toString()

  {

    return super.toString() + "\nPrey: " + getPrey();

  }

 

  //--- Attribute variables ---

  private String myPrey;

}

 

 

24.

/** DeerHuntingLicense.java provides a class to model hunting deer licenses.

 */

 

import ann.easyio.*;

 

public class DeerHuntingLicense extends HuntingLicense

{

 /** DeerHuntingLicense constructor

  *  Receive:       int number, String name, int age, String prey,

  *                 boolean, doePermit

  *  Postcondition: A HuntingLicense object is constructed with

  *                 myName == name &&  myAge && age && myIdNumber == number

  *                  && myPrey == prey && myDoePermit == doePermit.

  */


 

 public DeerHuntingLicense(String name, int age, int number,

                            boolean doePermit)

  {

    super(name, age, number, "Deer");

    myDoePermit = doePermit;

  }

 

 //--- Accessor ---

  public boolean getDoePermit()  { return myDoePermit; }

 

 //--- Mutator ---

  public void setDoePermit(boolean doePermit) { myDoePermit = doePermit; }

 

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a DeerHuntingLicense

  */

  public String toString()

  {

    return super.toString()

           + " -- Doe hunting is" + (getDoePermit() ? " " : " not ")

           + " allowed";

  }

 

  //--- Attribute variables ---

  private boolean myDoePermit;

}

 

//-- Driver to test license hierarchy

import ann.easyio.*;

import License.*;

import HuntingLicense.*;

import DeerHuntingLicense.*;

 

class LicenseDriver

{

  public static void main(String [] args)

  {

     License l

          = new License("John Doe", 19, 12345);

    Screen theScreen = new Screen();

    theScreen.println(l + "\n");

 

    HuntingLicense h

          = new HuntingLicense("Mary Doe", 18, 54321, "Doves");

    theScreen.println(h + "\n");

 

     DeerHuntingLicense d

          = new DeerHuntingLicense("Joe Blow", 66, 66666, true);

    theScreen.println(d);

  }

}

 


Output

John Doe

19 years old

ID: 12345

 

Mary Doe

18 years old

ID: 54321

Prey: Doves

 

Joe Blow

66 years old

ID: 66666

Prey: Deer -- Doe hunting is  allowed

 

25.

(a)

/** BankAccount.java provides a class to model bank accounts.

 */

 

import ann.easyio.*;

 

public class BankAccount extends Object

{

 /** BankAccount constructor

  *  Receive:       String name, int number, double balance

  *  Postcondition: A BankAccount object is constructed with myName == name

  *                  && myAccountNumber == number && myBalance == balance.

  */

  public BankAccount (String name, int number, double balance)

  {

    myName = name;

    myAccountNumber = number;

    myBalance = balance;

  }

 

 //--- Accessors ---

  public String getName()       { return myName; }

  public int getAccountNumber() { return myAccountNumber; }

  public double getBalance()    { return myBalance; }

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a BankAccount

  */

  public String toString()

  {

    return "Account Number: " + getAccountNumber()

         + "    Balance: $" + getBalance();

  }

 

  //--- Attribute variables ---

  protected String myName;

  protected int myAccountNumber;

  protected double myBalance;

}

 

(b)

/** CheckingAccount.java provides a class to model checking accounts.

 */

 

import ann.easyio.*;

 

public class CheckingAccount extends BankAccount

{

 /** CheckingAccount constructor

  *  Receive:       String name, int number, double balance,

  *                 double serviceCharge

  *  Postcondition: A CheckingAccount object is constructed with

  *                 myName == name &&  myAccountNumber == number

  *                  && myBalance == balance

  *                  && myServiceCharge == serviceCharge.

  */

  public CheckingAccount(String name, int number, double balance,

                         double serviceCharge)

  {

    super(name, number, balance);

    myServiceCharge = serviceCharge;

  }

 

 //--- Accessor ---

  public double getServiceCharge()  { return myServiceCharge; }

 

 /** Deposit method ---

  *  Receive:       amount of a deposit

  *  Postcondition: myBalance is increased by amount

  */

  public void deposit(double amount)

  {

    myBalance += amount;

  }

 

 /** Withdrawal method ---

  *  Receive:       amount of a withdrawal

  *  Postcondition: myBalance is decreased by amount

  */

  public void withdraw(double amount)

  {

    myBalance -= amount;

  }

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a CheckingAccount

  */

  public String toString()

  {

    return super.toString() + "\nService Charge: $" + getServiceCharge();

  }

 

  //--- Attribute variables ---

  protected double myServiceCharge;

}

 

 

 

(c)

/** Loan.java provides a class to model loans.

 */

 

import ann.easyio.*;

 

public class Loan extends BankAccount

{

 /** Loan constructor

  *  Receive:       String name, int number, double balance,

  *                 double interestRate

  *  Postcondition: A Loan object is constructed with

  *                 myName == name &&  myAccountNumber == number

  *                  && myBalance == balance

  *                  && myInterestRate == interestRate.

  */

  public Loan(String name, int number, double balance,

                  double interestRate)

  {

    super(name, number, balance);

    myInterestRate = interestRate;

  }

 

 //--- Accessor ---

  public double getInterestRate()  { return myInterestRate; }

 

 

 /** Make-payment method ---

  *  Receive:       amount of a payment

  *  Postcondition: myBalance is decreased by amount

  */

  public void makePayment(double amount)

  {

    myBalance -= amount;

  }

 

 /** Add-interest method ---

  *  Postcondition: myBalance is increased by amount of interest

  */

  public void addInterest()

  {

    double interest = myBalance * myInterestRate;

    myBalance += interest;

  }

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a Loan

  */

  public String toString()

  {

    return super.toString() + "\nInterest Rate = " + getInterestRate();

  }

 

  //--- Attribute variables ---

  protected double myInterestRate;

}

 

26.

/** StudentLoan.java provides a class to model student licenses.

 */

 

import ann.easyio.*;

 

public class StudentLoan extends Loan

{

 /** StudentLoan constructor

  *  Receive:       String name, int number, double balance,

  *                 double payment, int numMonths

  *  Postcondition: A StudentLoan object is constructed with

  *                 myName == name &&  myAccountNumber == number

  *                  && myBalance == balance && myMonthlyPayment == payment

  *                  && myLoanTerm == numMonths

  *                  && myCurrentPaymentNumber == 0.

  */

 

 public StudentLoan (String name, int number, double balance,

                     double interestRate,  double payment,

                     int numMonths)

  {

    super(name, number, balance, interestRate);

    myMonthlyPayment = payment;

    myLoanTerm = numMonths;

    myCurrentPaymentNumber = 0;

  }

 

 //--- Accessor ---

  public int getLoanTerm()          { return myLoanTerm; }

  public double getMonthlyPayment() { return myMonthlyPayment; }

 

 /** Make-payment method ---

  *  Receive:       amount of a payment

  *  Postcondition: myBalance is decreased by amount and

  *                 myCurrentPaymentNumber is incremented by 1.

  */

  public void makePayment()

  {

    myBalance -= myMonthlyPayment;

    myCurrentPaymentNumber++;

  }

 

 /** Payments remaining method ---

  *  Return: number of payments remaining

  */

  public int paymentsLeft()

  {

    return myLoanTerm - myCurrentPaymentNumber;

  }

 

 //--- Output method ---

 /** toString converter

  *  Return: a String representation of a StudentLoan

  */

  public String toString()

  {

    return super.toString() + "\nLoanTerm = " + getLoanTerm()

           + " months\nMonthly Payment: $" + getMonthlyPayment();

  }

 

 //--- Attribute variables ---

  private int myLoanTerm,

              myCurrentPaymentNumber;

  private double myMonthlyPayment;

}

 

 

//-- Driver to test bank account hierarchy

import ann.easyio.*;

import BankAccount.*;

import CheckingAccount.*;

import Loan.*;

import StudentLoan.*;

 

class AccountDriver

{

  public static void main(String [] args)

  {

    BankAccount ba

          = new BankAccount("John Doe", 12345, 500.00);

    Screen theScreen = new Screen();

    theScreen.println(ba + "\n");

 

    CheckingAccount ca

          = new CheckingAccount("Mary Doe", 54321, 500.00, 5.00);

    theScreen.println(ca + "\n");

    ca.deposit(100.00);

    theScreen.println("Deposit $100:\n" + ca + "\n");

    ca.withdraw(50.00);

    theScreen.println("Withdraw $50:\n" + ca + "\n");

 

    Loan l = new Loan("Joe Blow", 66666, 600, .01);

    theScreen.println(l);

    l.makePayment(50.00);

    theScreen.println("Pay $50:\n" + l);

    l.addInterest();

    theScreen.println("Add interest:\n" + l + "\n");

 

    StudentLoan sl

          = new StudentLoan("Joe Blow", 66666, 600.00, .05, 25.00, 15);

    theScreen.println(sl);

    sl.makePayment();

    theScreen.println("MakePayment:\n" + sl);

    theScreen.println(sl.paymentsLeft() + " payments left.");

  }

}

 

Output:

 

Account Number: 12345    Balance: $500.0

 

Account Number: 54321    Balance: $500.0

Service Charge: $5.0

 

Deposit $100:

Account Number: 54321    Balance: $600.0

Service Charge: $5.0

 

Withdraw $50:

Account Number: 54321    Balance: $550.0

Service Charge: $5.0

 

Account Number: 66666    Balance: $600.0

Interest Rate = 0.01

Pay $50:

Account Number: 66666    Balance: $550.0

Interest Rate = 0.01

Add interest:

Account Number: 66666    Balance: $555.5

Interest Rate = 0.01

 

Account Number: 66666    Balance: $600.0

Interest Rate = 0.05

LoanTerm = 15 months

Monthly Payment: $25.0

MakePayment:

Account Number: 66666    Balance: $575.0

Interest Rate = 0.05

LoanTerm = 15 months

Monthly Payment: $25.0

14 payments left.