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
-
Problem 6: Cost of Production Report Mara uses weighted-average process costing. It had the following data in August. Beginning inventory, 60% complete 10,000 units Units completed 50,000 units Units...
-
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...
-
Find a function f satisfying (a) (f = (2xy + y)i + (x2 + x + cos y)j; (b) (f = (yz - e-x)i + (xz + ey)j + xy k;
-
Give your honest opinion on the requirement for physics prerequisites in pre-med/health tracks at American higher education institution. Can time exist without matter? Is time finite or infinite? Is...
-
Which accounting concept is being applied when comparative financial statements are prepared to provide useful information for statement users?
-
Brynne Packaging is a large packaging company, offering its customers the highest standards in innovative packaging solutions and reliable service. About 25% of the employees at Brynne Packaging are...
-
Q1) a) Define the accumulation amount and accumulation factor for a loan/investment. Give the formula for the accumulation factor for simple interest with annual rate r. b) Explain the difference...
-
Rina's Performance Pizza is a small restaurant in Denver that sells gluten-free pizzas. Rina's very tiny kitchen has barely enough room for the three ovens in which her workers bake the pizzas. Rina...
-
A company is considering investing in a project that requires an initial investment of $100,000. The project is expected to generate cash inflows of $30,000 at the end of each year for the next five...
-
What role does tray efficiency play in the overall performance of a distillation column, and what factors contribute to deviations from theoretical stage efficiency ?
-
What are the fundamental thermodynamic principles underlying azeotropic distillation, and how do these principles guide the design of separation processes to break azeotropes ?
-
1. A scientist measures a piece of steel using a scale in the lab. The scale reports out a mass of 190 grams. However, for their calculations the scientist needs the mass to be in kg. What is the...
-
As an economic consultant for Lightning Volt Automotive, you are tasked with discussing the market structure that the firm is currently operating in and why. If Lightning Volt Automotive were...
-
Discuss the impact of non-ideal mixtures on distillation column design, focusing on vapor-liquid equilibrium (VLE) modeling and the use of activity coefficient-based models like UNIQUAC and NRTL
-
1) Discuss the following Learning theories: The Traditional View: Classical Conditioning The Contemporary View: Learning as a Cognitive Process 2) Choose3from the following list of organizational...
-
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...
-
The Washington Company owns no plant assets and had the following income statement for the year: Additional information about the company includes: Use the preceding information to calculate the cash...
-
The Azuza Company owns no plant assets and had the following income statement for the year: Additional information about the company includes: Use the preceding information to calculate the cash flow...
-
Use the following information regarding the Hamilton Corporation to prepare a statement of cash flows using the indirect method: Accounts payable decrease. Accounts receivable increase. Wages payable...
Study smarter with the SolutionInn App