Author Archives: graceliana@gmail.com

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 Application Project

Pet Shop v1 Application Project

This is the story about a pet shop with few pets but really cute ones. You will write an application designed to get information about this unusual pet shop. You will be applying concepts about polymorphism to develop your classes and driver program. This version one (v1) of Pet Shop will focus on inheritance. In addition, this application will help you better prepare for the AP exam Inheritance, and Polymorphism.

PetShop v1

The way you should focus on this project:

1. DO NOT VIOLATE ENCAPSULATION
2. Use INHERITANCE CONCEPTS to re-use code and functionality.

Specifications:
You will create only one animal class and two of your classmates will share one of their own each for you to have a complete Pet Shop application.

The sample test below is given only to help you understand this assignment. Write your own test class but follow this general outline:
(NOTE: How close you follow the outline and polymorphism concepts will determine your grade)

  • Create an object of PetSet.
  • Create multiple instances of at least three different kinds of animals. Observe that they are Animal objects.
  • Print each Animal object’s sound.
  • Add them to the PetSet object.
  • Print the PetSet object to display what the animals are.

Sample test:

…
PetSet myPetShop = new PetSet();
        
Animal tweety = new Bird(45.00, "I am a bird");
Animal duffy = new Bird(5.00, "I am a bird");
Animal nemo = new Fish(62.00, "I am a fish"); 
Animal bear = new Dog(1100.00, "I am a dog");
…
…
myPetShop.add(tweety);
myPetShop.add(duffy);
myPetShop.add(nemo);
myPetShop.add(bear);
…
System.out.println(myPetShop);


tweety duffy nemo bear

Sample output:

I can sing // phrase

I am a bird
I am a bird
I am a fish
I am a dog

1. PetSet class has the following members:
Attributes:

  • an ArrayList of Animal objects (use type parameter)
  • sum (type double)
  • maxPrice (type double)

Behaviors:

  • one constructor
  • add() an animal to the ArrayList and keeps track of largest price, it also updates the sum variable with the price of the animal
  • getAverage()
  • getMaximum()
  • toString()
  • whatIam() uses the for each loop to create a string with all the strings. Look at the sample output above.

This class should be very similar to the DataSet class.

2. The Animal class has the following members:
Attributes:

  • price (type double)
  • animal (type String)

Behaviors:

  • one constructor with one input argument, the “animal” or “price”
  • whatIam()
  • getPrice()
  • setPrice()

3. Animal’s children classes:
Each animal ( as an example: Bird) has the following members:
Attribute:

  • phrase (type String)
  • // For one choice of design. The other design, you do not have an instance field.

Behaviors:

  • one constructor
  • whatIam() returns “I am a bird”
  • getPrice()
  • a method of your choice unique to all animal’s child class. In the case of a bird, tweet() returns “I can sing”. This can be done in two different ways according to your design.

4. Driver and a bit more:

  • Create your own driver with all the necessary components to test all parts of your application.
  • Keep to the specification given so everyone can exchanges classes and have an easy integration into everyone’s application.
  • Maintain good documentation so everyone understands your code.
  • Talk to your classmates to find out what animal they are implementing so there is not unnecessary duplication.
  • Since the specifications for each class have been already defined above, work on the animal’s child class first so you can share with your classmates before the end of the period.

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.