Question1.java

Create a class called Egg that contains the private instance variables boolean incubating, int hatchTime, double degreesC. Provide a constructor that has an int parameter called hatchTime and a double parameter called degreesC. It should set incubating to true and set the other fields equal to their argument. Provide a method called isIncubating that returns the incubating field. Provide a method called isHatched that returns true if the hatchTime is zero or less. Provide a method called getDegreesC that returns the degreesC.

Provide a method called incubate that has a parameter of type boolean called heatUp. If the Egg is incubating, this should decrement the hatchTime. If heatUp is true, the method should increase the degreesC by 1. If heatUp is false, this should decrement degreesC. If the degreesC is outside the range of 36 and 39 or the Egg has hatched, set incubating to false.

Override the toString method to return "Boiled Egg" if degreesC is greater than 100, "Live Chicken" if the egg has hatched, "Fertile Egg" if it is incubating, or "Infertile Egg" if incubating is false.

Provide a static method called sitOn that has a parameter of type Egg. The method should ask the user "Sit on egg (y/n)?". If the user enters "y", call the argument Egg's incubate method with true as an argument. If the user enters "n" call the Egg's incubate method with false as the argument. If the user entered neither "y" nor "n", display an error message and ask for input again. This process should repeat while the Egg is incubating. Print the egg at the end of your loop.

Question2.java

Create a class called Sportsperson that contains the instance variables String name, int games, and int wins. Provide a constructor to set each field to arguments. Provide a toString method that returns a String in the format, "name won wins / games games", where name, wins, and games are the instance variables.

Create another class called SportsTeam that contains a private String name and a private array of Sportspersons as an instance variable. Provide a constructor that takes an argument of type String and another argument of type int and sets the name to the given string and the length of the array to the argument int. Provide a method called add that takes a Sportsperson as an argument and adds it to the next available position in the array. If the array is full, this should throw an ArrayIndexOutOfBoundsException. Use exception handling to print out its error message and prevent it from crashing the program. Provide a toString method that returns the name of the SportsTeam followed by each of its players' on new lines.

Provide a static method called readFromFile that takes a String filename as an argument. It should use the Scanner class to open the file with that name for reading. You can assume that the first line in the file is an int representing the number of players in the team. The following lines represent players and are in the format, "name:wins/games", i.e. "Michael Jordan:706/1072". The method should create a Sportsperson for each line in the file, using the information from that line to initialize the name, games and wins variables. The method should return a SportsTeam with all the created Sportspersons in its array. The name of the SportsTeam should be set to the filename, with the ".txt" extension removed.

Question3.java

Create a class called SpaceRock that contains the protected variables String name, double mass and double speed. Provide a constructor initializing the fields to arguments. Provide a getter for mass and name. Provide a method called getKmPerSec that returns the speed. Provide a toString method that returns a String in the form, "name travelling speed km per second", where name and speed are the instance variables.

Create a class called Planet that is derived from SpaceRock. Give it the additional protected variables boolean supportsLife and long population. Provide two constructor methods: a constructor that initializes all variables of SpaceRock to arguments but initializes supportsLife to false and population to zero; and another constructor that initializes all its parent's instance variables to arguments, sets the population to an argument and sets supportsLife to true. Provide a method called supportsLife that returns the supportsLife variable. Provide a getter for population. Provide a setter for population that only increases the population if the Planet supports life and the argument is zero or greater.

Back in the SpaceRock class provide a static method called percentageSupportsLife that takes an array of SpaceRocks as an argument. It should calculate and return the number of life supporting Planets in the array divided by the array's length multiplied by 100. Provide another static method called getHeavier that has two parameters of type SpaceRock. It should return the SpaceRock that has the highest mass, or null if both masses are the same.

Question4.java

Create a class called Food that contains the public final instance variables String name and int energy, and the private boolean field, eaten. Provide a constructor to set the name and energy to arguments and eaten to false. Provide a method called eat that sets eaten to true and returns the energy if eaten was false, or returns 0 if eaten was true. Override the toString method that returns the name.

Create an abstract class called Animal that contains the private instance variable int energy. Provide a constructor that sets the energy to an argument. Declare an abstract method called getSpecies that returns a String. Declare an abstract method called breedWith that has a parameter of type Animal and returns an Animal. Declare an abstract method called getDiet that returns a Food array.

Override the toString method to return the String returned from getSpecies. Provide a method called isAlive that returns true if the Animal's energy is greater than 0 and false otherwise. Provide a method called live that, if the Animal is alive, decreases the energy by 10. Provide a method called eat that takes a Food as an argument. If the Animal is alive and the argument Food's name is equal to the name of one of the Foods returned from the getDiet method, it should call the Food's eat method and add the energy returned to the Animal's energy.

Extend Animal to create the Cat class. Provide a constructor that sets the energy to 50. Implement the inherited abstract methods. The getSpecies method should return "cat". The getDiet method should return a Food array containing 3 Foods: fish with 50 energy; cheese with 10 energy; and chicken with 30 energy. The breedWith method should return a new Cat if the argument and the calling object are both alive and the argument is a Cat, otherwise it should return null. Override the live method to print "Meow!" if the Cat is alive. It should also perform the same action as its parent class's live method.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.