A common problem in computing is where you need to write a method that returns multiple...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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<F, S> { 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<String, Integer> 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<String, Integer> 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\rpgfo\.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<String, Integer> 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. 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<F, S> { 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<String, Integer> 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<String, Integer> 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\rpgfo\.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<String, Integer> 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.
Expert Answer:
Answer rating: 100% (QA)
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... View the full answer
Related Book For
Introduction To Materials Management
ISBN: 978-9386873248
8th edition
Authors: Arnold J. R. Tony, Gatewood Ann K., M. Clive Lloyd N. Chapman Stephen
Posted Date:
Students also viewed these programming questions
-
Case Study: Quick Fix Dental Practice Technology requirements Application must be built using Visual Studio 2019 or Visual Studio 2017, professional or enterprise. The community edition is not...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
John Wilson is a conservative investor who has asked your advice about two bonds he is considering. One is a seasoned issue of the Capri Fashion Company that was first sold 22 years ago at a face...
-
Compute the present value of the payment patterns provided below, given an 8 percent discount rate. a. $50 at the end of Year 2; $100 at the end of Year 5; $80 at the beginning end of Year 8. b. $100...
-
Cleanwater Inc. is an Australian public company involved in water treatment projects. In early 2020 it was involved in negotiations with the provincial government of Da Nang in Vietnam. Following the...
-
On January 2, 2018, The Register, a science and technology news site based in Britain, published an article revealing the existence of two major bugs leaving virtually all computers and smartphones...
-
Grave City is considering the relocation of several police substations to obtain better enforcement in high-crime areas. The locations under consideration together with the areas that can be covered...
-
Briefly describe the two Oracle Administrative Tools and talk about some of their administration usage: -SQL*Plus - Oracle Enterprise Manager Database Express (EM Express) Question2: Explain briefly...
-
1.Identify the three countries with the most favorable collection experience as measured by their receivables turnover ratios. RTR US = RTR Saudi Arabia = RTR UK = 2. Which country appears to have...
-
What was NOT TRUE of society and culture in the South? 1)Yeoman farmers were dependent on wealthy planters for use of specialized machines like the cotton gin 2)Hill People disliked the planters but...
-
IndoSurg is an Indian manufacturing company that specializes in state-of-the-art surgical instruments. Its newest client is Medhealhauser, a German university medical clinic. Medhealhauser's...
-
1. Describe the perfect target market for this product. Please include demographics and values, such as age, family size, gender, approximate income, profession, geographic characteristics of where...
-
Tim, Sim, and Pim are employed as a Customer Care Agent at ABC Company Limited. Over the past years, they worked well as a team. Kim, recently employed to the company, started to feel uncomfortable...
-
SwedFer is a Swedish metal company that produces wrought iron for the gate and railing industries. It is in the middle of negotiating a contract with Amerigate, an American wrought iron casting...
-
15 pts In order for a market to be segmented it must meet certain criteria. This is called the Criteria of Segmentation which includes 4 criteria that the segment must meet: 1. substantial 2....
-
A Cartesian equation for the parametric curve is given by O 12x+27y=1, 0 x 27 12x+27y=324, 0x 27 27x+12y=324, 0x 12 12x+27y=1, Os x 27 Ox+y 324, 0x 324 x=27sin0, y=12cos20, 0 3/2
-
Complete the equations for the following equilibria and calculate Keq where the Keq expression includes [HO]. Be sure to enter Keq in proper scientific notation. (a) ammonia (acting as a base) reacts...
-
For the following data, calculate the mean absolute deviation. Actual Demand Period Forecast Absolute Deviation 105 1 100 105 95 3 110 90 4. 115 130 100 5 120 6. 125 120 Total 675 650
-
A work center consisting of 3 machines operates 40 hours a week. In a 4-week period, it actually worked 355 hours and produced 475 standard hours of work. Calculate the utilization and efficiency of...
-
If the annual cost of goods sold is $12,000,000 and the average inventory is $2,250,000, a. What is the inventory turns ratio? b. What would be the reduction in average inventory if, through better...
-
What is an off-by-one error?
-
Will the universality of management continue to be true in the future? Why or why not?
-
Describe what a manager does. How does the work of managers differ from that of nonmanagerial employees?
Study smarter with the SolutionInn App