Creating Records and Calling Methods Practice
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:
- 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:
- 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
Pointrecord that hasintcoordinatesxandy. - Create three
Pointobjects for the points: (3, 1), (1, 3), (-1, 3) - Using the accessor method to obtain the x-coordinates of each
Pointand print them to the console. - Print each
Pointto the console - Use the
equalsmethod to check if the first twoPointobjects are equal to each other or not. Print the value to the console. - Create a new
Pointobject for the point (3, 1) - Use the
equalsmethod to check if this newly createdPointobject is equal to the firstPointobject 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
Receiptrecord based on the description above. - Create a new
Receiptobject with your name as the server, food total as100, tip as20, total bill as120. - For the newly created object, print the server name followed by " will receive $", the amount of tips, and " in tips."