Lab 7 (Year 3 Sem 1)

Lab 7 is the eighth lab in semester 1 of year 3, and the eighth lab in the total of year 3. It is a lab for classes, and is considered to be moderately easy.

Description
Use the TestRectangle class to test your Rectangle class.

Unlocked after completion

 * OCD (Score at least 60% for Lab 7: Classes and Objects (Part 1))
 * OOP or OPPS? (Score at least 80% for Lab 7: Classes and Objects (Part 1))

Question
Design a class named Book to represent a book. The class contains:
 * A String data field named title, with default value "Java for Dummies"
 * A String data field named author, with default value "The Guru"
 * A double data field named price, with default value 99.9
 * All above attributes are to have private access . For this question, you need not create a constructor.
 * An accessor called getTitle which will return the title of the book as a String
 * An accessor called getAuthor which will return the author of the book as a String
 * A method called getPrice which will return the price of the book after adding 7% GST
 * All above methods are to have  public access 

Write a test program TestBook.java that will function as follows:
 * Create a Book object.
 * Print out the information of the book object as shown in sample output below. Note that the price needs to be formatted to 2 decimal places.

Sample Output: Title = Java for Dummies Author = The Guru Price after GST = $106.89

Solution
This question is a basic introduction to how to write classes in Java.

You do not need a constructor.

Remember, you need to use  to make a Book object.

Also, format the price returned by getPrice to two decimal places

Question
Design a class named Rectangle to represent a rectangle. The class contains:

 Two double data fields named width and height that specify the width and height of the rectangle. Both attributes should have  private access . Constructors as follows: <ul style="list-style: circle"> default constructor that takes in no inputs. Set the default values for both width and height to a randomized integer in range [1, 10] (inclusive of 1 and 10)</li> a constructor that takes in input width and height and set the rectangle's width and height according to the input given.</li> a copy constructor that takes in a Rectangle object and creates a copy of the Rectangle object passed in</li> </ul> Accessors method <font face="Courier New">getWidth and <font face="Courier New">getHeight to return the width and height of the rectangle.</li> Mutators method <font face="Courier New">setWidth and <font face="Courier New">setHeight to allow user to change the width and height. The mutators should check that the input width or height is a valid non-negative number. Print an error message if an invalid input is given.</li> A public method named <font face="Courier New">getArea that returns the area of this rectangle.</li> A public method named <font face="Courier New">getPerimeter that returns the perimeter of this rectangle.</li> </ul>

The tester program has been created for you. Download it and use it to test your Rectangle class.

A possible sample output is as follows:

<span style="font-family: &quot;Courier New&quot;;">r1: randomized width &amp; height width = 10.0 height = 8.0 area = 80.0 perimeter = 36.0

<span style="font-family: &quot;Courier New&quot;;">r2: width and height input into constructor width = 5.0 height = 2.0 area = 10.0 perimeter = 14.0

<span style="font-family: &quot;Courier New&quot;;">r3: copy of r2 width = 5.0 height = 2.0 area = 10.0 perimeter = 14.0

Solution
For constructors:
 * To make constructor with 0 arguments, use the following to generate a random number in range [1, 10]: assuming f is your random object (, import java.util.Random first)
 * To make constructor with 1 argument, you can copy each attribute individually (see notes)

For the mutators:
 * Remember to check if the input is valid.
 * Print an error message if it is invalid, then return out without setting.

Question
Design a class named BankAccount to represent a bank account. The class contains:

<ul> Three data fields named <font face="Courier New">accNumber, accHolder and <span style="font-family: &quot;Courier New&quot;;">accBalance that specify the account number (String), account holder name (String) and account balance (double). All attributes should have  private access  </li> A static data field named <span style="font-family: &quot;Courier New&quot;;">numAccounts (int) to keep track of how many account objects are created in total.</li> A constructor that accepts three arguments (account number, name and balance) and initialize the instance variables accordingly, and update <span style="font-family: &quot;Courier New&quot;;">numAccounts accordingly. Note that your constructor should have relevant code to check that </li> <ul style="list-style: circle"> account number is always of the format XXX-XXXXX-X where x is a digit If incorrect format, print an error message "Error: Account number is in format XXX-XXXXX-X" </li> balance should be at least $500 If insufficient balance, print an error message "Error: Minimum balance is $500"</li> Note that the data attributes should only be updated if validation pass through. e.g. account balance should only be set if it pass through the validation checks, otherwise it should remain as 0.</li> </ul> A public method named <font face="Courier New">getBalance that returns the balance of the account.</li> </ul>

<ul>A public method named <font face="Courier New">getNumAccount that returns the number of accounts created.</li></ul> <ul> A public method named <font face="Courier New">deposit that deposit money into the account by updating the balance. The money deposit must be greater than 0.</li> <li>A public method named <font face="Courier New">withdraw that withdraws money from the account and updates the balance. You should check that the amount to withdraw is less than or equal to the balance.</li> </ul>

You may use the tester provided to test your BankAccount class.

Solution

 * Since <span style="font-family: &quot;Courier New&quot;;">numAccounts is static, <span style="font-family: &quot;Courier New&quot;;">getNumAccount should be static as well.
 * Check if input is valid in the code for <span style="font-family: &quot;Courier New&quot;;">deposit and <span style="font-family: &quot;Courier New&quot;;">withdraw
 * In the constructor, if the data is invalid, jump out by using, do not increment <span style="font-family: &quot;Courier New&quot;;">numAccounts.
 * To check validity of <span style="font-family: &quot;Courier New&quot;;">accNumber, you can use regex or a for loop.