Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program to be written should read some numbers - any number of those - on a single line and store them in an

image text in transcribed image text in transcribedimage text in transcribed

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).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres a Java program that follows the steps youve outlined import javautil public class Main public ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Management Science

Authors: Bernard W. Taylor

12th edition

133778843, 978-0133778847

More Books

Students also viewed these Programming questions

Question

What is a residual plot?

Answered: 1 week ago

Question

What is Accounting?

Answered: 1 week ago

Question

Define organisation chart

Answered: 1 week ago

Question

What are the advantages of planning ?

Answered: 1 week ago

Question

Explain the factors that determine the degree of decentralisation

Answered: 1 week ago

Question

What Is acidity?

Answered: 1 week ago