Identifying Instance Variables, Constructors, and Methods for a Record
|
Grab a Piece of Paper! |
Practice 1: Bank Account
Given a bank account record as follows:
record BankAccount(String accountType, String ownersName, String routingNumber, String accountNumber) {
}
- What is the name of the Record?
- List the instance variables for this record.
- What is the constructor for this record?
- List the accessor methods for this record.
- What other methods are provided?
Practice 2: Address Record
Given a Address record as follows:
record USAddress(String street1, String street2, String city, String state, String zip) {
}
- What is the name of the Record?
- List the instance variables for this record.
- What is the constructor for this record?
- List the accessor methods for this record.
- What other methods are provided?
Your Turn - 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 3: Create a Point
Record
- Create a
Point
record that has int
coordinates x
and y
.
- Create three
Point
objects for the points: (3, 1), (1, 3), (-1, 3)
- Using the accessor method to obtain the x-coordinates of each
Point
and print them to the console.
- Print each
Point
to the console
- 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.
- Create a new
Point
object for the point (3, 1)
- Use the
equals
method to check if this newly created Point
object is equal to the first Point
object you created.
Practice 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
.
- Create a
Receipt
record based on the description above.
- Create a new
Receipt
object with your name as the server, food total as 100
, tip as 20
, total bill as 120
.
- For the newly created object, print the server name followed by " will receive $", the amount of tips, and " in tips."