_Java Polymorphism and Abstract Classes (3)
_Java Polymorphism and Abstract Classes (3)
Polymorphism
Define a class named Sale in the package polymorphism. The class should represent a
simple sale of one item with no tax, discount, or other adjustments. Ensure that your class
satisfies the following conditions:
1. Class Invariants:
○ The price must always be nonnegative.
○ The name must be a nonempty string.
2. Class Definition:
○ Include the following private fields:
■ name (of type String)
■ price (of type double)
○ Provide the following constructors:
■ A default constructor that sets name to "no name yet" and price to 0.
■ A parameterized constructor that accepts a String for the name and a
double for the price.
■ A copy constructor that creates a deep copy of another Sale object. If the
provided object is null, it should display an error message and terminate
the program.
○ Implement getter and setter methods for both fields. Ensure that:
■ The setPrice method does not allow negative values and displays an
error message if a negative value is provided.
■ The setName method does not accept null or empty strings and displays
an error message if an invalid value is provided.
○ Include a toString method that returns a string representation of the sale
object in the format "name Price and total cost: price".
○ Implement an equals method to compare two Sale objects based on their
name and price.
○ Implement a bill method that returns the price.
○ Implement the equalDeals method that compares two Sale objects for equality
based on their name and bill amount.(bill amount is determined by a call to the
bill method)
○ Implement a lessThan method that compares two Sale objects based on their
bill amounts.(bill amount is determined by a call to the bill method)
○ Include a static method announcement that prints "This is the Sale
class".
Write the complete Java code for the Sale class according to the specifications above.
Define a class named DiscountSale that extends the Sale class in the package
polymorphism. This class should represent a sale of one item with a discount expressed as a
percentage of the price, but no other adjustments. Ensure that your class satisfies the following
conditions:
1. Class Invariants:
○ The price must always be nonnegative.
○ The name must be a nonempty string.
○ The discount must always be nonnegative.
2. Class Definition:
○ Include the following private field:
■ discount (of type double) representing the discount percentage on the
sale.
○ Provide the following constructors:
■ A default constructor that initializes the sale with "no name yet" for the
name, 0 for the price, and 0 for the discount.
■ A parameterized constructor that accepts a String for the name, a
double for the price, and a double for the discount percentage.
■ A copy constructor that creates a deep copy of another DiscountSale
object.
○ Implement a static method announcement that prints "This is the
DiscountSale class".
○ Override the bill method from the Sale class to calculate the total cost after
applying the discount. The bill should be calculated using the formula: (1 -
discount/100) * price.
○ Implement getter and setter methods for the discount field. Ensure that the
setDiscount method does not allow negative values and displays an error
message if a negative value is provided.
○ Override the toString method to return a string representation of the
DiscountSale object in the format: "name Price = $price Discount =
discount%\n Total cost = $total cost".
○ Override the equals method to compare two DiscountSale objects based on
their name, price, and discount percentage.
Write the complete Java code for the DiscountSale class according to the specifications
above.
1. Explain the difference between the terms late binding and polymorphism
2. Suppose that you modify the definitions of the class Sale by adding the modifier final to
the definition of the method bill. How would that change the output of the program ?
3. Would it be legal to add the following method definition to the class DiscountSale ?
public static boolean isAGoodBuy(Sale theSale)
{
return (theSale.getDiscount() > 20);
}
4. Consider the following code
Sale saleVariable;
DiscountSale discountVariable = new DiscountSale("paint", 15, 10);
saleVariable = (Sale)discountVariable;
System.out.println(saleVariable.toString());
We saw that without the type cast, the definition of the toString method used is the one
given in the definition of the class DiscountSale. With this added type cast, will the
definition of the toString method used still be the one given in DiscountSale or will it be
the one given in the definition of Sale ?
5. A first look at the clone method and the limitations of copy constructors. We will discuss
about this topic in class and we will see the implementation as well.
Abstract Classes
6. Recall the Employee class we defined when discussing Inheritance. Suppose you
wanted to add a method called samePay. samePay returns true by comparing the pay of
the calling employee object and the pay of the object given as an argument. It uses a
call to a method called getPay defined in the descendant classes of Employee.
Make the necessary additions to the definition of the class Employee to implement the
method described above.
7. Can a method definition include an invocation of an abstract method ?
8. Can you have a variable whose type is an abstract class?
9. Can you have a parameter whose type is an abstract class ?
10. Is it legal to have an abstract class in which all methods are abstract ?
11. Why bother to have any constructors in an abstract class? Aren’t they useless ?