Question

In: Computer Science

This is the question about the java problem, please give the detail comment and code of...

This is the question about the java problem, please give the detail comment and code of each class.

Please write tests for all the code of all the classes

Thank you

Create a class Mammal with the following UML diagrams:
+---------------------------------+
| Mammal |
+---------------------------------+
| - name: String |
+---------------------------------+
| + Mammal(String name) |
| + getName(): String |
| + isCookable(): boolean |
| + testMammal(): void | (this method is static )
+---------------------------------+
The isCookable method of the Mammal class returns a boolean indicating whether the mammal can be cooked or not: some mammals can be cooked and some mammals cannot be cooked so for safety reasons the isCookable method just prints a message "Do not cook this!" and returns false (because the isCookablemethod must return a boolean).
Add a class Human to your program. A human is a mammal. The constructor for the Human class takes no argument. All humans are named "Alice" and humans cannot be cooked (the isCookable method must not print any message though, it simply returns false).
Add a class Rabbit to your program. A rabbit is a mammal. The Rabbit class has a private instance variable weight of type double that describes the weight of the rabbit, and a getWeight method. The constructor for the Rabbit class takes the rabbit’s name and the rabbit’s weight as arguments. A rabbit can be cooked.
Add a class EuropeanRabbit to your program. European rabbit is a species of rabbit. The EuropeanRabbitclass has two constructors: the first constructor takes the European rabbit’s name and the European rabbit’s weight as arguments; the second constructor only takes the European rabbit’s name as argument and always uses 2.0 as the European rabbit’s weight. The second constructor must use the first constructor.
Add a class LapinSautéChasseur to your program. Lapin sauté chasseur is a kind of European rabbit. The constructor for the LapinSautéChasseur class takes no argument. Lapin sauté chasseur is always named "Delicious" and has a weight of 0.5.
Add a class FrankTheRabbit to your program. Frank The Rabbit is a kind of rabbit. The constructor for theFrankTheRabbit class takes no argument. Frank The Rabbit is always named "Frank", has a weight of 100.0, and cannot be cooked.
Add a class Start to your program to test all your classes.
Question 2
Add a class CastIronPot to your program with the following UML diagram:
+---------------------------------+
| CastIronPot |
+---------------------------------+
| - rabbit: Rabbit |
+---------------------------------+
| + CastIronPot(Rabbit rabbit) |
| + getRabbit(): Rabbit |
| + testCastIronPot(): void |(this method is static)
+---------------------------------+
In the testCastIronPot method, create a lapin sauté chasseur called lsc1, then create a cast iron pot with this lapin sauté chasseur in it. Then get the lapin sauté chasseur from the cast iron pot and store it into a local variable called lsc2 of type LapinSautéChasseur. Use the == operator to check that lsc1 and lsc2 are the same lapin sauté chasseur.

Solutions

Expert Solution

/**********************Mammal.java******************/


public class Mammal {

   private String name;

   public Mammal(String name) {
       super();
       this.name = name;
   }

   public String getName() {
       return name;
   }

   public boolean isCookable() {

       System.out.println("Do not cook this!");
       return false;
   }

   public static void testMammal() {

      
   }
}
/*****************************Human.java****************/


public class Human extends Mammal {

   public Human() {
       super("Alice");
   }

   @Override
   public boolean isCookable() {

       return false;
   }

}
/*******************************Rabbit.java***************************/


public class Rabbit extends Mammal {

   private double weight;

   public Rabbit(String name, double weight) {
       super(name);
       this.weight = weight;
   }

   public double getWeight() {
       return weight;
   }

   @Override
   public boolean isCookable() {

       return true;
   }

}
/*********************************EuropeanRabbit.java*******************************/


public class EuropeanRabbit extends Rabbit {

   public EuropeanRabbit(String name, double weight) {
       super(name, weight);

   }

   public EuropeanRabbit(String name) {
       this(name, 2.0);
   }

   @Override
   public boolean isCookable() {

       return true;
   }

}
/***********************************LapinSautéChasseur.java************************/


public class LapinSautéChasseur extends EuropeanRabbit {

   public LapinSautéChasseur() {
       super("Delicious", 0.5);

   }

}
/*******************************FrankTheRabbit.java******************************/


public class FrankTheRabbit extends Rabbit {

   public FrankTheRabbit() {
       super("Frank", 100.0);

   }

   @Override
   public boolean isCookable() {
      
       return false;
   }

  
}
/*************************************CastIronPot.java*************************/


public class CastIronPot {

   private Rabbit rabbit;

   public CastIronPot(Rabbit rabbit) {
       super();
       this.rabbit = rabbit;
   }

   public Rabbit getRabbit() {
       return rabbit;
   }

   public static void testCastIronPot() {

       LapinSautéChasseur lsc1 = new LapinSautéChasseur();
       LapinSautéChasseur lsc2 = new LapinSautéChasseur();

       if (lsc1 == lsc2) {

           System.out.println("Both are same");
       } else {

           System.out.println("Both are not same");
       }
   }
}
/*********************************Start.java*******************/

import java.util.ArrayList;

public class Start {

   public static void main(String[] args) {
      
       ArrayList<Mammal> mammals = new ArrayList<>();
      
       mammals.add(new Human());
       mammals.add(new Rabbit("ABC", 3.0));
       mammals.add(new EuropeanRabbit("DFG"));
       mammals.add(new LapinSautéChasseur());
       mammals.add(new FrankTheRabbit());
      
       for (Mammal mammal : mammals) {
          
           System.out.println(mammal.isCookable());
       }
      
       CastIronPot pot = new CastIronPot(new LapinSautéChasseur());
       System.out.println(pot.getRabbit().getName());
       CastIronPot.testCastIronPot();
   }
}
/************************************output*******************/

false
true
true
true
false
Delicious
Both are not same

Please let me know if you have any doubt or modify the answer, Thanks:)


Related Solutions

This the question about the java and please show the detail comment and code of each...
This the question about the java and please show the detail comment and code of each class.Thank you. And the "+" means "public" the "-" means "private" and testBankAccount(): void testMobilePhone(): void testChocolate(): void all are static Create four classes with the following UML diagrams: +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ |...
This problem is about java program and please show the detail comment and code in each...
This problem is about java program and please show the detail comment and code in each class. Thank you! Create four classes with the following UML diagrams: (The "-" means private and the testBankAccount() testMobilePhone() testChocolate() testStudent() all static +-----------------------------+ | BankAccount | +-----------------------------+ | - money: int | +-----------------------------+ | + BankAccount(int money) | | + getMoney(): int | | + setMoney(int money): void | | + testBankAccount(): void | +-----------------------------+ +------------------------------------------------+ | MobilePhone     | +------------------------------------------------+ | -...
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 | |...
This is an intro to java question. Please post with pseudocode and java code. Problem should...
This is an intro to java question. Please post with pseudocode and java code. Problem should be completed using repetition statements like while and selection statements. Geometry (10 points) Make API (API design) Java is an extensible language, which means you can expand the programming language with new functionality by adding new classes. You are tasked to implement a Geometry class for Java that includes the following API (Application Programming Interface): Geometry Method API: Modifier and Type Method and Description...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
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 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...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem...
This is an Intro to java question. Please provide code and pseudocode for better understanding. Problem 4: Player Move Overworld (10 points) (Game Development) You're the lead programmer for an indie game studio making a retro-style game called Zeldar. You've been tasked to implement the player movement. The game is top-down, with the overworld modeled as a 2d grid. The player's location is tracked by x,y values correlating to its row and column positions within that grid. Given the current...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA...
This is an intro to java question. Please answer with pseudocode and code. Problem 2: RSA Public Key (10 points) (Cyber Security) RSA is an asymmetric encryption scheme, where a public key is used to encrypt data and a different, private key decrypts that data. RSA public/private keys are generated from two prime numbers, typically very large ones. The idea behind RSA is based on the fact that its difficult to factorize very large integers. RSA public key generation is...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA...
This is an Intro to Java Question, Please respond with code and pseudocode. Problem 3: RSA Private Key (10 points) (Cyber Security) In the RSA algorithm, encrypting and decrypting a message is done using a pair of numbers that are multiplicative inverses with respect to a carefully selected modulus. In this task, you must calculate the modular multiplicative inverse for given set of values. What is the Modular Multiplicative Inverse? In mathematics, each operation typically has an inverse. For example,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT