Post date: May 16, 2018 1:49:26 PM
Chapter 7 Inheritance
7.1 Inheritance Basics
7.2 Encapsulation and Inheritance
7.3 Programming with inheritance
Suggested Self-Test Exercises:
p475: 1-4
p480: 5-6
p486: 7-8
p492: 11-13
p495-6: 14-17
p502-3: 18-22
Lab Exercise: Implement the following classes
Lab Exercises:
p508-9: Ex 1
p509: Ex 2
p511-12: Ex 6, 7
In-Class Exercises:
p511-12: Ex 6
Create a class called Vehicle that has the manufacturer’s name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has the following additional properties: the load capacity in tons (type double since it may contain a fractional part) and towing capacity in pounds (type int). Be sure your class has a reasonable complement of constructors, accessor and mutator methods, and suitably defined equals and toString methods. Write a program to test all your methods.
The definition of the class Person follows. Completing the definitions of the methods is part of this programming project.
public class Person
{
private String name;
public Person()
{...}
public Person(String theName)
{...}
public Person(Person theObject)
{...}
public String getName()
{...}
public void setName(String theName)
{...}
public String toString()
{...}
public boolean equals(Object other)
{...}
}
In-Class Exercise 2:
Define a class named Payment that contains an instance variable of type double that stores the amount of the payment and appropriate accessor and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment.
Next, define a class named CashPayment that is derived from Payment . This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor(s).
Define a class named CreditCardPayment that is derived from Payment . This class should contain instance variables for the name on the card, expiration date, and credit card number. Include appropriate constructor(s).
Finally, redefine the paymentDetails method to include all credit card information in the printout.
Create a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.
* You can find an implementation of to number to English converter here initially found here.