Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A common problem in computing is where you need to write a method that returns multiple values, but only a single return type is
A common problem in computing is where you need to write a method that returns multiple values, but only a single return type is allowed. This is almost a universal constraint in the major programming languages in use today (c++, c#, php, ruby, python, java, javascript, ...). There are a few workarounds for this problem: The return type can be an Array, which supports any number of return values stored in the array that is returned. The return type can be a class, where the different values being returned are stored in the fields of an instance of the class. Fields can be used to hold values that are intended to be returned from the method. This is usually a bad design choice. For this lab, we will create a Pair class to allow for two values to be returned from a method. Since a method can return different types of pairs (string-string, string-double, int-boolean, string-char, ...) we will use generics in place of the data types for the two fields. public class Pair { private F first; private S second; Note: You should notice that both types are declared in the class header, separated by a comma, and both are using conventions with a single uppercase letter. Next, add getters, setters, and a toString() method for Pair objects. The toString() should have the following format, where first and second are replaced by the field values: Pair (first, second) Try creating a few pair objects that store strings and integers. The string in the pair should be the name of a pet you've owned. The integer should be the age of the pet, adjusted for the type of animal (ie. in animal years). Note: Be careful to include the generic type with your Pair objects. It should be Pair and not Pair. Then print out the toString() of your Pair objects to the Java console: Pair (Captain Kidd, 33) Pair (Bardy, 28) Once you have verified that the toString() method is formatted correctly, replace the println() statements in your driver class to show a more easily read message about your pets and their ages. Do not update the toString() method to get the output seen below. Instead, use the getters in the Pair class to format the call to println(). Captain Kidd (33) is a parrot Bardy (28) is a cat Write a method that allows the user to enter their own pet name and age. The method should be called userPetDetails(). When executed the method will prompt the user for a string and integer and then return a Pair object. Test the method by calling it a few times and printing the resulting Pair object to the Java console. For example: Run: 2 On Main X C:\Users pgfo\.jdks\openjdk-19.0.1\bin\j Enter a first name for your pet: Cecil Enter an age for your pet: 17 Enter a first name for your pet: William Enter an age for your pet: 25 Pair (Cecil, 17) Pair (William, 25) Try writing another method to interact with Pair objects. This method should accept a Pair parameter and should increment the second field in the Pair. The method should be called increment(). When you have written the method, try passing one of the Pair objects you received from userPetDetails() to the increment() method and print the results. Cecil is 17 in pig years William is 25 in dog years Cecil is 18 in pig years William is 25 in dog years When you have completed the steps above, zip up your project files and upload them to the submission area on Canvas.
Step by Step Solution
★★★★★
3.37 Rating (144 Votes )
There are 3 Steps involved in it
Step: 1
Java code to create the Pair class Pairjava public class Pair attributes private F first private S second getter method for first public F getFirst re...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started