Exercises on Classes and Objects

  1. double width = 44.0;

int length = 13;

Rectangle myRect = new Rectangle(length, width);

  1. Identify the class
  2. Identify the object
  3. What type of parameter(s) are passed to the constructor
  1. Write out the signature for the constructor of the Rectangle class from #1 above.
  1. Suppose a constructor for the Lunch class is as follows:

public Lunch(boolean diet, int cal)

{

diet_yes_no = diet;

calories = cal;

}

Write appropriate code that will create a Lunch object called yummy5. Tell the constructor that yes, you are on a diet and the number of calories should be 900.

  1. BankAccount account39 = new BankAccount(500.43);
  1. Identify the class
  2. Identify the object
  3. What type of parameters are passed to the constructor
  1. A class is like a ______. An object is like a ______

Fill in the blanks above using the word “cookie” and “cookie cutter”.

  1. What’s wrong with the following constructor for the School class?

public void School (int d, String m)

{

district = d;

name = m;

}

  1. Which of the following is a correct association?
  1. One class, many objects
  2. One object, many classes
  1. What must exist first? The class or the object
  1. Is the following legal?

//Constructor

public House(int j, boolean k)

{…some code…}

//This code is in main of Tester class

int p=3, q=0;

House myHouse = new House(p, q);

  1. Create a class called BankAccount. It should have the following properties:
  1. Two instance variables:

double balance //This is how much money is currently in account

String name // The name of the person owning the account

  1. Constructor should accept 2 parameters.

-One should be a double variable that is used to initialize the state variable, balance

-The other should be a String that is used to initialize the state variable, name

  1. Add mutator, accessor and toString methods
  1. Two additional methods:

-deposit, returns nothing, accepts a double that is the amount of money being deposited. It is added to the balance to produce a new balance.

-withdraw, returns a boolean, accepts a double that is the amount of money being taken out of the account. If there is enough of a balance to allow for withdrawal, the amount is subtracted from the balance to produce a new balance and returns true. If the balance is not large enough to cover the withdrawal, return false.

Create a tester class that has a main() method. In that method you should input from the user the amount (1000) of money initially to be put into the account (via the constructor) along with the name of the person to whom the account belongs.

Use the user input to create a new BankAccount object called myAccount.

Call the deposit method to deposit $505.22

Print the balance state variable.

Call the withdraw method to withdraw $100

Print the remaining balance.

Call the withdraw method to withdraw $2000. Verify failure to withdraw.

Print the account using line: System.out.println(myAccount)