Inheritance Exercises

  < Previous  Next >
  1. In preparation for writing a Java program, you’re planning on having 3 classes: Employee, Cashier and Manager. Assume that cashiers and managers have access to the cash register (and that other employees do not). Additionally, managers have extra duties that cashiers do not (for example, locking up, extra paperwork, etc.). Given these assumptions, what is an appropriate inheritance hierarchy between the 3 classes (i.e., who inherits from who?)
    Optionally: Draw the UML diagram for these three classes.
  2. Brainstorm other examples of multiple classes that would fit into an inheritance hierarchy. Share them with your neighbor(s).
  3. Given the code below, add a Circle class that inherits from Shape and has a single private field (data member) double radius and a method void setRadius(double r) (which also sets area). Are there any changes that should be made to the Shape class? Execute ShapeDriver's main().
    /**
     * Defines a basic shape with just area
     *
     * @author Hyrum D. Carroll
     * @version 0.2 (10/05/2020)
     */
    public class Shape{
        protected double area;
    
        public Shape(){ area = 0.0; }
        public Shape( double a ){ this.area = a; }
        public void setArea( double a ){ area = a; }
        public double getArea(){ return area; }
        public String toString(){
            return "Shape:\n\tarea: " + area;
        }
    }
    
    /**
     * Create a simple Circle object
     *
     * @author Hyrum D. Carroll
     * @version 0.1 (March 20, 2019)
     */
    public class ShapeDriver{
        public static void main( String[] args ){
    	Circle cir = new Circle( );
    	cir.setRadius(5.0);
    	System.out.println(cir.toString());
        }
    }
    
  4. What's wrong with the following example? (Note: This came from a textbook.)
    public class PersonInfo {
       public String firstName;
       public String birthdate;
    
       ...
    }
    
    public class ChildInfo extends PersonInfo {
       public String schoolName;
    
       ...
    }
    
    public class MotherInfo extends PersonInfo {
       public String spousename;
       public ChildInfo children1;
       public ChildInfo children2;
       public ChildInfo children3;
      ...
    }
    
  5. Copy the following SimpleBankAccount class and use it as a base class:
    /**
     * Simple representation of a bank account
     *
     * @author Hyrum D. Carroll
     * @version 0.4 (February 21, 2020)
     */
    
    import java.text.NumberFormat;
    
    public class SimpleBankAccount{
        // field (instance variable)
        private double balance;
    
        /**
         * Constructor for objects of class SimpleBankAccount
         */
        public SimpleBankAccount(){
            balance = 0.0;
        }
    
        /**
         * Constructor for objects of class SimpleBankAccount
         */
        public SimpleBankAccount( double bal ){
            balance = bal;
        }
    
        /**
         * Add money to the balance
         *
         * @param  amount the amount to deposit
         * @return void
         */
        public void deposit( double amount ){
            balance += amount;
        }
    
        /**
         * Remove money from the balance
         *
         * @param  amount the amount to withdraw
         * @return true (success) or false (failure)
         */
        public boolean withdraw( double amount ){
            if( balance - amount >= 0 ){
                balance -= amount;
                return true;
            }else{
                return false;
            }
        }
    
        /**
         * Get the balance
         *
         * @return the balance
         */
        public double getBalance(){
             return balance;
        }
    
        /**
         * Produces a string representation of the balance
         * @return The balance (with a label)
         */
        public String toString( ){
            // display balance as currency
            String balanceStr = NumberFormat.getCurrencyInstance().format( balance );
            return "Balance: " + balanceStr + "\n";
        }
    }
    
    Include at least two classes: CheckingAccount and SavingsAccount. The CheckingAccount needs to add a field to track the last process check number and the following method:
    public boolean processCheck( int checkNum, double amount );
    which returns false if checkNum has the same check number as the last check processed, otherwise it reduces the balance by amount and returns true. Additionally, the SavingsAccount needs to have an interest rate and an applyInterest() method.
    The following code should work and produce the output below:
    /** 
     * Exercises the basic functionality of a Checking and SavingsAccount
     *
     * @author Hyrum D. Carroll
     * @version 0.2 (February 21, 2020)
     */
    public class AccountsDriver{
        final public static double INTEREST_RATE = 0.01;  // 1%
    
        public static void main( String[] args ){
            CheckingAccount checking = new CheckingAccount( 100.0 );
            SavingsAccount savings = new SavingsAccount( 1000.0, INTEREST_RATE );
    
            double monthlyExpenses = 756.34;
            int electricBillCheckNum = 2123;
            double electricBill = 60.34;
            int registationCheckNum = 2124;
            double registration = 50.00;
            double dinnerMoney = 55.32;
            double futureCar = 200.0;
            double textbook = 90.0;
    
            // checking account transactions
            checking.deposit( monthlyExpenses );
            checking.processCheck( electricBillCheckNum, electricBill );
            checking.withdraw( dinnerMoney );
            checking.processCheck( registationCheckNum, registration );
            System.out.print( checking.toString() );
    
            // savings account transactions
            savings.deposit( futureCar );
            savings.applyInterest( );
            savings.withdraw( textbook );
            System.out.print( savings.toString() );
        }
    }
    Output:
    Checking Account:
    Balance: $690.68
    Last processed check: 2124
    Savings Account:
    Balance: $1,122.00
    APR: 1.0%
    Make just the necessary changes to the code in SimpleBankAccount and AccountsDriver to complete the instructions.