Question

In: Computer Science

This question is about java program. Please show the output and the detail code and comment...

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.

Solutions

Expert Solution

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.


Related Solutions

Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
Please do it in C++. Please comment on the code, and comments detail the run time...
Please do it in C++. Please comment on the code, and comments detail the run time in terms of total operations and Big O complexities. 1. Implement a class, SubstitutionCipher, with a constructor that takes a string with the 26 uppercase letters in an arbitrary order and uses that as the encoder for a cipher (that is, A is mapped to the first character of the parameter, B is mapped to the second, and so on.) Please derive the decoding...
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1....
Please create a Java Eclipse code and show output and explanation. MUST USE EXCEPTION! Step 1. Ask users how much money was spent on an orange. Step 2. Then ask the user how many oranges they purchased Step 3. Lastly calculate the price for each orange purchased.
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {...
CONVERT CODE FROM JAVA TO C# PLEASE AND SHOW OUTPUT import java.util.*; public class TestPaperFolds {    public static void main(String[] args)    {        for(int i = 1; i <= 4; i++)               //loop for i = 1 to 4 folds        {            String fold_string = paperFold(i);   //call paperFold to get the String for i folds            System.out.println("For " + i + " folds we get: " + fold_string);        }    }    public static String paperFold(int numOfFolds)  ...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
Please code by Java. The following preemptive program with this problem after sample output(fill random number,...
Please code by Java. The following preemptive program with this problem after sample output(fill random number, recursive search, non-recursive search, exit), You just need to modify this code to add sorting and time calculations. Problem Description: Expand on Lab3 by adding methods that implement Bubble Sort, Selection Sort, Insertion Sort, Merge Sort and Quick Sort for displaying the sorted integers from the randomly generated array. NOTE: Create separate methods for each type of sort. Run Tests for all 4 types...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an...
Create a JAVA code program: Huffman code will be converted to its text equivalent Output an error message if the input cannot be converted I can give an thumbs up! :)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT