Payable interface declaration 1 2 3 4 public interface Payable 5 { 6 double getP
ID: 3648352 • Letter: P
Question
Payable interface declaration1
2
3
4 public interface Payable
5 {
6 double getPaymentAmount();
7 }
Invoice class that implements Payable
1
3
4 public class Invoice implements Payable
5 {
6 private String partNumber;
7 private String partDescription;
8 private int quantity;
9 private double pricePerItem;
10
11
12 public Invoice( String part, String description, int count,
13 double price )
14 {
15 partNumber = part;
16 partDescription = description;
17 setQuantity( count );
18 setPricePerItem( price );
19 }
20
21
22 public void setPartNumber( String part )
23 {
24 partNumber = part;
25 }
26
27
28 public String getPartNumber()
29 {
30 return partNumber;
31 }
32
33
34 public void setPartDescription( String description )
35 {
36 partDescription = description;
37 }
38
39
40 public String getPartDescription()
41 {
42 return partDescription;
43 }
44
45
46 public void setQuantity( int count )
47 {
48 quantity = ( count < 0 ) ? 0 : count; // quantity cannot be negative
49 }
50
51
52 public int getQuantity()
53 {
54 return quantity;
55 }
56
58 public void setPricePerItem( double price )
59 {
60 pricePerItem = ( price < 0.0 ) ? 0.0 : price; // validate price
61 }
62
63
64 public double getPricePerItem()
65 {
66 return pricePerItem;
67 }
68
69
70 public String toString()
71 {
72 return String.format( "%s: %s: %s (%s) %s: %d %s: $%,.2f",
73 "invoice", "part number", getPartNumber(), getPartDescription(),
74 "quantity", getQuantity(), "price per item", getPricePerItem() );
75 }
76
77 // method required to carry out contract with interface Payable
78 public double getPaymentAmount()
79 {
80 return getQuantity() * getPricePerItem(); // calculate total cost
81 }
82 }
Payable interface test program processing Invoices and Employees polymorphically.
3
4 public class PayableInterfaceTest
5 {
6 public static void main( String args[] )
7 {
8
9 Payable payableObjects[] = new Payable[ 4 ];
10
11
12 payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375.00 );
13 payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
14 payableObjects[ 2 ] =
15 new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
16 payableObjects[ 3 ] =
17 new SalariedEmployee( "Lisa", "Barnes", "888-88-8888", 1200.00 );
18
19 System.out.println(
20 "Invoices and Employees processed polymorphically: " );
21
22
23 for ( Payable currentPayable : payableObjects )
24 {
25 amount
26 System.out.printf( "%s %s: $%,.2f ",
27 currentPayable.toString(),
28 "payment due", currentPayable.getPaymentAmount() );
29 }
30 }
31 }
Use the program shown in Table 1, then change the interface Payable to an Abstract class.
Explanation / Answer
Payable interface declaration 1 2 3 4 public interface Payable 5 { 6 double getPaymentAmount(); 7 } Invoice class that implements Payable 1 3 4 public class Invoice implements Payable 5 { 6 private String partNumber; 7 private String partDescription; 8 private int quantity; 9 private double pricePerItem; 10 11 12 public Invoice( String part, String description, int count, 13 double price ) 14 { 15 partNumber = part; 16 partDescription = description; 17 setQuantity( count ); 18 setPricePerItem( price ); 19 } 20
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.