CitizenTest.java Answers

Click here to go back to the questions
// ANSWERS:

// (1) Write a method isRich that has no arguments and
// returns a boolean value as to whether or not the current
// person has more that $50,000 in savings. Add some code
// to the main method to test if Ernie is rich.

// HINT: Look at the getSalary method to see how to write a
// method that has no arguments and returns a value.
// 

public boolean isRich()
{
   if (savings > 50000)
   {
      return true;
   }
   else
   {
      return false;
   }
}

// Add the following line to the main method to call isRich:
System.out.println(e.isRich());

// 
// (2) Write a "salaryRise" method that has one int
// parameter called "amount" which increases the person's
// salary by that amount.  Add some code to the main method
// to raise Ernie's salary by $10,000.

// HINT: Look at the setSalary method to see how to write a
// method with one parameter.
// 

public void salaryRise(int amount)
{
   salary = salary + amount;
}

// In the main method, add the following line:
e.salaryRise(10000);

// 
// (3) Write a "netWorth" method that returns the difference
// of the person's savings and their loan.  Then put some
// code in the main method to print out Ernie's net worth.
// 

public int netWorth()
{
   return savings - loan;
}

// Add the following line to the main method to call isRich:
System.out.println("Ernie's net worth = " + e.netWorth());

// 
// (4) If we take away the "System.out.println( )" method
// call from the line that says: "e.getSalary()", then
// nothing is printed to the screen when we run it.  If we
// don't use the value returned by the call to getSalary,
// then why does Java even allow this code to compile?
// 

// ANSWER: In Java, you are allowed to ignore the return
// value of a method.

// 
// (5) Write a "toString" method that has no parameters and
// returns a string containing all the information about the
// citizen, including their name, salary, savings and their
// loan, if they have one.  Put a some code into the main
// method to test it out.
//
// Classes often have a toString method and you will learn
// more about this later.

// HINT: Use the "+" operator to build up a String object
// and return this object at the end of the method.

// 

public String toString()
{
   return ("name = " + name +
           ", salary = " + salary +
           ", savings = " + savings +
           ", loan = " + loan);
}

// Add the following line to the main method:
System.out.println(e.toString());

// 
// (6) Next to the constructor, write a second constructor
// that has one String parameter called "aName".  Make it so
// that the constructor sets the person's name to aName and
// sets every other property to zero dollars.  Add the
// following line of code to the main method to test out
// this second constructor that you have just written:

//  Citizen f = new Citizen("Fred");

// NOTE: Having more than one constructor in a class is an
// example of overloading.  You can overload any method, not
// just the constructor, by having several methods all with
// the same name but different parameters and in the same
// class.
//
// 

public Citizen(String aName)
{
   name    = aName;
   salary  = 0;
   savings = 0;
   loan    = 0;
}

// 
// (7) Write the Citizen method: public void paySalary()
// that adds the person's salary to their savings, less 10%
// tax.  Call paySalary from the main method on Ernie
// and Bert and verify that it works.
// 

public void paySalary()
{
   savings = savings + (int)(salary * .9);
}

// Add the following lines to the main method:
System.out.println("Before paying the salary Ernie has " + e.netWorth());
System.out.println("Before paying the salary Bert has " + b.netWorth());
e.paySalary();
b.paySalary();
System.out.println("After paying the salary Ernie has " + e.netWorth());
System.out.println("After paying the salary Bert has " + b.netWorth());

// 
// (8) Alter the paySalary method so that the Government's
// "money" property goes up by the amount of tax that was
// charged on the citizen.
//
// HINT: How do you access a static property?
// 

public void paySalary()
{
   savings = savings + (int)(salary * .9);
   Government.money = Government.money + (int)(salary * .1);
}

// 
// (9) Write a method public static void buyBattleships()
// in the Government class that causes the Government to buy
// as many battleships as it can afford, without going in
// debt.  Each battleship costs $42,000 each.  Remember to
// adjust both properties "money" and "numBattleships".
// 

public static void buyBattleships()
{
   while (money >= 42000)
   {
      money = money - 42000;
      numBattleships++;
   }
}

// alternatively, the modulo operator "%" can be used to
// produce the same result:
//
public static void buyBattleships()
{
   int howMany = money / 42000;
   money %= 42000;
   numBattleships += howMany;
}


// 
// (10) Call buyBattleships from the main method.
//
// HINT: How do you call a static method?
// 

Government.buyBattleships();

// 
// (11) HARDER: Why is everything in the Government class
// labelled as static?
// 

// Because there is only ever one government in the example
// that we are considering.  This makes the example simpler
// because if there were many governments, then for example
// we would have to specify which government gets the tax
// from the paySalary method.

//