1 public class Bird 2 { 3 private boolean canFly; 4 private double wingSpan; 5 6
ID: 3799586 • Letter: 1
Question
1 public class Bird
2 {
3 private boolean canFly;
4 private double wingSpan;
5
6 public Bird()
7 {
8 canFly = yes;
9 }
10 public Bird(boolean canFly, double
wingSpan)
11 {
12 canFly = this.canFly;
13 wingSpan = this.wingSpan;
14 }
15 public int setCanFly(boolean canFly)
16 {
17 this.canFly = canFly;
18 }
19 public void setWingSpan(double
wingSpan)
20 {
21 this.wingSpan = wingSpan;
22 }
23 public void getCanFly()
24 {
25 return canFly;
26 }
27 public void layEggs()
28 {
29 System.out.println("Laying Eggs!");
30 }
31 public void eat()
32 {
33 System.out.println("Yummy bugs!");
34 }
35 }
1 public class Penguin isa Bird
2 {
3 private boolean canSwim;
4
5 public void Penguin(double wingSpan)
6 {
7 this.canSwim = true;
8 super(false, wingspan);
9 }
10
11 public void swim()
12 {
13 System.out.println("Diving deep!");
14 }
15
16 public void eat()
17 {
18 System.out.println("Yummy fish!");
19 }
20
21 public void eat(String food)
22 {
23 System.out.println(“Yummy “ + food);
24 }
25 }
Fix all the compilation errors.
Answer the following questions about the code above.
1. Name the superclass. : Bird
2. Name the subclass. : Penguin
3. Explain what line #7 in the Penguin class does?
4. Is line #16 of the Penguin class an example of method overloading or overriding? Explain your answer.
5. Is line #21 of the Penguin class an example of method overloading or overriding? Explain your answer.
6. Name all the attributes of a Bird.
7. Name all the attributes of a Penguin.
8. List all the methods you can call on a Bird object.
9. List all the methods you can call on a Penguin object.
10. Which of the following statements are legal?
a. Bird eagle = new Bird(6.2);
b. Bird sparrow = new Bird();
c. Penguin chilly = new Penguin(30);
d. Penguin emperor = new Penguin(false, 33.4);
e. Bird bluejay = new Bird(true, 14);
11. Which version of eat will get executed for each code segment. State the line number or if the code is not valid simply write “not valid”.
a. Penguin happyFeet = new Penguin(28);
happyFeet.eat(“fish”);
b. Penguin skipper = new Penguin(31.6);
skipper.eat();
c. Bird kowalski = new Penguin(35);
kowalski.eat();
d. Bird tweety = new Bird();
tweety.eat();
e. Penguin rico = new Bird();
rico.eat();
Explanation / Answer
HI, Please find my code/answer.
Please let me know in case of any issue
public class Bird
{
private boolean canFly;
private double wingSpan;
public Bird()
{
canFly = true;
}
public Bird(boolean canFly, double wingSpan)
{
canFly = this.canFly;
wingSpan = this.wingSpan;
}
public void setCanFly(boolean canFly)
{
this.canFly = canFly;
}
public void setWingSpan(double
wingSpan)
{
this.wingSpan = wingSpan;
}
public boolean getCanFly()
{
return canFly;
}
public void layEggs()
{
System.out.println("Laying Eggs!");
}
public void eat()
{
System.out.println("Yummy bugs!");
}
}
public class Penguin extends Bird {
private boolean canSwim;
public Penguin(double wingSpan){
super(false, wingSpan);
this.canSwim = true;
}
public void swim()
{
System.out.println("Diving deep!");
}
public void eat()
{
System.out.println("Yummy fish!");
}
public void eat(String food)
{
System.out.println("Yummy " + food);
}
}
1. Name the superclass. : Bird
Ans : Object class
2. Name the subclass. : Penguin
Ans: None
3. Explain what line #7 in the Penguin class does?
this.canSwim = true; this line assigning 'true' to instance variable 'canSwim'
4. Is line #16 of the Penguin class an example of method overloading or overriding? Explain your answer.
THis is method overriding, because method with same signature is available in Parent class(Bird)
5. Is line #21 of the Penguin class an example of method overloading or overriding? Explain your answer.
This is method oveloading. For overriding, method with same signature should be available in parent class.
Here method name is same but it is taking an argument.
6. Name all the attributes of a Bird.
canFly;
wingSpan;
7. Name all the attributes of a Penguin.
canSwim
8. List all the methods you can call on a Bird object.
setCanFly
setWingSpan
getCanFly
layEggs
9. List all the methods you can call on a Penguin object.
setCanFly
setWingSpan
getCanFly
layEggs
eat
swim
eat(String food)
10. Which of the following statements are legal?
a. Bird eagle = new Bird(6.2);
Not legal : No constructor with one parameter
b. Bird sparrow = new Bird();
Legal, default constructor
c. Penguin chilly = new Penguin(30);
Legal
d. Penguin emperor = new Penguin(false, 33.4);
Not legal, no constructor that takes two parameters
e. Bird bluejay = new Bird(true, 14);
Legal
11. Which version of eat will get executed for each code segment. State the line number or if the code is not valid simply write “not valid”.
a. Penguin happyFeet = new Penguin(28);
happyFeet.eat(“fish”);
Penguin's class: 21 public void eat(String food)
b. Penguin skipper = new Penguin(31.6);
skipper.eat();
Penguin's class: 16 public void eat()
c. Bird kowalski = new Penguin(35);
kowalski.eat();
Penguin's class: 16 public void eat()
d. Bird tweety = new Bird();
tweety.eat();
Bird's class : 31 public void eat()
e. Penguin rico = new Bird();
rico.eat();
Not valid
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.