Category Archives: Class work

Chapter 7: Inheritance – Method Behavior

Inheritance
Method behavior

Base and Derived classes are simple classes with one constructor and two methods

Parent class

public class Base
{
    public Base()
    {
        System.out.println("Constructor from Base class ");
    }
    public void methodOne()
    {
        System.out.println("methodOne from Base class");
    }
    public void methodTwo()
    {
        System.out.println("methodTwo from Base class");
    }
    
} 

Child class

public class Derived extends Base
{
    public Derived()
    {
        System.out.println("Constructor from Derived class " );
    }
    public void methodOne()
    {
        System.out.println("methodOne from Derived class");      
    }
    public void methodThree()
    {
        System.out.println("methodThree from Derived class");
    }    
}

    

Driver/Test class

public class BaseDerivedTest
{
    public static void main(String [] args)
    {
        Base b = new Derived();
        b.methodOne();
        b.methodTwo();
        //b.methodThree(); ERROR
        System.out.println("*** Derived ****");
        Derived c = (Derived)b;
        c.methodThree();
        Derived d = new Derived();
        d.methodOne(); 
        d.methodTwo();
        d.methodThree();
    }
}

Include these three classes in your project. Run the driver.

Study the code and the output from the driver class. Use the output to make conclusions based on the following:
1. Do you see a pattern in the order in which the constructor and methods are called? Clearly explain
2. What criteria does the order depend on?
3. Explain how late binding, Polymorphism has a role in the behavior.
Note: Include the output in your submission.

In preparation for the test on Monday work on these assignments:
Self-Review 7.1 through 7.8

Chapter 7: Polymorphism and Inheritance: Pizza, Staff and Hospital

Polymorphism and Inheritance

Indirect use of class members
There is a subtle feature of inheritance that is worth noting at this point. The visibility modifiers determine whether a variable or method is inherited into a subclass. If a variable or method is inherited, it can be referenced directly in the subclass by name, as if it were declared locally in the subclass. However, all variables and methods that are defined in a parent class exist for an object of a derived class, even though they can’t be referenced directly. They can, however, be referenced indirectly.
Let’s look at an example that demonstrates this situation. The program shown in Listing 7.17 contains a main method that instantiates a Pizza object and invokes a method to determine how many calories the pizza has per serving due to its fat content.

//********************************************************************
   //  FoodAnalysis.java       Author: Lewis/Loftus
   //
   //  Demonstrates indirect referencing through inheritance.
   //********************************************************************
   public class FoodAnalysis

The FoodItem class shown in Listing 7.18 represents a generic type of food. The constructor of FoodItem accepts the number of grams of fat and the number of servings of that food. The calories method returns the number of calories due to fat, which the caloriesPerServing method invokes to help compute the number of fat calories per serving.

//********************************************************************
//  FoodItem.java       Author: Lewis/Loftus
//
//  Represents an item of food. Used as the parent of a derived class
//  to demonstrate indirect referencing through inheritance.
//********************************************************************
public class FoodItem

The Pizza class, shown in Listing 7.19, is derived from FoodItem class, but it adds no special functionality or data. Its constructor calls the constructor of FoodItem, using the super reference assuming that there are eight servings per pizza.
Note that the Pizza object called special in the main method is used to invoke the method caloriesPerServing, which is defined as a public method of FoodItem and is therefore inherited by Pizza. However, caloriesPerServing calls calories, which is declared private, and is therefore not inherited by Pizza. Furthermore, calories references the variable fatGrams and the constant CALORIES_PER_GRAM, which are also declared with private visibility.
Even though Pizza did not inherit calories, fatGrams, or CALORIES_PER_GRAM, they are available for use indirectly when the Pizza object needs them. The Pizza class cannot refer to them directly by name because they are not inherited, but they do exist. Note that a FoodItem object was never created or needed.

//********************************************************************
  //  Pizza.java       Author: Lewis/Loftus
  //
  //  Represents a pizza, which is a food item. Used to demonstrate
  //  indirect referencing through inheritance.
  //********************************************************************
  public class Pizza extends FoodItem
  {
     //-----------------------------------------------------------------
     //  Sets up a pizza with the specified amount of fat (assumes
     //  eight servings).
     //-----------------------------------------------------------------
     public Pizza (int fatGrams)
     {
        super (fatGrams, 8);
}
}

*** Answer questions about each variable and method declared in the FoodItem class. Indicate whether it exists in or is inherited by the Pizza class. Note that every FoodItem member exists in the Pizza class, no matter how it is declared. The items that are not inherited can be referenced only indirectly.

polymorphism

Usually, the type of a reference variable matches exactly the class of the object to which it refers. That is, if we declare a reference as follows, the bishop reference is used to refer to an object created by instantiating the ChessPiece class.

ChessPiece bishop;

However, the relationship between a reference variable and the object it refers to is more flexible than that.
The term polymorphism can be defined as “having many forms.” A polymorphic reference is a reference variable that can refer to different types of objects at different points in time. The specific method invoked through a polymorphic reference can change from one invocation to the next.
Consider the following line of code:

obj.doIt();

If the reference obj is polymorphic, it can refer to different types of objects at different times. If that line of code is in a loop or in a method that is called more than once, that line of code might call a different version of the doIt method each time it is invoked.
At some point, the commitment is made to execute certain code to carry out a method invocation. This commitment is referred to as binding a method invocation to a method definition. In most situations, the binding of a method invocation to a method definition can occur at compile time. For polymorphic references, however, the decision cannot be made until run time. The method definition that is used is based on the object that is being referred to by the reference variable at that moment. This deferred commitment is called late binding or dynamic bind- ing. It is less efficient than binding at compile time because the decision must be made during the execution of the program. This overhead is generally acceptable in light of the flexibility that a polymorphic reference provides.
We can create a polymorphic reference in Java in two ways: using inheritance and using interfaces. This section describes how we can accomplish polymorphism using inheritance. Later in the chapter we revisit the issue of interfaces and describe how polymorphism can be accomplished using interfaces as well.

In Java, a reference that is declared to refer to an object of a particular class can also be used to refer to an object of any class related to it by inheritance. For example, if the class Mammal is used to derive the class Horse, then a Mammal reference can be used to refer to an object of class Horse. This ability is
shown in the following code segment:
Mammal pet;
Horse secretariat = new Horse();
pet = secretariat; // a valid assignment

The reverse operation, assigning the Mammal object to a Horse reference, is also valid but requires an explicit cast. Assigning a reference in this direction is generally less useful and more likely to cause problems because although a horse has all the functionality of a mammal (because a horse “is-a” mammal), the reverse is not necessarily true.
This relationship works throughout a class hierarchy. If the Mammal class were derived from a class called Animal, the following assignment would also be valid:

Animal creature = new Horse();

Carrying this to the limit, an Object reference can be used to refer to any object because ultimately all classes are descendants of the Object class. An ArrayList, for example, uses polymorphism in that it is designed to hold Object references. That’s why an ArrayList can be used to store any kind of object. In fact, a particular ArrayList can be used to hold several different types of objects at one time because, in essence, they are all Object objects.

The reference variable creature, as defined in the previous section, can be polymorphic because at any point in time it can refer to an Animal object, a Mammal object, or a Horse object. Suppose that all three of these classes have a method called move that is implemented in different ways (because the child class overrode the definition it inherited). The following invocation calls the move method, but the particular version of the method it calls is determined at runtime:
creature.move();
At the point when this line is executed, if creature currently refers to an Animal object, the move method of the Animal class is invoked. Likewise, if creature currently refers to a Mammal or Horse object, the Mammal or Horse version of move is invoked, respectively.
Of course, since Animal and Mammal represent general concepts, they may be defined as abstract classes. This situation does not eliminate the ability to have polymorphic references. Suppose the move method in the Mammal class is abstract, and is given unique definitions in the Horse, Dog, and Whale classes (all derived from Mammal). A Mammal reference variable can be used to refer to any objects created from any of the Horse, Dog, and Whale classes, and
can be used to execute the move method on any of them.
Let’s look at another situation. Consider the class hierarchy shown below. The classes in it represent various types of employees that might be employed at a particular company. Let’s explore an example that uses this hierarchy to demonstrate several inheritance issues, including polymorphism.
The Firm class shown in Listing 7.16 contains a main driver that creates a Staff of employees and invokes the payday method to pay them all. The program output includes information about each employee and how much each is paid (if anything).

PolymorphismInheritance

ClassesDependencies

The classes in this UML represent various types of staff members in a company.

The Hospital class is a main driver that creates a Staff of employees and invokes the payday method to pay them all.

The program outputs information about the staff and how much each is paid.

The Staff class maintains an array of objects that represents the staff in a hospital.

The array is declared to hold StaffCrew references, but it is actually filled with objects created from several other classes, such as Supervisor and FullTimeStaff.

These classes are all descendants of the StaffMember class, so the assignments are valid. The staffList array is filled with polymorphic references.

The payday method of the Staff class traverses the list of employees, printing their information and calling their pay methods to determine how much each employee should be paid.

The invocation of the pay method is polymorphic, because each class has its own version of the pay method.

The StaffCrew class is abstract. It is not designed to be instantiated or to be any of the members of the staff. It is designed to be a parent of all staff member classes. Each staff has a name, address, and phone number, so variables to store these values are declared in the StaffCrew class and are inherited by all descendants.

The StaffCrew class contains a toString method to return the information managed by the StaffCrew class. It also contains an abstract method called basicPay, which takes no parameters and returns a value of type double. It would not make sense to define this method in this class since it is different for every staff member.

The children classes provide the definition for basicPay so they are dynamically selected at run time. This is called polymorphism. It lets us treat similar objects in consistent but unique ways.

//********************************************************************
//  Demonstrates polymorphism and inheritance.
//********************************************************************

public class Hospital
{
   //-----------------------------------------------------------------
   //  Creates a staff for a hospital and pays them.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Staff personnel = new Staff();

      personnel.payday();
   }
}

//********************************************************************
//  Represents the personnel staff of a particular hospital.
//********************************************************************

public class Staff
{
   private StaffCrew[] staffList;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the list of staff members.
   //-----------------------------------------------------------------
   public Staff ()
   {
      staffList = new StaffCrew[6];

      staffList[0] = new Supervisor ("Mary", "111 ABC Rd",
         "555-4444", "123456789", 2000.00);

      staffList[1] = new FullTimeStaff ("John", "222 DEF Rd",
         "555-5555", "234567891", 1000.00);
      staffList[2] = new FullTimeStaff ("Alice", "333 GHI Rd",
         "555-3333", "345678912", 1100.00);

      staffList[3] = new PartTime ("Bob", "444 JKL Rd",
         "555-2222", "456789123", 12.00);

      staffList[4] = new Volunteer ("Tom", "555 MNO Rd",
         "555-1111");
      staffList[5] = new Volunteer ("Jill", "666 PQR Rd",
         "555-7282");

      ((Supervisor)staffList[0]).grantBonus (300.00);

      ((PartTime)staffList[3]).addHours (25);
   }

   //-----------------------------------------------------------------
   //  Pays all staff members.
   //-----------------------------------------------------------------
   public void payday ()
   {
      double amount;

      for (int count=0; count < staffList.length; count++)
      {
         System.out.println (staffList[count]);

         amount = staffList[count].basicPay();  // polymorphic

         if (amount == 0.0)
            System.out.println ("Thanks!");
         else
            System.out.println ("Paid: " + amount);

         System.out.println ("-----------------------------------");
      }
   }
}

//********************************************************************
//  Represents any crew member.
//********************************************************************

abstract public class StaffCrew
{
   protected String name;
   protected String address;
   protected String phone;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this staff crew using the specified
   //  information.
   //-----------------------------------------------------------------
   public StaffCrew (String eName, String eAddress, String ePhone)
   {
      name = eName;
      address = eAddress;
      phone = ePhone;
   }

   //-----------------------------------------------------------------
   //  Returns a string including the basic full time staff information.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = "Name: " + name + "\n";

      result += "Address: " + address + "\n";
      result += "Phone: " + phone;

      return result;
   }

   //-----------------------------------------------------------------
   //  Derived classes must define the pay method for each type of
   //  full time staff.
   //-----------------------------------------------------------------
   public abstract double basicPay();
}

//********************************************************************
//  Represents a staff member that works as a volunteer.
//********************************************************************

public class Volunteer extends StaffCrew
{
   //-----------------------------------------------------------------
   //  Constructor: Sets up this volunteer using the specified
   //  information.
   //-----------------------------------------------------------------
   public Volunteer (String eName, String eAddress, String ePhone)
   {
      super (eName, eAddress, ePhone);
   }

   //-----------------------------------------------------------------
   //  Returns a zero pay value for this volunteer.
   //-----------------------------------------------------------------
   public double basicPay()
   {
      return 0.0;
   }
}

//********************************************************************
//  Represents a general paid full time staff.
//********************************************************************

public class FullTimeStaff extends StaffCrew
{
   protected String IDNumber;
   protected double payRate;
   
   //-----------------------------------------------------------------
   //  Constructor: Sets up this full time staff with the specified
   //  information.
   //-----------------------------------------------------------------
   public FullTimeStaff (String eName, String eAddress, String ePhone,
                    String eIDNumber, double rate)
   {
      super (eName, eAddress, ePhone);

      IDNumber = anIDNumber;
      payRate = rate;
   }

   //-----------------------------------------------------------------
   //  Returns information about a full-time staff as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = super.toString();

      result += "\nID Number: " + IDNumber;

      return result;
   }

   //-----------------------------------------------------------------
   //  Returns the pay rate for this full-time staff.
   //-----------------------------------------------------------------
   public double basicPay()
   {
      return payRate;
   }
}

//********************************************************************
//  Represents an Supervisor staff member, who can earn a bonus.
//********************************************************************

public class Supervisor extends FullTimeStaff
{
   private double bonus;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this supervisor with the specified
   //  information.
   //-----------------------------------------------------------------
   public Supervisor (String eName, String eAddress, String ePhone,
                     String eIDNumber, double rate)
   {
      super (eName, eAddress, ePhone, eIDNumber, rate);

      bonus = 0;  // bonus has yet to be awarded
   }

   //-----------------------------------------------------------------
   //  Grants the specified bonus to this supervisor.
   //-----------------------------------------------------------------
   public void grantBonus (double execBonus)
   {
      bonus = execBonus;
   }

   //-----------------------------------------------------------------
   //  Computes and returns the pay for a supervisor, which is the
   //  regular full-time staff payment plus a one-time bonus.
   //-----------------------------------------------------------------
   public double basicPay()
   {
      double payment = super.basicPay() + bonus;

      bonus = 0;

      return payment;
   }
}

//********************************************************************
//  Represents a full time staff that gets paid by the hour.
//********************************************************************

public class PartTime extends FullTimeStaff
{
   private int hoursWorked;

   //-----------------------------------------------------------------
   //  Constructor: Sets up this part time staff using the specified
   //  information.
   //-----------------------------------------------------------------
   public PartTime (String eName, String eAddress, String ePhone,
                  String eIDNumber, double rate)
   {
      super (eName, eAddress, ePhone, eIDNumber, rate);

      hoursWorked = 0;
   }

   //-----------------------------------------------------------------
   //  Adds the specified number of hours to this staff's
   //  accumulated hours.
   //-----------------------------------------------------------------
   public void addHours (int moreHours)
   {
      hoursWorked += moreHours;
   }

   //-----------------------------------------------------------------
   //  Computes and returns the pay for this part time staff.
   //-----------------------------------------------------------------
   public double basicPay()
   {
      double payment = payRate * hoursWorked;

      hoursWorked = 0;

      return payment;
   }

   //-----------------------------------------------------------------
   //  Returns information about this part time staff as a string.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = super.toString();

      result += "\nCurrent hours: " + hoursWorked;

      return result;
   }
}

The Staff class shown in Listing 7.17 maintains an array of objects that represent individual employees of various kinds. Note that the array is declared to hold StaffMember references, but it is actually filled with objects created from several other classes, such as Executive and Employee. These classes are all descendants of the StaffMember class, so the assignments are valid.
The payday method of the Staff class scans through the list of employees, printing their information and invoking their “pay” methods to determine how much each employee should be paid. The invocation of the “pay” method is polymorphic because each class has its own version of the “pay” method.
The StaffMember class shown in Listing 7.18 is abstract. It does not represent a particular type of employee and is not intended to be instantiated. Rather, it serves as the ancestor of all employee classes and contains information that applies to all employees. Each employee has a name, address, and phone number, so variables to store these values are declared in the StaffMember class and are inherited by all descendants.
The StaffMember class contains a toString method to return the information managed by the StaffMember class. It also contains an abstract method called pay, which takes no parameters and returns a value of type double. At the generic StaffMember level, it would be inappropriate to give a definition for this method. The descendants of StaffMember, however, each provide their own specific definition for pay. By defining pay abstractly in StaffMember, the payday method of Staff can polymorphically pay each employee.
The Volunteer class shown in Listing 7.19 represents a person that is not compensated monetarily for his or her work. We keep track only of a volunteer’s basic information, which is passed into the constructor of Volunteer, which in turn passes it to the StaffMember constructor using the super reference. The “pay” method of Volunteer simply returns a zero pay value. If pay had not been overridden, the Volunteer class would have been considered abstract and could not have been instantiated.
Note that when a volunteer gets “paid” in the payday method of Staff, a simple expression of thanks is printed. In all other situations, where the “pay” value is greater than zero, the payment itself is printed.
The Employee class shown in Listing 7.20 represents an employee that gets paid at a particular rate each pay period. The pay rate, as well as the employee’s social security number, is passed along with the other basic information to the Employee constructor. The basic information is passed to the constructor of StaffMember using the super reference.

Related questions:
1. In what specific cases does inheritance support polymorphism? You can copy and paste the code.
2. How is overriding related to polymorphism?
3. Why is the StaffCrew class in the Hospital example declared as abstract?
4. Why is the pay method declared in the StaffCrew class, given that it is abstract and has nobody at that level?
5. Which “pay” method is invoked by the following line from the payday method of the Staff class?
amount = staffList[count].pay();

Chapter 7: Designing for Inheritance and Interfaces

January 13th, 2017

Interface Hierarchies

  • The concept of inheritance can be applied to interfaces as well as classes.
  • One interface can be derived from another interface.
  • These relationships can form an interface hierarchy, which is similar to a class hierarchy.
  • When a parent interface is used to derive a child interface, the child inherits all abstract methods and constants of the parent.
  • Any class that implements the child interface must implement all of the methods.
  • There are no visibility issues when dealing with inheritance between interfaces (as there are with protected and private members of a class), because all members of an interface are public.
  • Class hierarchies and interface hierarchies do not overlap. That is, an interface cannot be used to derive a class, and a class cannot be used to derive an interface.

Designing for inheritance
▪ Every derivation should be an is-a relationship. The child should be a more specific version of the parent.
▪ Design a class hierarchy to capitalize on reuse, and potential reuse in the future.
▪ As classes and objects are identified in the problem domain, find their commonality. Push common features as high in the class hierarchy as appropriate for consistency and ease of maintenance.
▪ Override methods as appropriate to tailor or change the functionality of a child.
▪ Add new variables to the child class as needed, but don’t shadow (redefine) any inherited variables.
▪ Allow each class to manage its own data. Therefore use the super reference to invoke a parent’s constructor and to call overridden versions of methods if appropriate.
▪ Use interfaces to create a class that serves multiple roles (simulating multiple inheritance).
▪ Design a class hierarchy to fit the needs of the application, with attention to how it may be useful in the future.
▪ Even if there are no current uses for them, override general methods such as toString and equals appropriately in child classes so that the inherited versions don’t cause unintentional problems later.
▪ Use abstract classes to specify a common class interface for the concrete classes lower in the hierarchy.
▪ Choosing which classes and methods to make abstract is an important part of the design process. You should make such choices only after careful consideration. By using abstract classes wisely, you can create flexible, extensible software designs.
▪ Use visibility modifiers carefully to provide the needed access in derived classes without violating encapsulation.
▪ Using the final modifier to restrict inheritance abilities is a key design decision.

Chapter 7: Pet Shop v1 Questions

Pet Shop v1

Answer these questions:
1. What is the relationship between the Animal class and the Bird class?
2. Why doesn’t the java compiler give an error here: Animal tweety = new Bird(45.00);?
3. Can an Animal object “sing”? Explain
4. What collection is used in the PetSet class?
5. Why is the method whatIam() necessary in the Animal class?
6. Which whatIam() method is called in the PetShop class?
7. What statement shows late binding? Identify the line(s) of code where this takes place in your Pet Shop application.
8. What statement shows early binding? Give an example from your Pet Shop application.
9. What is the difference between the two bindings? Read below to help you answer this question.

Chapter 7: Inheritance – Student

Inheritance – Student Exercise
student
Use this implementation of Student and StudentAthlete ADTs to show inheritance concepts.

  1. Describe what the Student ADT looks like:
    A good description should include the name of the instance fields, whether the ADT has constructor(s), the methods’ purpose, and names.
  2. public class Student
    {
       private String name;
       private int numCourses;
    
       //-----------------------------------------------------------------
       //  Sets up a student with the specified name and number of
       //  courses.
       //-----------------------------------------------------------------
       public Student (String studentName, int courses)
       {
          name = studentName;
          numCourses = courses;
       }
    
       //-----------------------------------------------------------------
       //  Returns information about this student as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = "Student name: " + name + "\n";
    
          result += "Number of courses: " + numCourses;
    
          return result;
       }
    }
    
  3. Describe what the StudentAthlete ADT looks like:
    A good description should include the name of the instance fields, whether the ADT has constructor(s), the methods’s purpose, and names.
    Mostly clearly identify attributes, any overridden methods, any methods that belong to the child class. As a final sentence describe the relationship between Student and StudentAthlete.
  4. //********************************************************************
    //  StudentAthlete.java       Author: Lewis/Loftus/Cocking
    //
    //  Represents a student athlete who plays a sports team for the school.
    //  Used to demonstrate inheritance.
    //********************************************************************
    
    public class StudentAthlete extends Student
    {
       private String sport;
    
       //-----------------------------------------------------------------
       //  Sets up the student athlete using the specified information.
       //-----------------------------------------------------------------
       public StudentAthlete (String studentName, int courses,
                           String sportName)
       {
          super (studentName, courses);
    
          sport = sportName;
       }
    
       //-----------------------------------------------------------------
       //  Returns a description of this graduate student as a string.
       //-----------------------------------------------------------------
       public String toString()
       {
          String result = super.toString();
    
          result += "\nSport: " + sport;
    
          return result;
       }
    }
    
    
    
  5. Describe the Academia driver class.
  6. //********************************************************************
    //  Academia.java       Author: Lewis/Loftus/Cocking
    //
    //  Demonstrates the use of methods inherited from the Object class.
    //********************************************************************
    
    public class Academia
    {
       //-----------------------------------------------------------------
       //  Creates objects of two student types, prints some information
       //  about them, then checks them for equality.
       //-----------------------------------------------------------------
       public static void main (String[] args)
       {
          Student Frank = new Student ("Frank", 5);
          StudentAthlete Suki = new StudentAthlete ("Suki", 4, "Soccer");
    
          System.out.println (Frank);
          System.out.println ();
    
          System.out.println (Suki);
          System.out.println ();
    
          if (! Frank.equals(Suki))
             System.out.println ("These are two different students.");
       }
    }
    
    
    
  7. Add accessor and mutator methods for the studentName and sportName. NOTE: do not change any visibility specifier.
  8. In the Academia driver class, include calls to the methods you just implemented.

All source: Java Software Solutions by Lewis, Loftus and Cocking

Chapter 7: Inheritance – Outline of Concepts

Inheritance
Notes

  • Inheritance is the process of deriving a new class from an existing one.
  • One purpose of inheritance is to reuse existing software.
  • The original class is called the parent class, superclass, or base class.
  • Inheritance creates an “is-a” relationship between the parent and child classes.
  • Java uses the reserved word extends to indicate that a new class is being derived from an existing class.
  • Inheritance is a one-way street.

An example:

Screen Shot 2014-01-21 at 12.40.23 AM

A UML class diagram showing an inheritance relationship

public class Words
{
   //-----------------------------------------------------------------
   //  Instantiates a derived class and invokes its inherited and
   //  local methods.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Dictionary webster = new Dictionary();

      System.out.println ("Number of pages: " + webster.getPages());

      System.out.println ("Number of definitions: " +
                          webster.getDefinitions());

      System.out.println ("Definitions per page: " +
                          webster.computeRatio());
   }
}

public class Book
{
   protected int pages = 1500;

   //----------------------------------------------------------------
   //  Pages mutator.
   //----------------------------------------------------------------
   public void setPages (int numPages)
   {
      pages = numPages;
   }

   //----------------------------------------------------------------
   //  Pages accessor.
   //----------------------------------------------------------------
   public int getPages ()
   {
      return pages;
   }
}
public class Dictionary extends Book
{
   private int definitions = 52500;

   //-----------------------------------------------------------------
   //  Prints a message using both local and inherited values.
   //-----------------------------------------------------------------
   public double computeRatio ()
   {
      return (double) definitions/pages;
   }

   //----------------------------------------------------------------
   //  Definitions mutator.
   //----------------------------------------------------------------
   public void setDefinitions (int numDefinitions)
   {
      definitions = numDefinitions;
   }

   //----------------------------------------------------------------
   //  Definitions accessor.
   //----------------------------------------------------------------
   public int getDefinitions ()
   {
      return definitions;
   }
}

Visibility Modifiers

  • Private methods and variables of the parent class cannot be referenced in the child class or through an object of the child class.
  • Public visibility allows a derived class to reference it, but violates the principle of encapsulation.
  • Protected visibility allows the class to retain some encapsulation properties.
  • Protected visibility provides the best possible encapsulation that permits inheritance. However, the encapsulation is not as tight as if the variable or method were declared private, but it is better than if it were declared public.
  • Modifiers list:
    • public – visible anywhere
    • protected – can only be applied to inner classes. Visible to any classes in the package and to children classes.
    • private – can only be applied to inner classes. Visible enclosing class only.
    • no modifier – default – visible to any classes in the same package.
  • For the AP Computer Science A Exam a design question may required a student to develop a solution that includes all data declared private.

The Super keyword

  • The reserved word super can be used in a class to refer to its parent class.
  • Using the super reference, we can access a parent’s members.
  • Like the this reference, what the word super refers to depends on the class in which it is used.
  • A parent’s constructor can be invoked using the super reference. ( First line in the constructor )
  • If no such call exists, Java will automatically make a call to super() at the beginning of the constructor.
  • The super reference can also be used to reference other variables and methods defined in the parent’s class.
  • The visibility modifiers determine whether a variable or method is inherited into a subclass. If a variable or method is inherited, it can be referenced directly in the child class by name, as if it were declared locally in the child class.
  • However, all variables and methods in a parent class exist for an object of a child class, even though they can’t be referenced directly. They can, however, be referenced indirectly.
  • All members of a superclass exist for a subclass, but they are not necessarily inherited. Only inherited members can be referenced by name in the subclass.
public class Words2
{
   //-----------------------------------------------------------------
   //  Instantiates a derived class and invokes its inherited and
   //  local methods.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Dictionary2 webster = new Dictionary2 (1500, 52500);

      System.out.println ("Number of pages: " + webster.getPages());

      System.out.println ("Number of definitions: " +
                          webster.getDefinitions());

      System.out.println ("Definitions per page: " +
                          webster.computeRatio());
   }
}


public class Book2
{
   protected int pages;

   //----------------------------------------------------------------
   //  Constructor: Sets up the book with the specified number of
   //  pages.
   //----------------------------------------------------------------
   public Book2 (int numPages)
   {
      pages = numPages;
   }

   //----------------------------------------------------------------
   //  Pages mutator.
   //----------------------------------------------------------------
   public void setPages (int numPages)
   {
      pages = numPages;
   }

   //----------------------------------------------------------------
   //  Pages accessor.
   //----------------------------------------------------------------
   public int getPages ()
   {
      return pages;
   }
}


public class Dictionary2 extends Book2
{
   private int definitions;

   //-----------------------------------------------------------------
   //  Constructor: Sets up the dictionary with the specified number
   //  of pages and definitions.
   //-----------------------------------------------------------------
   public Dictionary2 (int numPages, int numDefinitions)
   {
      super(numPages);

      definitions = numDefinitions;
   }

   //-----------------------------------------------------------------
   //  Prints a message using both local and inherited values.
   //-----------------------------------------------------------------
   public double computeRatio ()
   {
      return (double) definitions/pages;
   }

   //----------------------------------------------------------------
   //  Definitions mutator.
   //----------------------------------------------------------------
   public void setDefinitions (int numDefinitions)
   {
      definitions = numDefinitions;
   }

   //----------------------------------------------------------------
   //  Definitions accessor.
   //----------------------------------------------------------------
   public int getDefinitions ()
   {
      return definitions;
   }
}



Overriding Notes

  • A child class can override (redefine) the parent’s definition of an inherited 
method.
  • Constructors, however, are not inherited and cannot be overridden.
  • Method overriding is a key element in object-oriented design.
  • Method overriding allows two objects that are related by inheritance to use the same naming conventions for methods that accomplish the same general task in different ways.
  • A method can be defined with the final modifier. A child class cannot override a final method. This technique is used to ensure that a derived class uses a particular definition of a method.
  • The final modifier can also be applied to an entire class. A final class cannot be extended at all.

Shadowing

  • If a variable of the same name is declared in a child class, it is called a shadow variable.
  • To avoid confusion and logical errors, shadowing variables should be avoided.

Class Hierarchy

Screen Shot 2014-01-21 at 12.40.50 AM

A UML class diagram showing a class hierarchy

  • The child of one class can be the parent of one or more other classes, creating a class hierarchy.
  • Common features should be located as high in a class hierarchy as is reasonably possible.
  • All Java classes are derived, directly or indirectly, from the Object class.
  • The toString and equals methods are inherited by every class in every 
Java program.
  • Inheritance can be applied to interfaces so that one interface can be derived from another.
  • Private members are inherited by the child class, but cannot be referenced directly by name. They may be used indirectly, however.
  • Java’s approach to inheritance is called single inheritance.

Abstract Classes

  • It represents a concept on which other classes can build their definitions.An abstract class cannot be instantiated.
  • A class derived from an abstract parent must override all of its parent’s abstract methods, or the derived class will also be considered abstract.
  • An abstract class is similar to an interface in some ways.
  • However, unlike interfaces, an abstract class can contain methods that are not abstract.
  • An abstract class can also contain data declarations other than constants.
  • A class is declared as abstract by including the abstract modifier in the class header.
  • Any class that contains one or more abstract methods must be declared as abstract.
  • In abstract classes (unlike interfaces), the abstract modifier must be applied to each abstract method.
  • A class declared as abstract does not have to contain abstract methods.
  • Abstract classes serve as placeholders in a class hierarchy.
  • If a child of an abstract class does not give a definition for every abstract method that it inherits from its parent, then the child class is also considered abstract.
  • Note that it would be a contradiction for an abstract method to be modified as final or static.
  • Because abstract methods have no implementation, an abstract static method would make no sense.

Homework:
Read pages 382 through 419

giphy

All source: Java Software Solutions by Lewis, Loftus and Cocking

Chapter 6: The Selection Sort


Classwork:

Video from Professor Sedgewick on edmodo.com

Videos you can watch at home:



Sorts: The Selection Sort

selectionSort

selectionSortCode

selectionCodeAndVisual

Time efficiency and the Big-Oh notation

Homework:
1. Tell the story of the Selection Sort to someone who knows no computer programming.
2. Show the sequence of changes the selection sort algorithm makes to the following list of numbers:
5 7 1 8 2 4 3

3. Write the code for the Selection Sort in a static function in a tester/driver class, SortTester.java.
Assign random integers in the range of 15 to 35 to an array of 20 “int” elements.
Display the array.
Sort it with the Selection Sort and display it again.

Chapter 6: The Insertion Sort


Sorts: The Insertion Sort

insertionSort

https://www.youtube.com/watch?v=lCDZ0IprFw4

//********************************************************************
//  Sorts.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the selection sort and insertion sort algorithms,
//  as well as a generic object sort.
//********************************************************************
@SuppressWarnings("unchecked")
public class Sorts
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (int[] numbers)
   {
      int min, temp;

      for (int index = 0; index < numbers.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < numbers.length; scan++)
            if (numbers[scan] < numbers[min])
               min = scan;

         // Swap the values
         temp = numbers[min];
         numbers[min] = numbers[index];
         numbers[index] = temp;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (int[] numbers)
   {
      for (int index = 1; index < numbers.length; index++)
      {
         int key = numbers[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && numbers[position-1] > key)
         {
            numbers[position] = numbers[position-1];
            position--;
         }
            
         numbers[position] = key;
      }
   }

   //-----------------------------------------------------------------
   //  Sorts the specified array of objects using the insertion
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void insertionSort (Comparable[] objects)
   {
      for (int index = 1; index < objects.length; index++)
      {
         Comparable key = objects[index];
         int position = index;

         // shift larger values to the right
         while (position > 0 && objects[position-1].compareTo(key) > 0)
         {
            objects[position] = objects[position-1];
            position--;
         }
            
         objects[position] = key;
      }
   }
}

Classwork:
1. Tell the story of the Insertion Sort to someone who knows no computer programming.
2. Show the sequence of changes the insertion sort algorithm makes to the following list of numbers:
5 7 1 8 2 4 3

Assignments:
MC 6.6
T/F 6.8 and 6.10
SA 6.4 and 6.5
Programming Projects 6.11:
Modify the Tunes program so that it keeps the CDs sorted by title. Use the general object sort defined in the Sorts class from this chapter.

Programming Projects 6.12
Modify the Sorts class to include an overloaded version of the SelectionSort method that performs a general object sort. Modify the SortPhoneList program to test the new sort.

Chapter 6: ArrayLists<E>()

ArrayLists

ArrayLists, type parameter, and for-each loop example

import java.util.ArrayList;

/**
   This bank contains a collection of bank accounts.
*/
public class Bank
{   
   /**
      Constructs a bank with no bank accounts.
   */
   public Bank()
   {
      accounts = new ArrayList();
   }

   /**
      Adds an account to this bank.
      @param a the account to add
   */
   public void addAccount(BankAccount a)
   {
      accounts.add(a);
   }
   
   /**
      Gets the sum of the balances of all accounts in this bank.
      @return the sum of the balances
   */
   public double getTotalBalance()
   {
      double total = 0;
      for (BankAccount a : accounts)
      {
         total = total + a.getBalance();
      }
      return total;
   }

   /**
      Counts the number of bank accounts whose balance is at
      least a given value.
      @param atLeast the balance required to count an account
      @return the number of accounts having least the given balance
   */
   public int count(double atLeast)
   {
      int matches = 0;
      for (BankAccount a : accounts)
      {
         if (a.getBalance() >= atLeast) matches++; // Found a match
      }
      return matches;
   }

   /**
      Finds a bank account with a given number.
      @param accountNumber the number to find
      @return the account with the given number, or null if there
      is no such account
   */
   public BankAccount find(int accountNumber)
   {
      for (BankAccount a : accounts)
      {
         if (a.getAccountNumber() == accountNumber) // Found a match
            return a;
      } 
      return null; // No match in the entire array list
   }

   /**
      Gets the bank account with the largest balance.
      @return the account with the largest balance, or null if the
      bank has no accounts
   */
   public BankAccount getMaximum()
   {
      if (accounts.size() == 0) return null;
      BankAccount largestYet = accounts.get(0);
      for (int i = 1; i < accounts.size(); i++) 
      {
         BankAccount a = accounts.get(i);
         if (a.getBalance() > largestYet.getBalance())
            largestYet = a;
      }
      return largestYet;
   }

   private ArrayList accounts;
}

ArrayLists, type parameter, for-each loop and Iterator another example

/** 
 * mrs.e
 * 12/14/2018
 * 
 * ArrayLists
 * for-each loop
 * Iterator
 * 
 * */

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListIterator 
{
   public static void main( String[] args )
   {
      // add elements to an ArrayList
      String[] pets = { "Puppy", "Kitten", "Gerbil", "Bunny", "Ferret" };
      ArrayList< String > aList = new ArrayList< String >();      

      for ( String pet : pets )
         aList.add( pet ); // for each loop - don't change the ArrayList   

      // add elements in removePets array to removeList
      String[] removePets = { "Gerbil", "Bunny" };
      ArrayList< String > removeList = new ArrayList< String >();

      for ( String pet : removePets )
         removeList.add( pet ); 

      // print items of the list
      System.out.println( "ArrayList: " );

      for ( int count = 0; count < aList.size(); count++ )
         System.out.printf( "%s ", aList.get( count ) );

      // remove from list the pets contained in removeList
      removePets( aList, removeList );

      // print itmes of the list 
      System.out.println( "\n\nArrayList after calling removePets: " );

      for ( String pet : aList )
         System.out.printf( "%s ", pet );
   } // end main

   // remove pets in collection2 found in collection1
   private static void removePets( ArrayList< String > collection1, 
      ArrayList< String > collection2 )
   {
      // get iterator
      Iterator< String > iterator = collection1.iterator(); 

      // loop while collection has items
      while ( iterator.hasNext() )         
      {
         if ( collection2.contains( iterator.next() ) )
            iterator.remove(); // remove current pet
      } // end while
   } // end method removePets
} // end class ArrayListIterator

indexOf and lastIndexOf example from http://www.dotnetperls.com/arraylist-java

import java.util.ArrayList;

public class Program {
    public static void main(String[] args) {

    ArrayList list = new ArrayList<>();
    list.add(101);
    list.add(100);
    list.add(99);
    list.add(100);
    list.add(99);

    // Search for values.
    int index = list.indexOf(100);
    int lastIndex = list.lastIndexOf(100);
    int notFound = list.indexOf(200);

    // Display results.
    System.out.println(index);     // 1
    System.out.println(lastIndex); // 3
    System.out.println(notFound);  // -1
    }
}

Output

1
3
-1

§ § § § § §

More advanced topics in this link. (Optional)

ListIterator, indexOf, Iterator, List Interface
Screen Shot 2014-12-15 at 9.05.33 PM

[collapse]

Screen Shot 2014-12-11 at 9.19.49 PM

Screen Shot 2014-12-11 at 9.26.39 PM

ArrayList<E>()

Constructs an empty list with an initial capacity of ten.

Screen Shot 2014-12-11 at 9.30.09 PM

boolean add(E obj)
Appends the specified element to the end of this list.

void add(int index, E obj)
Inserts the specified element at the specified position in this list.

E get(int index)
Returns the element at the specified position in this list.

int indexOf(Object obj)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

boolean isEmpty()
Returns true if this list contains no elements.

int size()
Returns the number of elements in this list.

ListIterator<E> listIterator()

Returns a list iterator over the elements in this list (in proper sequence).

E remove(int index)
Removes the element at the specified position in this list.

boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.

//********************************************************************
//  DestinysChild.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the use of a ArrayList object.
//********************************************************************

import java.util.ArrayList;

public class DestinysChild
{
   //-----------------------------------------------------------------
   //  Stores and modifies a list of band members.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      ArrayList band = new ArrayList();

      band.add ("Michelle");
      band.add ("Kelly");
      band.add ("Beyonce");
      band.add ("Farrah");

      System.out.println (band);

      int location = band.indexOf ("Farrah");
      band.remove (location);

      System.out.println (band);
      System.out.println ("At index 1: " + band.get(1));

      System.out.println (band);
      System.out.println ("Size of the band: " + band.size());
   }
}


[Michelle, Kelly, Beyonce, Farrah]
[Michelle, Kelly, Beyonce]
At index 1: Kelly
[Michelle, Kelly, Beyonce]
Size of the band: 3

Two ways to iterate through the elements of a collection in java:

  1. By Iterator interface.
  2. By for-each loop.
//********************************************************************
//  Recipe.java       Author: Lewis/Loftus/Cocking
//
//  Demonstrates the use of a ListIterator to iterate through the
//  elements of an ArrayList.
//********************************************************************

import java.util.ArrayList;
import java.util.ListIterator;

public class Recipe
{
   //-----------------------------------------------------------------
   //  Stores and then prints a list of ingredients for a recipe.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      ArrayList ingredients = new ArrayList();

      ingredients.add ("flour");
      ingredients.add ("sugar");
      ingredients.add ("cocoa");
      ingredients.add ("oil");
      ingredients.add ("butter");
      ingredients.add ("eggs");
      ingredients.add ("baking soda");

      System.out.println ("To make a chocolate cake, use the following " +
         ingredients.size() + " ingredients:");

      // traversing ArrayList by Iterator
      ListIterator iterator = ingredients.listIterator(); 
      while (iterator.hasNext())
         System.out.println(iterator.next());

      // traversing ArrayList using the "for-each" loop
       for(String obj:ingredients)  
          System.out.println(obj);  

   }
}

To make a chocolate cake, use the following 7 ingredients:
flour
sugar
cocoa
oil
butter
eggs
baking soda

Classwork/Homework:
1. Create a class DataSet of BankAccounts. The constructor creates an ArrayList of BankAccount(s). Write the test class to add, delete and print (use the for-each loop) bank accounts. Your driver should have at least 3 BankAccount objects. Make any changes necessary to have the DataSet ADT implemented with an ArrayList. Also, change the comments to fit the modifications to the it.

DataSet with ArrayList

/**
   Computes the average of a set of data values.
*/
public class DataSet
{
   /**
      Constructs an empty data set with no BankAccount objects.
   */
   public DataSet()
   {
      sum = 0;
      count = 0; // Warning: Is "count" necessary?
      maximum = 0; // Warning: this should not be a numeric type!!
      // create an object of the ArrayList dataSet
   }

   /**
      Adds a BankAccount object to the data set
      @param x a BankAccount object
   */
   public void add(BankAccount bank)
   {
      // add object to ArrayList
      sum = sum + bank.getBalance();
      if (count == 0 || maximum.getBalance() < x.getBalance()) maximum = x;
      count++;
   }

   /**
      Gets the average of the added BankAccount balances.
      @return the average or 0 if no data has been added
   */
   public double getAverage()
   {
      if (count == 0) return 0;
      else return sum / count;  // Warning
   }

   /**
      Gets the largest of the added data. // changes must be made here
      @return the maximum or 0 if no data has been added
   */
   public BankAccount getMaximum()
   {
      return maximum;
   }

   /**
      Prints the balance for each element of the added data in the ArrayList.// Warning
      @return the average or 0 if no data has been added
   */
   public void printDataSet()
   {
     // implement the code to print the elements of the ArrayList datSet. Add more specific comments
     // use the for-each loop
   }

   private double sum;
   private BankAccount maximum;
   private int count;
   private ArrayList dataSet;
}

[collapse]
  1. Create a class Purse (see below) that contains an ArrayList of Coin(s). Write the test class to add, delete, and print coins. Make any changes necessary to have the Purse class implemented with an ArrayList. Also, change the comments to fit the modifications to the ADT.
/**
   A coin with a monetary value.
*/
public class Coin
{
   /**
      Constructs a coin.
      @param aValue the monetary value of the coin.
      @param aName the name of the coin
   */
   public Coin(double aValue, String aName) 
   { 
      value = aValue; 
      name = aName;
   }

   /**
      Gets the coin value.
      @return the value
   */
   public double getValue() 
   {
      return value;
   }

   /**
      Gets the coin name.
      @return the name
   */
   public String getName() 
   {
      return name;
   }

   public boolean equals(Object otherObject)
   {  
      Coin other = (Coin)otherObject;
      return name.equals(other.name) 
         && value == other.value; 
   }

   private double value;
   private String name;
}

Modify the Purse class to be implemented with ArrayList<Coin> and to use a for-each loop.
Implement the following methods in the Purse class:

   /**
      Counts the number of coins in the purse
      @return the number of coins
   */
   public int count()
   {
     
   }

   /**
      Tests if the purse has a coin that matches
      a given coin.
      @param aCoin the coin to match
      @return true if there is a coin equal to aCoin
   */
   public boolean find(Coin aCoin)
   {

   }

   /**
      Counts the number of coins in the purse that match
      a given coin.
      @param aCoin the coin to match
      @return the number of coins equal to aCoin
   */
   public int count(Coin aCoin)
   {

   }

   /**
      Finds the coin with the largest value. 
      (Precondition: The purse is not empty)
      @return a coin with maximum value in this purse
   */
   pulic Coin getMaximum()
   {

   }

NOTE: Use the “for-each” loop in the implementation.