Considering a hierarchy of greeting cards. The parent class is Cards, and its ch
ID: 3913602 • Letter: C
Question
Considering a hierarchy of greeting cards. The parent class is Cards, and its children classes are Valentine, Holiday and Birthday, as shown below:
In this exercise, Cards class is provided as below. As you can see, Cards class contains a recipient's name in String type, and a method: greeting(). In this exercise, please implement the three Cards children classes: Valentine, Holiday, and Birthday, according to the following specifications:
Valentine class contains its own data field: int kisses.
Birthday class contains its own data field: int age.
Implement the constructor and greeting() method for each subclass. The constructor should initilize the subclass's data field as well as its parent's data field. The greeting() method outputs the message related to its own subclass and should contain the information of its atributes.
The example greeting messages are followings:
Your three subclasses should work with the following driver program "CardTest.java".
Submission: print out four files: Valentine.java, Holiday.java, Birthday.java, and CardTest.java, and append your result at the end.
Card Valentine Birthday HolidayExplanation / Answer
ScreenShot
Program
//Cards.java
class Cards{
//Member variable
String recipient;
//Default constructor
public Cards() {
this("John Smith");
}
//Parameterized constructor
public Cards(String recipient) {
this.recipient=recipient;
}
//Member method
public void greeting() {
System.out.println("Dear "+recipient+": ");
System.out.println("Wish you a wonderful summer!");
}
}
//Valentine .java
class Valentine extends Cards{
//Member variable
int kisses;
//Parameterized constructor
public Valentine(String recipient,int kisses) {
this.kisses=kisses;
this.recipient=recipient;
}
//Member method access from base class
public void greeting() {
System.out.print("Dear "+recipient+", ");
System.out.print("Love and Kisses, ");
for(int i=0;i<kisses;i++) {
System.out.print("X");
}
System.out.println("Kisses");
}
}
//Birthday.java
class Birthday extends Cards{
//Member variable
int age;
//parameterized constructor
public Birthday(String recipient,int age) {
this.age=age;
this.recipient=recipient;
}
//Base class greetings modified
public void greeting() {
System.out.print("Dear "+recipient+" ");
System.out.println("Happy "+age+"th Birthday!");
}
}
//Holiday.java
class Holiday extends Cards{
//Parameterized constructor
public Holiday(String recipient) {
this.recipient=recipient;
}
//Base class greetings reused
public void greeting() {
System.out.print("Dear "+recipient+" ");
System.out.println("Season's Greetings!");
}
}
//CardTest.java
public class CardTest {
public static void main(String[] args) {
//Object creation and method calling
Cards card=new Holiday("Alice");
card.greeting();
card=new Valentine("Bob",12);
card.greeting();
card=new Birthday("Charlie",4);
card.greeting();
}
}
------------------------------------------------------------------
Output
Dear Alice
Season's Greetings!
Dear Bob,
Love and Kisses,
XXXXXXXXXXXXKisses
Dear Charlie
Happy 4th Birthday!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.