CitizenTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called CitizenTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// CitizenTest.java
// Copyright (C) 1998-2016 Davin Pearson
// Website: http://davin.50webs.com/java-tutorials

class Citizen
{
   // Properties of the class...
   private String name;
   private int    salary;
   private int    savings;
   private int    loan;

   // Constructor of the class...
   public Citizen(String aName, int aSalary, int aSavings, int aLoan)
   {
      name    = aName;
      salary  = aSalary;
      savings = aSavings;
      loan    = aLoan;
   }

   // Methods of the class...
   public int getSalary()
   {
      return salary;
   }
   public void setSalary(int aSalary)
   {
      salary = aSalary;
   }
}

class Government
{
   public static int money;
   public static int numBattleships;
}

class CitizenTest
{
   // The main method is the point of entry into the program...
   public static void main(String[] args)
   {
      Citizen e = new Citizen("Ernie", 50000, 2000,   0);
      Citizen b = new Citizen("Bert", 100000,10000,5000);

      System.out.println("Ernie's salary is: " + e.getSalary());
   }
}

// QUESTIONS:

// (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.
// 
// (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.
// 
// (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.
// 
// (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?
// 
// (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.

// 
// (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.
//
// 
// (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.
// 
// (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?
// 
// (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".
// 
// (10) Call buyBattleships from the main method.
//
// HINT: How do you call a static method?
// 
// (11) HARDER: Why is everything in the Government class
// labelled as static?
//