Java Reference
In-Depth Information
Display 8.2
The Derived Class DiscountSale (part 1 of 2)
1 /**
2 Class for a sale of one item with discount expressed as a percent of
3 the price, but no other adjustments.
4 Class invariant: The price is always nonnegative; the name is a
5 nonempty string; the discount is always nonnegative.
6 */
7 public class DiscountSale extends Sale
8 {
9
private double discount; //A percent of the price. Cannot be
//negative.
10 public DiscountSale()
11 {
12 super ();
13 discount = 0;
14 }
The meaning would be unchanged if
this line were omitted.
15
/**
16
Precondition: theName is a nonempty string; thePrice is
nonnegative; theDiscount is expressed as a percent of the price
17
and is nonnegative.
18
*/
19
public DiscountSale(String theName,
20
double thePrice, double theDiscount)
21 {
22 super (theName, thePrice);
23 setDiscount(theDiscount);
24 }
25 public DiscountSale(DiscountSale originalObject)
26 {
27 super (originalObject);
28 discount = originalObject.discount;
29 }
30 public static void announcement()
31 {
32 System.out.println("This is the DiscountSale class.");
33 }
34 public double bill()
35 {
36
double fraction = discount/100;
37
return (1 − fraction)*getPrice();
38 }
Search WWH ::




Custom Search