1. The following program shows example of abstract class and abstract method. a. Write, compile and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
1. The following program shows example of abstract class and abstract method. a. Write, compile and run the following program of three classes. Give the output. abstract class Card String recipient; public abstract void greeting(); class Holiday extends Card public Holiday String ) ( recipient } public void greeting() System.out.println("Dear + recipient + " "); System.out.println("Season's Greetings! "); public class CardTester f public static void main(String[] args) ( } Holiday hol new Holiday ("Santa"); hol.greeting(); b. Add Birthday class that inherits from abstract Card class as following skeleton. public class CardTester public static void main(String[] args) f Card card new Holiday( "Amy" ); card. greeting(); //Invoke a Holiday greeting() card=new Birthday( "Cindy", 17 ); card. greeting(); //Invoke a Birthday greeting () } c. Instantiate a Birthday object and assign to greeting() method in the Card Tester class as following code: Birthday bd = new Birthday( "Maria", 21 ); bd.greeting(); d. Compile and Run. Give the output. e. Rewrite your CardTester class with below program. class f int age; extends public { recipient = 1; age = years; } (String r, int years ) public void greeting () { } System.out.println("Dear System.out.println("Happy " + recipient + ", "); + "th Birthday "); 2. The following program demonstrates polymorphic behavior. Write, compile and run the following program. Give the output. import java.io.*; import java.awt.*; public abstract class Animal { private String name; public Animal (String nm) { name nm; } } public String getName() { } return(name); public abstract void speak(); public class Dog extends Animal { public Dog (String nm) super (nm); } public void speak () System.out.println("Woof"); } public class Cow extends Animal { public Cow (String nm) } super (nm); } public void speak() System.out.println("Moo"); } public class VariousAnimal { { public static void main(String[] args) Animal ref; Dog aDog = new Dog ("Lassie"); Cow aCow = new Cow ("Mabel"); ref=aCow; ref. speak(); ref=aDog; ref. speak(); Explain the polymorphic behavior. 3. Extend the program in exercise 2 by adding two more subclasses of Animal i.e. Snake and Cat where each has a method speak() which prints "Sss" and "Meow" respectively. 4. Modify the extended program created in Exercise 3 in this lab by creating an array of superclass objects and manipulate it by invoking the appropriate method for each subclass member to print the sound of each animal eg. dog, cat, snake and cow. 1. The following program shows example of abstract class and abstract method. a. Write, compile and run the following program of three classes. Give the output. abstract class Card String recipient; public abstract void greeting(); class Holiday extends Card public Holiday String ) ( recipient } public void greeting() System.out.println("Dear + recipient + " "); System.out.println("Season's Greetings! "); public class CardTester f public static void main(String[] args) ( } Holiday hol new Holiday ("Santa"); hol.greeting(); b. Add Birthday class that inherits from abstract Card class as following skeleton. public class CardTester public static void main(String[] args) f Card card new Holiday( "Amy" ); card. greeting(); //Invoke a Holiday greeting() card=new Birthday( "Cindy", 17 ); card. greeting(); //Invoke a Birthday greeting () } c. Instantiate a Birthday object and assign to greeting() method in the Card Tester class as following code: Birthday bd = new Birthday( "Maria", 21 ); bd.greeting(); d. Compile and Run. Give the output. e. Rewrite your CardTester class with below program. class f int age; extends public { recipient = 1; age = years; } (String r, int years ) public void greeting () { } System.out.println("Dear System.out.println("Happy " + recipient + ", "); + "th Birthday "); 2. The following program demonstrates polymorphic behavior. Write, compile and run the following program. Give the output. import java.io.*; import java.awt.*; public abstract class Animal { private String name; public Animal (String nm) { name nm; } } public String getName() { } return(name); public abstract void speak(); public class Dog extends Animal { public Dog (String nm) super (nm); } public void speak () System.out.println("Woof"); } public class Cow extends Animal { public Cow (String nm) } super (nm); } public void speak() System.out.println("Moo"); } public class VariousAnimal { { public static void main(String[] args) Animal ref; Dog aDog = new Dog ("Lassie"); Cow aCow = new Cow ("Mabel"); ref=aCow; ref. speak(); ref=aDog; ref. speak(); Explain the polymorphic behavior. 3. Extend the program in exercise 2 by adding two more subclasses of Animal i.e. Snake and Cat where each has a method speak() which prints "Sss" and "Meow" respectively. 4. Modify the extended program created in Exercise 3 in this lab by creating an array of superclass objects and manipulate it by invoking the appropriate method for each subclass member to print the sound of each animal eg. dog, cat, snake and cow.
Expert Answer:
Related Book For
OCP Oracle Certified Professional Java SE 11 Developer Practice Tests Exam 1Z0 819 And Upgrade Exam
ISBN: 9781119696131
1st Edition
Authors: Scott Selikoff, Jeanne Boyarsky
Posted Date:
Students also viewed these programming questions
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
What is the difference between MouseListener and MouseAdapter? [3 marks] (b) Via suitable HTML, the compiled version of the following Java code is presented to the appletviewer application: import...
-
For the inexperienced marketer, the similar-but-different aspect of culture creates an illusion of similarity that usually does not exist. Discuss and give examples.
-
Label each of the following as a substance, a heterogeneous mixture, or a solution. a. Seawater b. Sulfur c. Fluorine d. Beach sand
-
Luzadis Company makes furniture using the latest automated technology. The company uses a job-order costing system and applies manufacturing overhead cost to products on the basis of machine-hours....
-
Using one of the online calculators, how large a correlation would you need for the relationships shown in Exercise 9.2 to be significant? (This will involve a bit of trial and error.) Calculate the...
-
Julie is considering three alternative investments of $10,000. Julie is in the 28% marginal tax bracket for ordinary income and 15% for qualifying capital gains in all tax...
-
8. Given the following code sequence: for (i=0; i <10; i++) { if (A[i] != 5) A[B[i]] += 1 else A[i] = B[i+1]} If the base address of arrays A and B are in $s1 and $s2 respectively and i, 5 and 1 are...
-
Mr. Ramli has agreed and signed the Sales and Purchase Agreement (SPA) with JP Construction Berhad to buy a bungalow unit that comes together with a gated and guarded security system. The said...
-
Which of the following factors would affect a buyers reservation price for a given good or service: social influence, the price of the good, or the cost of producing the item?
-
Design a lag-compensator for a unity feedback system with forward transmittance \[ \mathrm{G}(s)=\frac{\mathrm{K}}{(s+1)(s+3)(s+5)} \] to yield the following specifications: (i) peak overshoot \(\leq...
-
Identify whether a debit or credit results in the indicated change for each of the following accounts. a. To increase Land f. To decrease Prepaid Rent b. To decrease Cash g. To increase Notes Payable...
-
Indicate whether a debit or credit decreases the normal balance of each of the following accounts. a. Interest Payable e. Common Stock i. Dividends b. Service Revenue f. Prepaid Insurance j. Unearned...
-
A domestic boiler heating system is heating the outside air at 5 C up to 20 C with 80 percent first law efficiency. Determine the second law efficiency of this system. Draw the corresponding Sankey...
-
For each scenario, which statistical procedure is most appropriate (one sample mean, one sample proportion, two independent proportions, two independent means, two paired/dependent means, one-way...
-
Test your confidence in the following Project Decisions: SI. # Question 01 02 03 04 05 06 07 08 09 10 How many years did it take to construct the largest Egyptian Pyramid- Pyramid of Cheops? When was...
-
What is the purpose of a drilling machine ? Explain its working principle.
-
How drilling machines are classified?
-
Describe the quick return motion mechanism for a shaper.
Study smarter with the SolutionInn App