Homework 5

Due Monday August 8, 5:30pm


Homework 5 should be turned in as a printout of your program before class/lab begins on Thursday. Your program is still expected to run in DrScheme: ProfessorJ Intermediate.
Printout should contain your first and last name, as well as the text Hw 5

  1. Run the following classes in the Definitions window:
    abstract class  Furniture {
      //To report the price of this Furniture
      abstract double price();
    }

    class Chair extends Furniture {

      boolean hasWheels;
      String covering;

      Chair( boolean hasWheels, String covering ) {
         this.hasWheels = hasWheels;
         this.covering = covering;
      }

      //new Chair(true, "leather").price() -> 105.95
      //new Chair(false, "leather").price() -> 88.95
      //new Chair(true, "tweed").price() -> 49.95
      //To report the price of this Chair
      double price() {
        // ... this.hasWheels ... this.covering ...
        if (this.hasWheels && this.covering.equals("leather") )
            return 105.95;
        else if (this.covering.equals("leather") )
            return 88.95;
        else
            return 49.95;
       }
    }

    abstract class List {
    }

    class Empty extends List {
    }

    class Larger extends List {
      Object first;
      List rest;

      Larger( Object first, List rest ) {
        this.first = first;
        this.rest = rest;
      }
    }
     
     Consider the following items. Explain what you expect will happen when entering them into the Interactions window. (The only incorrect answer here is no answer, so there is no benefit in trying them out first). Now enter them into the Interactions window. Describe and explain what occurs.
    1. new Chair(false,"velvet").price()
    2. Furniture f = new Chair(false, "satin");
    3. f.price()
    4. f.hasWheels
    5. new Larger(f,new Empty()).first
    6. List myL = new Larger(f, new Empty());
    7. myL.first
    8. new Larger( myL, myL )
  2. Add a method to sort the following list so that the lowest paid Employee is first, using insertion sort.

    abstract class EmployeeL {
    }

    class EmptyEL extends EmployeeL {
       EmptyEL() { }
    }

    class LargerEL extends EmployeeL {
      Employee first;
      EmployeeL rest;

      LargerEL( Employee first, EmployeeL rest ) {
        this.first = first;
        this.rest = rest;
      }
    }

    abstract class  Employee  {
      abstract double salary();
    }

    class Temp extends Employee {
       int hours;
       double rate;
      
       Temp( int hours, double rate) {
          this.hours = hours;
          this.rate = rate;
        }

        double salary() {
           return this.rate * this.hours;
        }
    }
     
  3. Modify the Fish class so that it implements the Totalable interface from class
  4. Challenge Modify ListT so that it implements the Totalable interface.
  5. Challenge Consider the following data definition
    /*
                          +------------+                        
                          | Expression |<-----------------------+
                          +------------+                        |
                          +------------+                        |
                               / \                              |
                               ---                              |
                                |                               |
           ---------------------------------------              |
           |                |                    |              |
      +---------+    +-------------+    +------------------+    |
      | Num     |    | Variable    |    | Operation        |    |
      +---------+    +-------------+    +------------------+    |
      | int val |    | String name |    | String op        |    |
      +---------+    +-------------+    | Expression left  |-+  |
                                        | Expression right |-+  |
                                        +------------------+ |  |
                                                             |  |
                                                             +--+

    */
    Develop a method to create a list of all variables used in an Expression