Skip Top Navigation Bar

Creating Records and Calling Methods Practice

Identifying Instance Variables, Constructors, and Methods for a Record

Practice Set 1 - Bank Account Record

Given a bank account record as follows:

record BankAccount(String accountType, String ownersName, String routingNumber, String accountNumber) { 
}
  1. What is the name of the Record?
  2. List the instance variables for this record.
  3. What is the constructor for this record?
  4. List the accessor methods for this record.
  5. What other methods are provided?

Practice Set 2 - Address Record

Given a Address record as follows:

record USAddress(String street1, String street2, String city, String state, String zip) { 
}
  1. What is the name of the Record?
  2. List the instance variables for this record.
  3. What is the constructor for this record?
  4. List the accessor methods for this record.
  5. What other methods are provided?

Record Practice with the Java Playground

We will be using the Java Playground to complete the practice exercises in this set. In the Java Playground, you can create a new record type, create an object of that new type, and call methods on that object.

Practice Set 3 - Create a Point Record

  1. Create a Point record that has int coordinates x and y.
  2. Create three Point objects for the points: (3, 1), (1, 3), (-1, 3)
  3. Using the accessor method to obtain the x-coordinates of each Point and print them to the console.
  4. Print each Point to the console
  5. Use the equals method to check if the first two Point objects are equal to each other or not. Print the value to the console.
  6. Create a new Point object for the point (3, 1)
  7. Use the equalsmethod to check if this newly created Point object is equal to the first Point object you created.

Practice Set 4 - Restaurant Receipt

A restaurant receipt might consists of:

  • The server's name,
  • The food total,
  • The tip amount,
  • The total bill.

In this example, the server's name is being represented as a String. The food total, tip amount, and total bill are being represented as type double.

  1. Create a Receipt record based on the description above.
  2. Create a new Receipt object with your name as the server, food total as 100, tip as 20, total bill as 120.
  3. For the newly created object, print the server name followed by " will receive $", the amount of tips, and " in tips."