3B - Class Methods / Conditionals


You should only use constructs and concepts we have covered so far in the class.

Classwork


You will write two classes, Gorilla and Cat, which interact.

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 no parameter 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 while they still have 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 (a String).

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());

Part D

 Give the Cat class an instance variable ape, of type Gorilla, and add mutator and accessor for it.

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

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, if the name given for the Cat is not null, 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.

 In the harness, test that these work.


Homework


Part A

Create a class Taxpayer which has instance variables to store income, age, marital status, and veteran status. Include at least a default constructor, toString, and an accessor and mutator for each variable.

Use conditionals to make sure income and age are within valid ranges, and that no one is both under age 18 and married or a veteran.

Part B

To your Taxpayer class add a method printTax(). In this method use conditionals to determine tax paid by the Taxpayer based on the scheme:

Print a statment like "From a gross income of $41000, you owe $10250 tax and your take-home income is $30750. Use an inline conditional so that if they pay 0 tax the statement reads something like  "From a gross income of $9000, you owe no tax and your take-home income is $9000." 

In a separate main program, create several Taxpayer objects with different values for their instance variables, and for each use printTax to show their tax situation.

Part C

 Actually, people should pay no tax on the money they make up to $10000, 15% on the amount over $10000 up to $35000, %25 on the amount over $35000 up to $85000, etc.

So if someone makes $75000 they would pay 25% on the amount over 35000, which is 40000, 15% on the amount between 10000 and 35000, which is 25000, and 0% on the rest.  So: 0%* 10000 + %15 * 25000 + 25% * 40000.   (hint: notice that once we'd found the taxes on the amount over 35000, we can ignore that and just deal with the remaining $35000... a similar pattern holds for all tax brackets)

Create a second method in Taxpayer printBracketedTax() to calculate and print tax based on this scheme, (still using exemptions)  Do this by first determining which is the highest tax bracket they must pay in and using an int to keep track of this.  Then use a switch statement on this int and take advantage of falling through multiple cases to actually compute the tax.  (DO NOT get rid of your existing printTax). 

Also test this version in your main program.