Lab Assignment 09
Course Code: CSE111
Course Title: Programming Language II
Topic: Polymorphism
Number of Tasks: 10
Task 1
1.a. Write the Pokemon class so that the following code generates the output below:
Driver Code Output
public class PokemonTester{ Pikachu attacks with a basic move!
public static void main(String[] args){
Pikachu uses Thunderbolt!
Pokemon pikachu = new Pokemon("Pikachu");
[Link](); Pokemon uses Iron Tail with power 90!
[Link]("Thunderbolt");
[Link]("Iron Tail", 90);
}
}
1.b. What type of polymorphism is depicted in the code above?
Task 2
Write the Mango and the Jackfruit classes derived from Fruit class so that the following
code generates the output below:
Parent Class
public class Fruit{
private boolean formalin = false;
private String name = "";
public Fruit(boolean formalin, String name){
[Link] = formalin;
[Link] = name;
}
public String getName(){
return name;
}
public boolean hasFormalin(){
return formalin;
}
}
Driver Code Output
public class FruitTester{ ----Printing Detail-----
public static void testFruit(Fruit f){
Do not eat the Mango.
[Link]("----Printing Detail-----");
if([Link]()){ Mangos are bad for you
[Link]("Do not eat the "+[Link]()+".");
----Printing Detail-----
[Link](f);
}else{ Eat the Jackfruit.
[Link]("Eat the "+[Link]()+".");
Jackfruits are good for you
[Link](f);
}
}
public static void main(String [] args){
Mango m = new Mango();
testFruit(m);
Jackfruit j = new Jackfruit();
testFruit(j);
}
}
Task 3
Write the CSEStudent and CSE111Student classes derived from Student class so that the
following code generates the output below:
Parent Class
public class Student{
public String msg = "I love BU";
public String shout(){
return msg;
}
}
Driver Code Output
public class StudentTester{ I love BU
public static void printShout(Student s){ I want to transfer to CSE
[Link]("------------------"); I love Java Programming
[Link]([Link]); ------------------
[Link]([Link]()); I love BU
} I love BU
public static void main(String [] args){ ------------------
Student s = new Student(); I love BU
CSEStudent cs = new CSEStudent(); I want to transfer to CSE
CSE111Student cs111 = new CSE111Student(); ------------------
[Link]([Link]); I love BU
[Link]([Link]); I love Java Programming
[Link]([Link]);
printShout(s);
printShout(cs);
printShout(cs111);
}
}
Task 4
Write the PlatinumCard and SignatureCard classes derived from CreditCard class so that
the following code generates the output below.
Note: Platinum card users initially have 100 reward points and will get 2 reward points
for spending 100 taka each. Signature card users initially have 200 reward points and
will get 4 reward points for spending 100 taka each. Signature card users are allowed to
bring upto 5 companions at lounges.
Parent Class
public class CreditCard {
public String cardHolder;
public String accountNo;
public int rewardPoints;
public CreditCard(String cardHolder, String accountNo, int rewardPoints){
[Link] = cardHolder;
[Link] = accountNo;
[Link] = rewardPoints;
}
public void cardDetails(){
[Link]("Card Holder Name: " + cardHolder);
[Link]("Account Number: " + accountNo);
[Link]("Reward point gained: " + rewardPoints);
}
}
Driver Code Output
public class CardTester { ================
public static void main(String[] args) { Previous Reward Points: 100
CreditCard card1 = new PlatinumCard("Ali", "345 127"); Reward points after spending 200
CreditCard card2 = new SignatureCard("Rahul", "514 123"); taka: 104
================
CreditCard card3 = new SignatureCard("Rohan", "147 965"); Card Holder Name: Ali
CreditCard [] cards = {card1, card2, card3}; Account Number: 345 127
for (int i = 0; i<[Link]; i++) Reward point gained: 104
{ ================
[Link]("================"); Previous Reward Points: 200
if (cards[i] instanceof SignatureCard) Reward points after spending 500
taka: 220
{
================
SignatureCard new_card = (SignatureCard) cards[i]; Card Holder Name: Rahul
new_card.spendCash(500); Account Number: 514 123
} Reward point gained: 220
else if (cards[i] instanceof PlatinumCard) Possible Number of Companions for
{ Lounge: 5
PlatinumCard new_card = (PlatinumCard) cards[i]; ================
Previous Reward Points: 200
new_card.spendCash(200); Reward points after spending 500
} taka: 220
[Link]("================"); ================
cards[i].cardDetails(); Card Holder Name: Rohan
} Account Number: 147 965
} Reward point gained: 220
} Possible Number of Companions for
Lounge: 5
Task 5
Design a set of classes for a Fantasy Game Character System. There is a parent class
called Character. From it, there are two different child classes: Warrior and Mage.
Additionally, there is a subclass called Paladin that extends Warrior.
Each character has:
● name (String)
● level (int)
Lastly, you need to Override the .equals() method inside the parent class
Parent Class
public class Character {
public String name;
public int level;
public Character(String name, int level) {
[Link] = name;
[Link] = level;
}
public void specialMove() {
[Link]("Character uses a generic move.");
}
// Override the .equals() method
}
Driver Code Output
public class GameTester { Arthur unleashes a holy strike!
public static void main(String[] args) { Merlin casts a powerful fireball!
Character c1 = new Paladin("Arthur", 10); Leon performs a heavy sword slash!
Character c2 = new Mage("Merlin", 12); Arthur unleashes a holy strike!
Character c3 = new Warrior("Leon", 10); c3 equals w1? true
c2 equals m1? false
[Link]();
[Link]();
[Link]();
if (c1 instanceof Paladin) {
Paladin p = (Paladin) c1;
[Link]();
}
Warrior w1 = new Warrior("Leon", 10);
[Link]("c3 equals w1? " + [Link](w1));
Mage m1 = new Mage("Merlin", 15);
[Link]("c2 equals m1? " + [Link](m1));
}
}
Task 6
Write the Garage, Bike and Car class. Car, Bike are child classes of Vehicle class. But
Garage is neither a parent nor a child class. The Garage class has two arrays as instance
variables called cars and bikes that can store Car and Bike objects.
Hint: In this task you’ll need to use the instanceof keyword and downcasting.
Vehicle Class
public class Vehicle {
private String brand;
private int year, wheels;
public Vehicle(String b, int y){
[Link] = b;
[Link] = y;
}
public String getBrand(){
return [Link];
}
public int getYear(){
return [Link];
}
public void setWheels( int w ){
[Link] = w;
}
public int getWheels(){
return [Link];
}
public String toString(){
return "Brand: "+[Link]+", Year: "+[Link]+", Wheels: "+[Link];
}
}
DRIVER CODE OUTPUT
Garage g = new Garage(2, 3); Welcome to the Garage!
[Link]("==========0==========="); Car Capacity: 2
Vehicle vC1 = new Car("Ford", "Mustang", 2022, 2, 4, Bike Capacity: 3
false); ==========0===========
Vehicle vC2 = new Car("Tesla", "Model S", 2025, 4, 4, ==========1===========
true); Car Brand: Ford, Year: 2022,
Vehicle vC3 = new Car("Reliant", "Robin", 1981, 2, 3, Wheels: 4, Model: Mustang,
false); Doors: 2, AI: false
[Link]("==========1==========="); ==========2===========
[Link](vC1); A Ford CAR has been added to the
[Link]("==========2==========="); Garage
[Link](vC1); A Tesla CAR has been added to
[Link](vC2); the Garage
[Link](vC3); Can't add more Cars! Capacity: 2
[Link]([Link][1]); Car Brand: Tesla, Year: 2025,
[Link]("==========3==========="); Wheels: 4, Model: Model S,
[Link][0].startAutoPilot(); Doors: 4, AI: true
[Link][1].startAutoPilot(); ==========3===========
[Link]("==========4==========="); Ford:Mustang has NO AutoPilot
Vehicle vB1 = new Bike("Honda", "Gold Wing", 2022, 3, Tesla:Model S AutoPilot Started
true); ==========4===========
[Link](vB1); Bike Brand: Honda, Year: 2022,
[Link]( vB1 ); Wheels: 3, Model: Gold Wing,
[Link]("==========5==========="); SideCar: true
Vehicle vB2 = new Bike("Royal Enfield", "Classic A Honda BIKE has been added to
350", 2021, 2, false); the Garage
[Link]( vB2 ); ==========5===========
[Link]([Link][1]); A Royal Enfield BIKE has been
[Link]("==========6==========="); added to the Garage
Vehicle vB3 = new Bike("Harley-Davidson", "Street Bike Brand: Royal Enfield, Year:
750", 2022, 2, false); 2021, Wheels: 2, Model: Classic
[Link]( vB3 ); 350, SideCar: false
Vehicle vB4 = new Bike("Yamaha", "MT-15", 2023, 2, ==========6===========
false); A Harley-Davidson BIKE has been
[Link]( vB4 ); added to the Garage
[Link]("=========7============"); Can't add more bikes! Capacity:
[Link][0].doAWheelie(); 3
[Link][1].doAWheelie(); =========7============
Wheelie Failed. Honda:Gold Wing
has SideCar
Royal Enfield:Classic 350 is
doing Wheelie!!
Task 7
1 public class Gandalf {
2 public void method1(){
3 [Link]("Gandalf 1");
4 }
5
6 public void method2(){
7 [Link]("Gandalf 2");
8 method1();
9 }
10 }
11 public class Bilbo extends Gandalf{
12 public void method1(){
13 [Link]("Bilbo 1");
14 }
15 }
16 public class Gollum extends Gandalf{
17 public void method3(){
18 [Link]("Gollum 3");
19 }
20 }
21 public class Frodo extends Bilbo{
22 public void method1(){
23 [Link]("Frodo 1");
24 super.method1();
25 }
26
27 public void method3(){
28 [Link]("Frodo 3");
29 }
30 }
Assuming the following variables have been defined:
Gandalf var1 = new Frodo();
Gandalf var2 = new Bilbo();
Gandalf var3 = new Gandalf();
Object var4 = new Bilbo();
Bilbo var5 = new Frodo();
Object var6 = new Gollum();
In the table below,
● The output produced by the statement in the left-hand column,
should be written in the right-hand column
● If the statement produces more than one line of output, indicate
the line breaks with slashes as in "a/b/c" to indicate three
lines of output with "a" followed by "b" followed by "c".
● If the statement causes an error, fill in the right-hand column
with either the phrase "compiler error" or "runtime error" to
indicate when the error would be Detected.
Statement Output
1 var1.method1();
2 var2.method1();
3 var4.method1();
4 var6.method1();
5 var1.method2();
6 var3.method2();
7 var4.method2();
8 var5.method2();
9 var6.method2();
10 ((Frodo)var4).method3();
11 ((Frodo)var6).method2();
12 ((Gollum)var1).method3();
13 ((Gollum)var4).method1();
14 ((Gandalf)var1).method2();
15 ((Frodo)var4).method1();
16 ((Gollum)var6).method2();
17 ((Gandalf)var2).method1();
18 ((Bilbo)var6).method2();
19 ((Frodo)var1).method3();
20 ((Gandalf)var5).method3();
Task 8
1 public class Sue {
2 void method1() {
3 [Link]("sue 1");
4 }
5 void method3() {
6 [Link]("sue 3");
7 }
8 }
9
10 public class Blue {
11 void method1() {
12 [Link]("blue 1");
13 method3();
14 }
15 void method3() {
16 [Link]("blue 3");
17 }
18 }
19
20 public class Moo extends Blue {
21 void method2() {
22 super.method3();
23 [Link]("moo 2");
24 this.method3();
25 }
26 void method3() {
27 [Link]("moo 3");
28 }
29 }
30
31 public class Crew extends Moo {
32 void method1() {
33 [Link]("crew 1");
34 }
35 void method3() {
36 [Link]("crew 3");
37 }
38 }
Assuming the following variables have been defined:
Moo var1 = new Crew();
Blue var2 = new Moo();
Object var3 = new Sue();
Sue var4 = new Sue();
Blue var5 = new Crew();
Blue var6 = new Blue();
In the table below,
● The output produced by the statement in the left-hand column,
should be written in the right-hand column
● If the statement produces more than one line of output, indicate
the line breaks with slashes as in "a/b/c" to indicate three
lines of output with "a" followed by "b" followed by "c".
● If the statement causes an error, fill in the right-hand column
with either the phrase "compiler error" or "runtime error" to
indicate when the error would be detected.
Statement Output
1 var1.method1();
2 var2.method1();
3 var3.method1();
4 var4.method1();
5 var5.method1();
6 var6.method1();
7 var1.method3();
8 var2.method3();
9 var3.method3();
10 ((Blue)var1).method1();
11 ((Crew)var1).method2();
12 ((Sue)var1).method3();
13 ((Blue)var3).method1();
14 ((Crew)var3).method1();
15 ((Sue)var3).method3();
16 ((Moo)var2).method2();
17 ((Crew)var3).method2();
18 ((Moo)var5).method2();
19 ((Moo)var6).method2();
20 ((Moo)var2).method1();
Task 9
1 public class Foo {
2 String name = "foo";
3 public void call1() {
4 [Link]("Foo 1");
5 }
6 public void call2() {
7 call1();
8 [Link]("Foo 2");
9 }
10 }
11
12 public class Bar extends Foo {
13 public void call2() {
14 [Link]("Bar 2");
15 }
16 public void call3() {
17 [Link]("Bar 3");
18 }
19 }
20
21 public class Buzz extends Bar {
22 String name = "Buzz";
23 public void call1() {
24 [Link]("Buzz 1");
25 }
26 public void call4() {
27 call3();
28 [Link]("Buzz 4");
29 }
30 }
31 public class Bux extends Foo {
32 String name = "Bux";
33 public void call1() {
34 [Link]("Bux 1");
35 }
36 public void call3() {
37 [Link]("Bux 3");
38 }
39 }
Assuming the following variables have been defined:
Foo foo1 = new Foo();
Bar bar1 = new Bar();
Bux bux1 = new Bux();
Foo foo2 = new Buzz();
Bar bar2 = new Buzz();
Object obj1 = new Foo();
In the table below,
● The output produced by the statement in the left-hand column,
should be written in the right-hand column
● If the statement produces more than one line of output, indicate
the line breaks with slashes as in "a/b/c" to indicate three
lines of output with "a" followed by "b" followed by "c".
● If the statement causes an error, fill in the right-hand column
with either the phrase "compiler error" or "runtime error" to
indicate when the error would be detected.
Statement Output
1 bar1.call1();
2 foo2.call1();
3 foo2.call2();
4 bar2.call3();
5 [Link]([Link]);
6 [Link]([Link]);
7 [Link](((Buzz)bar2).name);
8 ((Buzz)bar1).call4();
9 ((Bar)foo1).call3();
10 ((Foo)bux1).call1();
11 ((Bux)foo1).call1();
12 bux1.call1();
13 bux1.call2();
14 ((Foo)foo2).call2();
15 ((Buzz)obj1).call3();
16 ((Buzz)obj1).call2();
17 ((Bux)foo2).call2();
18 ((Buzz)obj1).call1();
19 [Link]([Link]);
20 [Link](((Bux)foo2).name);
Task 10
1 public class Caramel extends SilkOreo{
2 String texture = "Softy";
3 public void method1() {
4 [Link]("Caramel m1");
5 }
6 public void method4() {
7 [Link]("Caramel m4");
8 }
9 public String toString(){
10 method2();
11 return "Caramel is "+ texture;
12 }
13 }
14 public class Chocolate{
15 String texture = "Chocolaty";
16 public void method1() {
17 method2();
18 [Link]("Chocolate m1");
19 }
20 public void method2() {
21 [Link]("Chocolate m2");
22 }
23 public String toString(){
24 method2();
25 return "Chocolate is "+ texture;
26 }
27 }
28 public class DairyMilk extends Chocolate{
29 String texture = "Yummy";
30 public void method2() {
31 [Link]([Link]);
32 [Link]("DairyMilk m2");
33 }
34 public void method3() {
35 [Link]("DairyMilk m3");
36 }
37 }
38 public class KitKat extends Chocolate{
39 String texture = "Crunchy";
40 public void method1() {
41 [Link]("KitKat m1");
42 }
43 public void method4() {
44 [Link]("KitKat m4");
45 }
46 public String toString(){
47 method2();
48 return "KitKat is "+ texture;
49 }
50 }
51 public class SilkOreo extends DairyMilk{
52 String texture = "Silky";
53 public void method1() {
54 super.method1();
55 [Link]("SilkOreo m1");
56 }
57 public void method3() {
58 [Link]("SilkOreo m3");
59 [Link](this);
60 }
61 }
Assuming the following variables have been defined:
Chocolate choco1 = new Chocolate();
KitKat kit = new KitKat();
DairyMilk dairyMilk1 = new DairyMilk();
DairyMilk dairyMilk2 = new SilkOreo();
Object obj1 = new DairyMilk();
Object obj2 = new KitKat();
Chocolate caramel1 = new Caramel();
In the table below,
● The output produced by the statement in the left-hand column,
should be written in the right-hand column
● If the statement produces more than one line of output, indicate
the line breaks with slashes as in "a/b/c" to indicate three
lines of output with "a" followed by "b" followed by "c".
● If the statement causes an error, fill in the right-hand column
with either the phrase "compiler error" or "runtime error" to
indicate when the error would be detected.
Statement Output
1 choco1.method1();
2 dairyMilk1.method1();
3 dairyMilk2.method4();
4 caramel1.method1();
5 [Link](caramel1);
6 [Link]([Link]);
7 ((Chocolate)kit).method2();
8 ((SilkOreo)dairyMilk2).method3();
9 ((DairyMilk)kit).method2();
10 ((Chocolate)kit).method3();
11 ((Chocolate)dairyMilk2).method1();
12 ((Chocolate)obj1).method2();
13 ((Caramel)obj1).method2();
14 ((SilkOreo)obj2).method3();
15 [Link](((Object)choco1).toString());
16 [Link](((Chocolate)kit).texture);