IN JAVA Problem A Complex fractions and points oh my 20 poin
IN JAVA
Problem A: Complex, fractions and points... oh my! (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*)
Example 1 (user input is boldened):
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 bold):
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
Here\'s the multiple classes in java code:
Solution
ComplexNumber..java
public class ComplexNumber {
//Declaring variables
private double x,y;
//Parameterized constructor
public ComplexNumber(double x, double y) {
super();
this.x = x;
this.y = y;
}
/* toString() method is used to display
* the contents of an object inside it
*/
@Override
public String toString() {
return x +\"+\"+ y +\"i\";
}
}
_____________________
Fraction.java
public class Fraction {
//Declaring variables
private double x,y;
//Parameterized constructor
public Fraction(double x, double y) {
super();
this.x = x;
this.y = y;
}
/* toString() method is used to display
* the contents of an object inside it
*/
@Override
public String toString() {
return x +\"/\"+ y;
}
}
_______________________
Point.java
public class Point {
//Declaring variables
private double x,y;
//Parameterized constructor
public Point(double x, double y) {
super();
this.x = x;
this.y = y;
}
/* toString() method is used to display
* the contents of an object inside it
*/
@Override
public String toString() {
return \"(\" + x + \",\" + y + \")\";
}
}
____________________
TestClass.java (Run this class.As this contain main() method)
import java.util.ArrayList;
import java.util.Scanner;
public class TestClass {
public static void main(String[] args) {
//Declaring variables
double a,b;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Creating an array list object
ArrayList ar=new ArrayList();
ar.add(\"\");
//This loop continues to execute until the user entered \'q\' or \'Q\'
while(true)
{
//Displaying the contents inside the array list
System.out.print(\"Current stored:\");
System.out.print(\"[\");
//This for loop will iterate over the array list
for(Object obj:ar)
{
System.out.print(obj.toString()+\" \");
}
System.out.print(\"]\");
//Displaying the menu
System.out.print(\"\ Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit(q)?\");
char choice=sc.next(\".\").charAt(0);
//Based on the user\'s choice the corresponding case will get executed
switch(choice)
{
//If the user entered \'c\' or \'C\' this case will get executed
case \'c\':
case \'C\':
{
//Getting the values entered by the user
System.out.print(\"Enter two values:\");
a=sc.nextDouble();
b=sc.nextDouble();
//Creating the ComplexNumber class object
ComplexNumber c=new ComplexNumber(a, b);
//Adding the object to the array list
ar.add(c);
continue;
}
//If the user entered \'p\' or \'P\' this case will get executed
case \'p\':
case \'P\':
{
//Getting the values entered by the user
System.out.print(\"Enter two values:\");
a=sc.nextDouble();
b=sc.nextDouble();
//Creating the Point class object
Point p=new Point(a, b);
//Adding the point class object to the array list
ar.add(p);
continue;
}
//If the user entered \'f\' or \'F\' this case will get executed
case \'f\':
case \'F\':
{
//Getting the values entered by the user
System.out.print(\"Enter two values:\");
a=sc.nextDouble();
b=sc.nextDouble();
//Creating the fraction class object
Fraction f=new Fraction(a, b);
//Adding the fraction class object to the array list
ar.add(f);
continue;
}
/* If the user entered \'q\' or \'Q\' this case will get executed
* and control will come out of the loop
*/
case \'q\':
case \'Q\':
{
break;
}
//If the user entered invalid choice then this case will get executed
default:
{
System.out.println(\"** Invlid.Please Enter Valid Choice **\");
continue;
}
}
break;
}
}
}
___________________
Output:
Current stored:[ ]
Do you want to add a (c)omplex, 2D (p)oint, (f)raction or quit(q)?c
Enter 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(q)?p
Enter 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(q)?f
Enter 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(q)?c
Enter 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)?q
________Thank You




