Guys I have some problems with this code. It wouldn\'t compile. public class Dri
ID: 3568017 • Letter: G
Question
Guys I have some problems with this code. It wouldn't compile.
public class Driver
{
public static void main(String[] args)
{
Pet p = new Pet("Terrance");
p.printInfo();
System.out.println();
Mammal m = new Mammal();
m.printInfo();
m.setName("Roxie");
m.printInfo();
System.out.println();
Fish f = new Fish("red");
f.printInfo();
f.setName("Shark");
f.printInfo();
System.out.println();
Dog d = new Dog("Watson", "Basset");
d.printInfo();
}
}
----------------------------------------
public class Pet
{
protected String name;
public Pet(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String inName)
{
name = inName;
}
public void printInfo()
{
System.out.println("The pet's name is " + name);
}
}
---------------------------------
public class Fish extends Pet
{
protected String color;
public Fish(String name, String color)
{
super(name);
this.color = color;
}
public void printInfo()
{
super.printInfo();
System.out.println("The fish is " + color + "and its name is " + getName());
}
}
----------------------------------
public class Mammal extends Pet
{
public Mammal(String name)
{
super(name);
}
public void setName(String inName)
{
name = inName;
}
public void printInfo()
{
//System.out.println("The pet's name is " + name);
}
}
--------------------------------
public class Dog extends Mammal
{
public Dog(String name)
{
super(name);
}
public void printInfo()
{
System.out.println("The dog's name is " + name + "and " + name);
}
}
-----------------------------
I need help please!
----------------------------
This is the output that should be printed:
Explanation / Answer
//Modfied the super class and subclass as per
//the requirements .
//Pet.java
public class Pet
{
protected String name;
public Pet(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String inName)
{
name = inName;
}
public void printInfo()
{
System.out.print("The pet's name is " + name);
}
}
-------------------------------------------------------------------------------
//Mammal.java
public class Mammal extends Pet
{
public Mammal(String name)
{
super(name);
}
public void setName(String inName)
{
name = inName;
}
//Call super class Pet getName() method instead of calling printInfo
//to print name of the mammel.
public void printInfo()
{
super.printInfo();
}
}
-------------------------------------------------------------------------------
//Dog.java
public class Dog extends Mammal
{
private String type;
//In this Constructor,order of the parameters are interchanged.
//to match with the output.
public Dog(String name,String type)
{
super(name);
this.type=type;
}
public void printInfo()
{
//call super call Mammal getName that inturn
//calls its super class Pet getName print name of thd Dog
//and print its type here.
System.out.print("The dog's name is "+getName()+" and it's " + type);
}
}
-------------------------------------------------------------------------------
//Driver.java
public class Driver
{
public static void main(String[] args)
{
Pet p = new Pet("Terrance");
p.printInfo();
System.out.println();
Mammal m = new Mammal("fluffy");
m.printInfo();
//print a new line
System.out.println();
m.setName("Roxie");
m.printInfo();
System.out.println();
Fish f = new Fish("red","swimmy");
f.printInfo();
//print a new line
System.out.println();
f.setName("Shark");
f.printInfo();
System.out.println();
Dog d = new Dog("Watson", "Basset");
d.printInfo();
}
}
-------------------------------------------------------------------------------
Sample output:
The pet's name is Terrance
The pet's name is fluffy
The pet's name is Roxie
The fish is red and its name is swimmy
The fish is red and its name is Shark
The dog's name is Watson and it's Basset
Hope this would helps you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.