In: Computer Science
This question is about java program. Please show the output and the detail code and comment of the each question and each Class and interface.
And the output (the test mthod )must be all the true! Thank you!
Question1
Create a class Animal with the following UML
specification:
+-----------------------+
| Animal |
+-----------------------+
| - name: String |
+-----------------------+
| + Animal(String name) |
| + getName(): String |
| + getLegs(): int |
| + canFly(): boolean |
| + testAnimal(): void |(this is the
static method)
+-----------------------+
where the name instance variable stores a name for
the animal, the getLegs method returns as result
the animal's number of legs, and the canFly method
returns as result a boolean indicating whether the animal can fly
or not. The testAnimal method is static.
Should the getLegs method be abstract? Why or why
not?
Should the canFly method be abstract? Why or why
not?
Should the Animal class be abstract? Why or why
not?
What kinds of tests can you write inside the
testAnimal method?
Add the following code to your program to test the
Animal class:
public class Start {
public static void main(String[] args) {
Animal.testAnimal();
}
}
Question 2
Add a class Dog to your program. Dogs are animals.
The constructor for the Dog class takes the name
of the dog as argument. Dogs have four legs and cannot fly.
Do not forget to change the main method of the
Start class to run the unit tests of the new Dog
class.
Question 3
Add a class Bird to your program. Birds are
animals. The Bird class must have a private
instance variable called
numOfEggs which is an integer indicating how many
eggs the bird has. The constructor for Bird takes
as arguments a
name and a number of eggs. The Bird class has a
public method called getNumOfEggs that takes zero
arguments
and returns as result the bird's number of eggs. All birds have two
legs. Some birds can fly (for example magpies) and
some birds cannot fly (for example ostriches).
Do not forget to change the main method of the
Start class to run the unit tests of the new Bird
class.
Question 4
Add a class Magpie to your program. Magpies are
birds. The constructor for Magpie takes as
argument only a name.
Magpies always have 6 eggs. Magpies can fly.
Do not forget to change the main method of the
Start class to run the unit tests of the new
Magpie class.
Question 5
Add a class Ostrich to your program. Ostriches are
birds. The constructor for Ostrich takes as
argument only a
name. Ostriches always have 10 eggs. Ostriches cannot fly.
Do not forget to change the main method of the
Start class to run the unit tests of the new
Ostrich class.
Question 6
Add a class Pegasus to your program. Pegasi are
birds (a pegasus has the wings of a bird so for this lab we will
assume
that pegasi are birds). The constructor for
Pegasus takes as argument only a name. Pegasi have
four legs (not two legs
like other birds). Pegasi can fly.
Pegasi do not lay eggs so the getNumOfEggs method
of the Pegasus class should just print a message
"Pegasi do
not lay eggs!" and return zero as a result.
Do not forget to change the main method of the
Start class to run the unit tests of the new
Pegasus class.
Question 7
Add an interface Flyer with the following UML
specification:
+-----------------------+
| <<interface>> |
| Flyer |
+-----------------------+
| + getName(): String |
| + canFly(): boolean |
+-----------------------+
The Bird class implements the
Flyer interface.
Change the main method of the
Start class to tests magpies, ostriches, and
pegasi when viewed through the Flyer
interface.
Can you test objects from the Animal,
Dog, or Bird classes through the
Flyer interface? Why or why not?
Question 8
Add a class Airplane that implements the Flyer interface. The
constructor for Airplane takes as argument only
a
name. An airplane is not an animal.
Do not forget to change the main method of the
Start class to run the unit tests of the new
Airplane class. Also
make sure to test it when viewed through the Flyer
interface.
Question 9
Add a method isDangerous to the
Flyer interface. This method returns a boolean as
result, indicating whether the
corresponding object is dangerous or not. Only ostriches are
dangerous.
Which classes need to be changed? Which classes do not need to be
changed?
Do not forget to add new tests for the isDangerous
method.
Question1
Should the getLegs method be abstract? Why or why not?
The getLegs method should be abstract because we don't know what kind of Animal it is and how many legs it has at this point. It can be Dog or Bird.
Should the canFly method be abstract? Why or why
not?
Yes, the canFly() method should be abstract as the Animal can be a bird which can fly or a dog which cannot fly.
Should the Animal class be abstract? Why or why
not?
Yes, the Animal class should be abstract because it contains abstract methods.
What kinds of tests can you write inside the
testAnimal method?
public static void testAnimal(Animal a)
{
if(a.canFly() == true) // test if
the Animal is a bird
System.out.println("It is a
bird");
if(a.getLegs() == 4) // test if the
Animal is a dog
System.out.println("It can be a
dog");
}
Question 2
class Dog extends Animal
{
public Dog(String name)
{
super(name);
}
public int getLegs()
{
return 4;
}
public boolean canFly()
{
return false;
}
}
Question 3
class Bird extends Animal
{
private int numOfEggs;
public Bird(String name, int numOfEggs)
{
super(name);
this.numOfEggs = numOfEggs;
}
public int getNumOfEggs()
{
return numOfEggs;
}
public int getLegs()
{
return 2;
}
}
Question 4
class Magpie extends Bird
{
public Magpie(String name)
{
super(name,6);
}
public int getNumOfEggs()
{
return 6;
}
public boolean canFly()
{
return true;
}
}
Question 5
class Ostrich extends Bird
{
public Ostrich(String name)
{
super(name,10);
}
public int getNumOfEggs()
{
return 10;
}
public boolean canFly()
{
return false;
}
}
Question 6
class Pegasus extends Bird
{
public Pegasus(String name)
{
super(name,0);
}
public int getNumOfEggs()
{
System.out.println("Pegasi do not
lay eggs!");
return 0;
}
public boolean canFly()
{
return true;
}
public int getLegs()
{
return 4;
}
}
Question 7
interface Flyer
{
public String getName();
public boolean canFly();
}
Animal, Dog and Bird classes are not concrete . They are abstract so cannot be tested with interface Flyer.
Question 8
class Airplane implements Flyer
{
private String name;
public Airplane(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public boolean canFly()
{
return true;
}
}
class Start
{
public static void main (String[] args)
{
Flyer a = new Airplane("Boeing 734");
}
}
Question 9
interface Flyer
{
public String getName();
public boolean canFly();
public boolean isDangerous();
}
Which classes need to be changed?
Dog, Ostrich, Pegasus, Magpie, Airplane
Which classes do not need to be changed?
Bird
Complete Code
interface Flyer
{
public String getName();
public boolean canFly();
public boolean isDangerous();
}
abstract class Animal implements Flyer
{
private String name;
public Animal(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public abstract int getLegs();
public abstract boolean canFly();
public static void testAnimal(Animal a)
{
if(a.canFly() == true)
System.out.println("It is a
bird");
if(a.getLegs() == 4)
System.out.println("It can be a
dog");
}
}
class Dog extends Animal
{
public Dog(String name)
{
super(name);
}
public int getLegs()
{
return 4;
}
public boolean canFly()
{
return false;
}
public boolean isDangerous()
{
return true;
}
}
abstract class Bird extends Animal
{
private int numOfEggs;
public Bird(String name, int numOfEggs)
{
super(name);
this.numOfEggs = numOfEggs;
}
public int getNumOfEggs()
{
return numOfEggs;
}
public int getLegs()
{
return 2;
}
}
class Magpie extends Bird
{
public Magpie(String name)
{
super(name,6);
}
public int getNumOfEggs()
{
return 6;
}
public boolean canFly()
{
return true;
}
public boolean isDangerous()
{
return true;
}
}
class Ostrich extends Bird
{
public Ostrich(String name)
{
super(name,10);
}
public int getNumOfEggs()
{
return 10;
}
public boolean canFly()
{
return false;
}
public boolean isDangerous()
{
return true;
}
}
class Pegasus extends Bird
{
public Pegasus(String name)
{
super(name,0);
}
public int getNumOfEggs()
{
System.out.println("Pegasi do not
lay eggs!");
return 0;
}
public boolean canFly()
{
return true;
}
public int getLegs()
{
return 4;
}
public boolean isDangerous()
{
return false;
}
}
class Airplane implements Flyer
{
private String name;
public Airplane(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public boolean canFly()
{
return true;
}
public boolean isDangerous()
{
return true;
}
}
class Start
{
public static void main (String[] args)
{
// Testing all classes
Flyer a = new Airplane("Boeing 734");
System.out.println(a.getName());
System.out.println(a.isDangerous());
Flyer dog = new Dog("Buzzo");
System.out.println(dog.getName());
System.out.println(dog.isDangerous());
System.out.println(dog.canFly());
Flyer magpie = new Magpie("maggi");
System.out.println(magpie.getName());
System.out.println(magpie.isDangerous());
System.out.println(magpie.canFly());
Flyer ostrich = new Ostrich("ostri");
System.out.println(ostrich.getName());
System.out.println(ostrich.isDangerous());
System.out.println(ostrich.canFly());
Flyer pegasus = new Pegasus("pegasi");
System.out.println(pegasus.getName());
System.out.println(pegasus.isDangerous());
System.out.println(pegasus.canFly());
}
}
Output:
Boeing 734 true Buzzo true false maggi true true ostri true false pegasi false true
Do ask if any doubt. Please upvote.