ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 06 Jun 2017 My Price 8.00

write two classes, Gorilla and Cat

You will write two classes, Gorilla and Cat, which interact (also a test harness).

Part A

☑ Write a class Gorilla. Gorillas should have a name and a weight, so add instance variables to represent these.

☑ Add an accessor and a mutator for each of the instance variables. In the mutator for weight, don't allow the weight to be set to a negative number.

☑ Add a default constructor which sets the name and weight to reasonable default values.

☑ Add a parameterized constructor that takes values for name and weight as parameters and sets the instance variables accordingly

☑ Add a toString that includes all the information about the Gorilla.

☑ Create a harness class with a main and test that you can create Gorillas (using both constructors) and set their weights and names. Don't forget to print the original values for these variables, to show your constructors work.

Part B

☑ Create a class Cat. Cats should have a name and a color (both Strings).

☑ Add accessors, mutators, and a default constructor for the Cat class. Also add a parameterized constructor that takes a value for the name.

☑ Add a toString that includes all the information about the Cat.

In the same harness as in part A, test that you can create Cats and set their variables.

Part C

☑ Add to the Gorilla class an instance variable of type Cat, called pet  (Not a String.  We want a Cat, not just its name!!!). 

(Do not use new to create a new cat in the no-parameter Gorilla constructor, unless you are prepared to argue that every time a baby gorilla is born, a kitten magically appears with it.)

☑ Add an accessor and mutator for the pet variable.

☑ In your harness, check that your new accessor and mutator work. For instance, if you have a Gorilla koko and a Cat fluffy, you should be able to say

koko.setPet(fluffy);

And if you then said

System.out.println(koko.getPet().getName());

you should get the same result as

System.out.println(fluffy.getName());

(Think through it and make sure you understand why those should be the same thing. You may want to draw what is happening in memory, if you're having trouble.)

EC +10: Give the Cat class an instance variable ape, of type Gorilla, and add mutator and accessor for it. In the harness, test that these work.

EC +10: To the Gorilla class, add a parameterized constructor that takes a String and a Cat, and sets the Gorilla's name and pet accordingly.

EC +15: To the Gorilla class, add a parameterized constructor that takes two Strings. The first is the name of the Gorilla. The second will be the name of the pet Cat. In this constructor, use the parameterized constructor for the Cat class to set up the pet variable so that it points to a newly created Cat, using the given second string for the name. So in this case calling the Gorilla constructor will also cause a Cat to be created on the heap.

Answers

(11)
Status NEW Posted 06 Jun 2017 03:06 AM My Price 8.00

-----------

Not Rated(0)