Java Reference
In-Depth Information
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.
2. Define a class named Document that contains an instance variable of type String
named text that stores any textual content for the document. Create a method
named toString that returns the text field and also include a method to set
this value.
Next, define a class for Email that is derived from Document and includes instance
variables for the sender , recipient , and title of an email message. Implement
appropriate accessor and mutator methods. The body of the email message should
be stored in the inherited variable text . Redefine the toString method to con-
catenate all text fields.
Similarly, define a class for File that is derived from Document and includes a
instance variable for the pathname . The textual contents of the file should be stored
in the inherited variable text . Redefine the toString method to concatenate all
text fields.
Finally, create several sample objects of type Email and File in your main method.
Test your objects by passing them to the following subroutine that returns true if
the object contains the specified keyword in the text property.
public static boolean ContainsKeyword(Document docObject,
String keyword)
{
if (docObject.toString().indexOf(keyword,0) >= 0)
return true ;
return false ;
}
3. The following is some code designed by J. Hacker for a video game. There is an
Alien class to represent a monster and an AlienPack class that represents a band
of aliens and how much damage they can inflict:
VideoNote
Solution to
Programming
Project 7.3
class Alien
{
public static final int SNAKE_ALIEN = 0;
public static final int OGRE_ALIEN = 1;
public static final int MARSHMALLOW_MAN_ALIEN = 2;
public int type; // Stores one of the three above types
public int health; // 0=dead, 100=full strength
public String name;
Search WWH ::




Custom Search