Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need Help for the Java : Complex, fractions and points(20 points) Write a progra

ID: 3571197 • Letter: N

Question

Need Help for the Java :

Complex, fractions and points(20 points)

Write a program that repeatedly asks the user to input either a fraction, 2D point or a complex number until the user indicates they want to stop. If the user does not want to stop, ask for the values of the specified type and keep track of everything they have entered. You can assume they will not enter more than 100 items. The order of the types of numbers the user enters can be anything. You must show the previously entered values exactly as shown in the example.

Hint: See the MultipleClasses.java file attached for how to incorporate multiple classes in the same file (*hint* *hint*)

Hint2: override the toString() method using inheritance requires writing a very minimal amount of code

Note: However you solve this, please use only one Scanner variable.

Example 1 (user input is bolded):

Current stored: []

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

c

Enter the two values:

1 2

Current stored: [1.0 + 2.0i]

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

p

Enter the two values: ­

2 2.5

Current stored: [1.0 + 2.0i, (­2.0, 2.5)]

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

f

Enter the two values:

6 8

Current stored: [1.0 + 2.0i, (­2.0, 2.5), 6.0/8.0]

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

c

Enter the two values:

32 0

Current stored: [1.0 + 2.0i, (­2.0, 2.5), 6.0/8.0, 32.0 + 0.0i]

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

q

Example 2 (user input is bolded):

Current stored: []

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

f

Enter the two values:

2 3

Current stored: [2.0/3.0]

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

f

Enter the two values:

0 0

Current stored: [2.0/3.0, 0.0/0.0]

Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?

q

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Explanation / Answer

Please follow the code and comments for description :

CODE :

import java.text.DecimalFormat; // required imports
import java.util.ArrayList;
import java.util.Scanner;

public class inputData { // class to run the code

    public static void main(String[] args) { // driver method
        Scanner sc = new Scanner(System.in); // scanner class to run the code
        ArrayList al = new ArrayList(); // list that saves the data
        DecimalFormat f = new DecimalFormat("#.0"); // formatter
        System.out.println("Current Stored : " + al.toString()); // message initially
        System.out.println("Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?"); // prompt
        while (true) { // iterate till the user ends
            String choice = sc.nextLine(); // get the data
            switch (choice) { // switch over the choice
                case "c": // for a complex number case
                    System.out.println("Enter Two Numbers : "); // message
                    double x = sc.nextDouble();
                    double y = sc.nextDouble();
                    String res = f.format(x) + " + " + f.format(y) + "i"; // format the data
                    al.add(res); // add to list
                    System.out.println("Current Stored : " + al.toString()); // print to console
                    System.out.println("Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?"); // prompt
                    break;
                case "p":
                    System.out.println("Enter Two Numbers : "); // message
                    double m = sc.nextDouble();
                    double n = sc.nextDouble();
                    String r = "(" + f.format(m) + ", " + f.format(n) + ")"; // formatter
                    al.add(r); // add to list
                    System.out.println("Current Stored : " + al.toString()); // prompt
                    System.out.println("Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?"); // prompt
                    break;
                case "f": // for fraction message
                    System.out.println("Enter Two Numbers : "); // prompt
                    double p = sc.nextDouble();
                    double q = sc.nextDouble();
                    String result = f.format(p) + "/" + f.format(q); // formatter
                    al.add(result); // add to list
                    System.out.println("Current Stored : " + al.toString()); // prompt
                    System.out.println("Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?"); // prompt
                    break;
                case "q":
                    System.exit(0); // exit the code
                    break;
                default:
                    break;
            }
        }
    }
}


OUTPUT :

Current Stored : []
Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?
c
Enter Two Numbers :
1
2
Current Stored : [1.0 + 2.0i]
Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?
p
Enter Two Numbers :
1
2
Current Stored : [1.0 + 2.0i, (1.0, 2.0)]
Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?
f
Enter Two Numbers :
1
2
1.0/2.0
Current Stored : [1.0 + 2.0i, (1.0, 2.0), 1.0/2.0]
Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?
c
Enter Two Numbers :
3
4
Current Stored : [1.0 + 2.0i, (1.0, 2.0), 1.0/2.0, 3.0 + 4.0i]
Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit?
q


Hope this is helpful.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote