Title: Polymorphism Book Examples
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

Polymorphism Book Examples

Sale.java




/**
 Class for a simple sale of one item with no tax, discount, or other adjustments.
 Class invariant: The price is always nonnegative; the name is a nonempty string.
*/
public class Sale
{
    private String name; //A nonempty string
    private double price; //nonnegative

    public Sale( )
    {
        name = "No name yet";
        price = 0;
    }

    /**
     Precondition: theName is a nonempty string; thePrice is nonnegative.
    */
    public Sale(String theName, double thePrice)
    {
        setName(theName);
        setPrice(thePrice);
    }

    public Sale(Sale originalObject)
    {
        if (originalObject == null)
        {
            System.out.println("Error: null Sale object.");
            System.exit(0);
        }
        //else
        name = originalObject.name;
        price = originalObject.price;
    }

    public static void announcement( )
    {
        System.out.println("This is the Sale class.");
    }

    public double getPrice( )
    {
        return price;
    }

   /**
     Precondition: newPrice is nonnegative.
    */
    public void setPrice(double newPrice)
    {
        if (newPrice >= 0)
            price = newPrice;
        else
        {
            System.out.println("Error: Negative price.");
            System.exit(0);
        }
    }

    public String getName( )
    {
        return name;
    }

    /**
     Precondition: newName is a nonempty string.
    */
    public void setName(String newName)
    {
        if (newName != null && newName != "")
            name = newName;
        else
        {
            System.out.println("Error: Improper name value.");
            System.exit(0);
        }
    }

    public String toString( )
    {
        return (name + " Price and total cost = $" + price);
    }

    public double bill( )
    {
        return price;
    }

    /*
     Returns true if the names are the same and the bill for the calling
     object is equal to the bill for otherSale; otherwise returns false.
     Also returns false if otherObject is null.
    */
    public boolean equalDeals(Sale otherSale)
    {
        if (otherSale == null)
            return false;
        else
            return (name.equals(otherSale.name)
                && bill( ) == otherSale.bill( ));
    }

    /*
     Returns true if the bill for the calling object is less
     than the bill for otherSale; otherwise returns false.
    */
    public boolean lessThan (Sale otherSale)
    {
        if (otherSale == null)
        {
            System.out.println("Error: null Sale object.");
            System.exit(0);
        }
        //else
        return (bill( ) < otherSale.bill( ));
    }

    public boolean equals(Object otherObject)
    {
        if (otherObject == null)
            return false;
        else if (getClass( ) != otherObject.getClass( ))
            return false;
        else
        {
            Sale otherSale = (Sale)otherObject;
            return (name.equals(otherSale.name)
               && (price == otherSale.price));
        }
    }

    public Sale clone( )
    {
        return new Sale(this );
    }
}


DiscountSale.java


/**
 Class for a sale of one item with discount expressed as a percent of the price,
 but no other adjustments.
 Class invariant: The price is always nonnegative; the name is a
 nonempty string; the discount is always nonnegative.
*/

public class DiscountSale extends Sale
{
    private double discount; //A percent of the price. Cannot be negative.

    public DiscountSale( )
    {
        super( );
        discount = 0;
    }

    /**
     Precondition: theName is a nonempty string; thePrice is nonnegative;
     theDiscount is expressed as a percent of the price and is nonnegative.
    */
    public DiscountSale(String theName,
                                  double thePrice, double theDiscount)
    {
        super(theName, thePrice);
        setDiscount(theDiscount);
    }

    public DiscountSale(DiscountSale originalObject)
    {
        super(originalObject);
        discount = originalObject.discount;
    }

    public static void announcement( )
    {
        System.out.println("This is the DiscountSale class.");
    }

    public double bill( )
    {
        double fraction = discount/100;
        return (1 - fraction)*getPrice( );
    }

    public double getDiscount( )
    {
        return discount;
    }

    /**
     Precondition: Discount is nonnegative.
    */
    public void setDiscount(double newDiscount)
    {
        if (newDiscount >= 0)
            discount = newDiscount;
        else
        {
            System.out.println("Error: Negative discount.");
            System.exit(0);
        }
    }

    public String toString( )
    {
        return (getName( ) + " Price = $" + getPrice( )
                + " Discount = " + discount + "%\n"
                + "   Total cost = $" + bill( ));
    }

    public boolean equals(Object otherObject)
    {
        if (otherObject == null)
            return false;
        else if (getClass( ) != otherObject.getClass( ))
            return false;
        else
        {
            DiscountSale otherDiscountSale =
                             (DiscountSale)otherObject;
            return (super.equals(otherDiscountSale)
                && discount == otherDiscountSale.discount);
        }
    }

    public DiscountSale clone( )
    {
        return new DiscountSale(this );
    }
}



LateBindingDemo.java



/**
 Demonstrates late binding.
*/
public class LateBindingDemo
{
    public static void main(String[] args)
    {
        Sale simple = new Sale("floor mat", 10.00);//One item at $10.00.
        DiscountSale discount = new DiscountSale("floor mat", 11.00, 10);
                                 //One item at $11.00 with a 10% discount.
        System.out.println(simple);
        System.out.println(discount);

        if (discount.lessThan(simple))
            System.out.println("Discounted item is cheaper.");
        else
            System.out.println("Discounted item is not cheaper.");

        Sale regularPrice = new Sale("cup holder", 9.90);//One item at $9.90.
        DiscountSale specialPrice = new DiscountSale("cup holder", 11.00, 10);
                                 //One item at $11.00 with a 10% discount.
        System.out.println(regularPrice);
        System.out.println(specialPrice);

        if (specialPrice.equalDeals(regularPrice))
            System.out.println("Deals are equal.");
        else
            System.out.println("Deals are not equal.");
    }
}



StaticMethodsDemo.java



/**
 Demonstrates that static methods use static binding.
*/
public class StaticMethodsDemo
{
    public static void main(String[] args)
    {
        Sale.announcement( );
        DiscountSale.announcement( );
        System.out.println(
             "That showed that you can override a static method definition.");

        Sale s = new Sale( );
        DiscountSale discount = new DiscountSale();
        s.announcement( );
        discount.announcement( );
        System.out.println("No surprises so far, but wait.");

        Sale discount2 = discount;
        System.out.println(
              "discount2 is a DiscountSale object in a Sale variable.");
        System.out.println( "Which definition of announcement( ) will it use?");
        discount2.announcement( );
        System.out.println(
              "It used the Sale version of announcement( )!");
   }
}


CopyingDemo.java



/**
 Demonstrates where the clone method works,
 but copy constructors do not.
*/
public class CopyingDemo
{

    public static void main(String[] args)
    {
        Sale[] a = new Sale[2];
        a[0] = new Sale("atomic coffee mug", 130.00);
        a[1] = new DiscountSale("invisible paint", 5.00, 10);
        int i;

        Sale[] b = badCopy(a);

        System.out.println("With copy constructors:");
        for (i = 0; i < a.length; i++)
        {
            System.out.println("a[" + i + "] = " + a[i]);
            System.out.println("b[" + i + "] = " + b[i]);
            System.out.println();
        }
        System.out.println();

        b = goodCopy(a);

        System.out.println("With clone method:");
        for (i = 0; i < a.length; i++)
        {
            System.out.println("a[" + i + "] = " + a[i]);
            System.out.println("b[" + i + "] = " + b[i]);
            System.out.println();
        }

    }

    /**
     Supposedly returns a safe copy of a. That is, if b is the
     array returned, then b[i] is supposedly an independent copy of a[i].
    */
     public static Sale[] badCopy(Sale[] a)
     {
         Sale[] b = new Sale[a.length];
         for (int i = 0; i < a.length; i++)
             b[i] = new Sale(a[i]);//Problem here!
         return b;
     }

     /**
      Returns a safe copy of a.That is, if b is the
      array returned, then b[i] is an independent copy of a[i].
     */
     public static Sale[] goodCopy(Sale[] a)
     {
         Sale[] b = new Sale[a.length];
         for (int i = 0; i < a.length; i++)
             b[i] = a[i].clone( );
         return b;
     }
 }


Employee.java ( you will also need HourlyEmployee, SalariedEmployee, and Date)



/**
 Class Invariant: All objects have a name string and hire date.
 A name string of "No name" indicates no real name specified yet.
 A hire date of January 1, 1000 indicates no real hire date specified yet.
*/
public abstract class Employee
{
    private String name;
    private Date hireDate;

    public abstract double getPay( );

    public boolean samePay(Employee other)
    {
        if (other == null)
        {
            System.out.println("Error: null Employee object.");
            System.exit(0);
        }
        //else
        return (this.getPay( ) == other.getPay( ));
    }

    public Employee( )
    {
         name = "No name";
         hireDate = new Date("Jan", 1, 1000); //Just a placeholder.
    }

    /**
     Precondition: Neither theName nor theDate is null.
    */
    public Employee(String theName, Date theDate)
    {
        if (theName == null || theDate == null)
        {
             System.out.println("Fatal Error creating employee.");
             System.exit(0);
        }
        name = theName;
        hireDate = new Date(theDate);
    }

    public Employee(Employee originalObject)
    {
         name = originalObject.name;
         hireDate = new Date(originalObject.hireDate);
    }

    public String getName( )
    {
        return name;
    }

    public Date getHireDate( )
    {
        return new Date(hireDate);
    }

    /**
     Precondition newName is not null.
    */
    public void setName(String newName)
    {
        if (newName == null)
        {
             System.out.println("Fatal Error setting employee name.");
             System.exit(0);
        }
       else
            name = newName;
    }

    /**
     Precondition newDate is not null.
    */
    public void setHireDate(Date newDate)
    {
        if (newDate == null)
        {
             System.out.println("Fatal Error setting employee hire date.");
             System.exit(0);
        }
        else
            hireDate = new Date(newDate);
    }


    public String toString( )
    {
        return (name + " " + hireDate.toString( ));
    }


    public boolean equals(Object otherObject)
    {
        if (otherObject == null)
            return false;
        else if (getClass( ) != otherObject.getClass( ))
            return false;
        else
        {
            Employee otherEmployee =
                           (Employee)otherObject;
            return (name.equals(otherEmployee.name)
             && hireDate.equals(otherEmployee.hireDate));
        }
    }
}



DemoAbstractEmployee.java


public class DemoAbstractEmployee {
    public static void main(String[] args)
    {
        Employee e1 = new SalariedEmployee("Fred", new Date(1,1,2000), 12000);
        Employee e2 = new HourlyEmployee("John", new Date(3,3,2001), 10, 100);

        if (e1.samePay(e2))
            System.out.println(e1.getName() + " has the same pay as " + e2.getName());
    }
}




Last Modified: October 28, 2013 0:13 AM