Question
Java : /** Class for a simple sale of one item with no tax, discount, or other adjustments. Class invariant: The price is always nonnegative;
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; /onnegative
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( )
}
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 );
}
}
/**
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);
}
// Copy Constructor
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 + "% "
+ " 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 );
}
}
1. Download the Sale and DiscountSale classes from the K: drive. 2. Define a class named Multi! tem Sale as specified in Chapter 8-Project #7 3. Include some accessor and mutator methods as well as a copy constructor, a clone method, and an equals method. 4. Create a test program that creates two MultiltemSale objects, multiSalel and multiSale2. Each object should contain 5 - 6 sale items that are a combination of Sale and Discount Sale objects. Use your accessor methods to print out several ite from each object. 5. Use your copy constructor to create a twin object of multiSale1, multiSale3, and a twin object of multiSale2, multiSale4. Use your mutator methods to change sever of the items in these objects. are unique and not the same objects as multiSale1 and multiSale2. multiSale2. Be sure your display identifies the objects and their items for the user 6. Use your equals method to prove that the twin objects, multiSale3 and multiSale4 7. Print out the individual items and the total sale amount for both multiSale1 and 8. Print out the individual items and the total sale amount for both multiSale3 and multiSale4. Be sure your display identifies the objects and their items for the user 9. Use your clone method to create a twin object of multiSale3, multiSale5, anda twin object of multiSale4, multiSale6. Use your mutator methods to change seve different items in these objects. 10. Print out the individual items and the total sale amount for both multiSale5 and multiSale6. Be sure your display identifies the objects and their items for the us 11. Print out the memory addresses of all six objects to ensure that they are indeed all unique objects. 1. Download the Sale and DiscountSale classes from the K: drive. 2. Define a class named Multi! tem Sale as specified in Chapter 8-Project #7 3. Include some accessor and mutator methods as well as a copy constructor, a clone method, and an equals method. 4. Create a test program that creates two MultiltemSale objects, multiSalel and multiSale2. Each object should contain 5 - 6 sale items that are a combination of Sale and Discount Sale objects. Use your accessor methods to print out several ite from each object. 5. Use your copy constructor to create a twin object of multiSale1, multiSale3, and a twin object of multiSale2, multiSale4. Use your mutator methods to change sever of the items in these objects. are unique and not the same objects as multiSale1 and multiSale2. multiSale2. Be sure your display identifies the objects and their items for the user 6. Use your equals method to prove that the twin objects, multiSale3 and multiSale4 7. Print out the individual items and the total sale amount for both multiSale1 and 8. Print out the individual items and the total sale amount for both multiSale3 and multiSale4. Be sure your display identifies the objects and their items for the user 9. Use your clone method to create a twin object of multiSale3, multiSale5, anda twin object of multiSale4, multiSale6. Use your mutator methods to change seve different items in these objects. 10. Print out the individual items and the total sale amount for both multiSale5 and multiSale6. Be sure your display identifies the objects and their items for the us 11. Print out the memory addresses of all six objects to ensure that they are indeed all unique objectsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started