The program to be written should read some numbers - any number of those - on...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
The program to be written should read some numbers - any number of those - on a single line and store them in an ArrayList. The numbers read could be integers, doubles, and rational numbers. Then, it should go through each element of that list, printing each on a single line and identifying the minimum value (just the value, not its index). This minimum value should be printed at the end. Here is an example of what the input/output should look like (the second line, in bold, is what the user enters): Enter a list of numbers separated by a space: 3 5.5 10 4/5 The list is: 3 5.5 10 4/5 The min is: 4/5 This assignment can be done incrementally, over 7 steps. Step 1: Read the input line, and decompose each part (i.e., number but still as a String) into an array using the split method in the String class. You can print each element of your array to confirm what you got (as strings) but do not do any calculation yet. Step 2: When looping over each number, have the code to check which type it is. Hint: if the number contains a ".", it is a double. If the number contains a "/", it is a rational number. Otherwise, it is an int. In the case of an int or a double, parse them properly to an int variable or double variable, using the parsing method of the corresponding Wrapper class. In the case of a rational number, use the split method again to separate the numerator and the denominator. Parse each of these 2 numbers (as int), and create an object of type RationalNumber using these 2 values. Use the RationalNumber class provided to you. Step 3: At the beginning of your program, create an ArrayList of integers. As you are looping over all numbers read (array in Step 1), when the number is an integer (as determined in Step 2), add it to the ArrayList. Of course, use the parsed number for this, not the original string. Step 4: In a separate loop (after the one you have from the steps above), create another loop to go over all elements of your ArrayList, printing each of these elements and identifying the minimum value in the list. Note: the minimum value at this point will be for the int values only. For example, if the example input above was provided, the list would be only 3 and 10, and the minimum would be 3. Step 5: Handle the doubles as well (i.e., adding them to the ArrayList). Of course, the type of objects the ArrayList contains will have to change, so that you can put both ints and doubles in it (note: do not keep the old code - ArrayList of int only - as it is not going to be marked. It was there just to make the assignment easier to do in smaller steps). Use a class that both Integer and Double are inheriting from (but do not use the "Object" class). Then modify your program so that it handles both integers and doubles (i.e., storing the numbers in the first loop, and printing them / identifying the minimum value in the second loop). In particular, for identifying the minimum value in the second loop, you will need to access the numbers in a common way (hint: there is a common method you can use that will extract the value as a double). Step 6: At this point, your code in the second loop probably contains multiple calls to the "get(i)" method in the ArrayList class. This is really not efficient. Change your code so that there is only one such call in each iteration of the loop, by saving this value (i.e., what is returned from that call) in a proper variable and then use this variable instead. Step 7: Finally, handle the rational numbers as well (i.e., adding the objects created in Step 2 into the ArrayList). Now since this class (Rational Number) does not inherit from the same class as Integer and Double, you will not be able to use it as is, as a valid type for your ArrayList. Modify the RationalNumber class so that it can be used in the ArrayList (i.e., make it inherit from the same class as the one currently used for your ArrayList - and add all the code necessary so that such inheritance is properly implemented). Then modify the rest of your program so that it handles all 3 types of input (similar modifications as for Step 5). For each modification made to the RationalNumber class, add a comment in your code explaining why you need such modification. IMPORTANT: • Make sure that when you print the minimum value, you print it in the same way as how it is written in the input. In the example above, this means printing "4/5" as the minimum value, not "0.8". Do not use the original string for this though: use a method in the Rational Number class. Your second loop should not have any code that is testing the type of the number (e.g., "if (x instanceOf RationalNumber)..."). You should just rely on polymorphism to get everything working fine. • Don't forget to add the comments in the RationalNumber class, as requested in Step 7 above. You have to indicate the reason for each of the changes you made in that class, including but not limited to: class "extends", add/modify/delete variable(s), add/modify/delete method(s). The program to be written should read some numbers - any number of those - on a single line and store them in an ArrayList. The numbers read could be integers, doubles, and rational numbers. Then, it should go through each element of that list, printing each on a single line and identifying the minimum value (just the value, not its index). This minimum value should be printed at the end. Here is an example of what the input/output should look like (the second line, in bold, is what the user enters): Enter a list of numbers separated by a space: 3 5.5 10 4/5 The list is: 3 5.5 10 4/5 The min is: 4/5 This assignment can be done incrementally, over 7 steps. Step 1: Read the input line, and decompose each part (i.e., number but still as a String) into an array using the split method in the String class. You can print each element of your array to confirm what you got (as strings) but do not do any calculation yet. Step 2: When looping over each number, have the code to check which type it is. Hint: if the number contains a ".", it is a double. If the number contains a "/", it is a rational number. Otherwise, it is an int. In the case of an int or a double, parse them properly to an int variable or double variable, using the parsing method of the corresponding Wrapper class. In the case of a rational number, use the split method again to separate the numerator and the denominator. Parse each of these 2 numbers (as int), and create an object of type RationalNumber using these 2 values. Use the RationalNumber class provided to you. Step 3: At the beginning of your program, create an ArrayList of integers. As you are looping over all numbers read (array in Step 1), when the number is an integer (as determined in Step 2), add it to the ArrayList. Of course, use the parsed number for this, not the original string. Step 4: In a separate loop (after the one you have from the steps above), create another loop to go over all elements of your ArrayList, printing each of these elements and identifying the minimum value in the list. Note: the minimum value at this point will be for the int values only. For example, if the example input above was provided, the list would be only 3 and 10, and the minimum would be 3. Step 5: Handle the doubles as well (i.e., adding them to the ArrayList). Of course, the type of objects the ArrayList contains will have to change, so that you can put both ints and doubles in it (note: do not keep the old code - ArrayList of int only - as it is not going to be marked. It was there just to make the assignment easier to do in smaller steps). Use a class that both Integer and Double are inheriting from (but do not use the "Object" class). Then modify your program so that it handles both integers and doubles (i.e., storing the numbers in the first loop, and printing them / identifying the minimum value in the second loop). In particular, for identifying the minimum value in the second loop, you will need to access the numbers in a common way (hint: there is a common method you can use that will extract the value as a double). Step 6: At this point, your code in the second loop probably contains multiple calls to the "get(i)" method in the ArrayList class. This is really not efficient. Change your code so that there is only one such call in each iteration of the loop, by saving this value (i.e., what is returned from that call) in a proper variable and then use this variable instead. Step 7: Finally, handle the rational numbers as well (i.e., adding the objects created in Step 2 into the ArrayList). Now since this class (Rational Number) does not inherit from the same class as Integer and Double, you will not be able to use it as is, as a valid type for your ArrayList. Modify the RationalNumber class so that it can be used in the ArrayList (i.e., make it inherit from the same class as the one currently used for your ArrayList - and add all the code necessary so that such inheritance is properly implemented). Then modify the rest of your program so that it handles all 3 types of input (similar modifications as for Step 5). For each modification made to the RationalNumber class, add a comment in your code explaining why you need such modification. IMPORTANT: • Make sure that when you print the minimum value, you print it in the same way as how it is written in the input. In the example above, this means printing "4/5" as the minimum value, not "0.8". Do not use the original string for this though: use a method in the Rational Number class. Your second loop should not have any code that is testing the type of the number (e.g., "if (x instanceOf RationalNumber)..."). You should just rely on polymorphism to get everything working fine. • Don't forget to add the comments in the RationalNumber class, as requested in Step 7 above. You have to indicate the reason for each of the changes you made in that class, including but not limited to: class "extends", add/modify/delete variable(s), add/modify/delete method(s).
Expert Answer:
Answer rating: 100% (QA)
Heres a Java program that follows the steps youve outlined import javautil public class Main public ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
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...
-
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...
-
ABC Company acquired 60,000 shares of DEF Incorporated's 200,000 ordinary shares on January 2, 2019 for P 4,000,000 when the net assets of DEF Incorporated was P 12,000,000. The excess of acquisition...
-
As of January 1, 2015, Norman has a basis of $90,000 in his 30% capital interest in the Plata Partnership. He and the partnership use the calendar year for tax purposes. The partnership incurs an...
-
Computing factorials lends itself to recursion because _________________. a. It is impossible to compute a factorial using a loop b. Recursion is most useful for all mathematical computations c....
-
Daphne Brown-Wright worked as a teacher for East St. Louis School District 189 from 1975 until 1998 and then returned as an administrator from 2002 until 2012, thus serving the District for 33...
-
The Mega-Bucks Corporation is planning its production schedule for the next four weeks and is forecasting the following demand for compound Xa key raw material used in its production process: The...
-
Create three new users with names and privileges as described below in Oracle database: Show the code you used to make these users and grant them the correct privileges. Name User1 User2 User3...
-
Parson Company acquired an 80 percent interest in Syber Company on January 1, 2020. Any portion of Sybers business fair value in excess of its corresponding book value was assigned to trademarks....
-
Case Study: Managing a branch closure program: an exercise in planning and managing the process of change The aim of this activity is to explore the issues and choices involved in developing an...
-
Explain how the current fiscal policy agenda (as reflected in the annual Budget Review released by National Treasury 2023 ) impacts South Africas global competitiveness through specially its balance...
-
Suppose an economy initially operates at the natural rate of production (full employment production). Use the AD-AS model to graphically illustrate the effects on price and output of an increase in...
-
Due to general price inflation in our economy, the purchasing power of the dollar decreases over time. If the average headline inflation rate is expected to be 7% annually for the foreseeable future,...
-
Identify a South African company whose products or services you really admire. Conduct an assessment of why, where, and how this company might expand internationally.
-
What are marketing 5 problems faced in tourism industry in Oman 2021?
-
a. Discuss the various operational budgets prepared for: (i) a service organisation (ii) a trading organisation. b. (i) How do firms handle the complexity of calculating breakeven when there are many...
-
Don Griffin worked as an accountant at a local accounting firm for five years after graduating from university. Recently, he opened his own accounting practice, which he operates as a corporation....
-
A. Formulate a goal programming model to help the board with its dilemma. B. Solve the goal programming model by using the computer. The Oakdale County School Board was meeting in special session. A...
-
The bus company in Draper operates a "Weekend Rider" bus service from the Tech campus to Washington, DC. The bus seats 55 passengers and selects the shortest route to DC that will also result in at...
-
The Beta Upsilon Sigma honorary business fraternity at Tech wants to open a food booth at the football stadium for the upcoming season as a project to raise money to tutor students in management...
-
What are two ratios that measure market value for publicly traded investor-owned businesses?
-
Grandview Clinic has fixed costs of $2 million and an average variable cost rate of $15 per visit. Its sole payer, an HMO, has proposed an annual capitation payment of $150 for each of its 20,000...
-
Part A For each item, identify whether it best applies to lean businesses \((L)\) or traditional businesses \((T)\). 1. Production begins with a sales forecast. 2. Only finished goods are inspected...
Study smarter with the SolutionInn App