Write a Complex number class. It will have a default constructor, explicit const
ID: 3533943 • Letter: W
Question
Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read () public Complex add(Complex), public Complex subtract(Complex),public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a complexToString() method. Include get and set methods as well. You will also need a public void setUpComplexAnswer(), that will build an internal Complex object to each of the complex objects used in the math operations. (Choose descriptive variable names: a, b, c, and d are not acceptable variable names.). Also, no arrays please.
ComplexNumberDemoEngine class:
package complexNumbers;
import java.util.Scanner;
public class ComplexNumberDemoEngine
{
/**
* This program tests the Complex number class.
* 1. Ask user to input the real part of the first complex number.
* 2. Ask user to input the imaginary part of the first complex number.
* 3. Ask user to input the real part of the first complex number.
* 4. Ask user to input the imaginary part of the first complex number.
* 5. Output to the terminal the two complex numbers.
* 6. Print the options menu.
* 7. Ask the user to select the operation to be performed.
* 8. Accept input from user and use a switch() to call the appropriate
* Complex math method(), and then print the answer.;
* 8.a If invalid entry loop back to 7.
*
* 9. Ask the user if they would like to try another number combination.
* 9.a Use do-while to check for yes or no answer.
* 9.a.1 If yes go to 1.
* 9.a.2 If no quit.
*/
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
Complex myFirstNumber = null;
Complex mySecondNumber = null;
Complex myAnswer = null;
myFirstNumber = new Complex();
mySecondNumber = new Complex();
myAnswer = null;
//This builds an internal Complex number, in each of the Complex numbers
//listed below, that will be used within the Complex number
//so the Complex class math operations can return an
//answer without needing to pass in a Complex object.
myFirstNumber.setUpComplexAnswer();
mySecondNumber.setUpComplexAnswer();
String yesNo = "yes";
boolean invalidInput = false;
// String firstNumber = "";
// String secondNumber = "";
System.out.println("This program will perform basic math on Complex numbers");
do
{
System.out.println("Please input your first number.");
myFirstNumber.read();
// firstNumber = myFirstNumber.complexToString();
System.out.println("Please input your second number.");
mySecondNumber.read();
// secondNumber = mySecondNumber.complexToString();
System.out.println("You input the following Complex numbers: " +
myFirstNumber.complexToString() + " " +
mySecondNumber.complexToString());
System.out.println("You may perform one of the following opperations:");
System.out.println("1. Add");
System.out.println("2. Subtract the first number from the second number");
System.out.println("3. Subtract the second number from the first number");
System.out.println("4. Multiplication");
System.out.println("5. Divide the first number by the second number");
System.out.println("6. Divide the second number by the first number");
System.out.println("7. Quit");
do
{
invalidInput = false;
switch(keyboard.nextLine())
{
case "1":
System.out.println("Adding " + myFirstNumber.complexToString() + " to " + mySecondNumber.complexToString());
myAnswer = myFirstNumber.add(mySecondNumber);
printAnswer(myAnswer);
break;
case "2":
System.out.println("Subtracting " + myFirstNumber.complexToString() + " from " + mySecondNumber.complexToString());
if(myFirstNumber.equals(mySecondNumber))
System.out.println("(This should be interesting as they are equal to one another!)");
myAnswer = mySecondNumber.subtract(myFirstNumber);
printAnswer(myAnswer);
break;
case "3":
System.out.println("Subtracting " + mySecondNumber.complexToString() + " from " + myFirstNumber.complexToString());
if(myFirstNumber.equals(mySecondNumber))
System.out.println("(This should be interesting as they are equal to one another!)");
myAnswer = myFirstNumber.subtract(mySecondNumber);
printAnswer(myAnswer);
break;
case "4":
System.out.println("Multiplying " + myFirstNumber.complexToString() + " by " + mySecondNumber.complexToString());
myAnswer = mySecondNumber.multiply(myFirstNumber);
printAnswer(myAnswer);
break;
case "5":
System.out.println("Dividing " + myFirstNumber.complexToString() + " by " + mySecondNumber.complexToString());
myAnswer = myFirstNumber.divide(mySecondNumber);
printAnswer(myAnswer);
break;
case "6":
System.out.println("Dividing " + mySecondNumber.complexToString() + " by " + myFirstNumber.complexToString());
myAnswer = mySecondNumber.divide(myFirstNumber);
printAnswer(myAnswer);
break;
case "7":
break;
default:
invalidInput = true;
System.out.println("Please enter an integer from 1 to 7.");
break;
}
}while(invalidInput);
do
{
System.out.println("Please enter yes or no");
yesNo = keyboard.nextLine();
}while(!yesNo.equalsIgnoreCase("yes") && !yesNo.equalsIgnoreCase("no"));
}while(yesNo.equalsIgnoreCase("yes"));
System.out.println("Goodbye!!");
}
private static void printAnswer(Complex myAnswer)
{
System.out.println("Produces: " + myAnswer.complexToString());
System.out.println("Would you like to enter new numbers?");
}
}
Explanation / Answer
As per your requirement, made the various options. checked the working. All the functionalities are working fine. Validated the output with calcultor.
Code
import java.io.*;
class ComplexNumber {
// Define variables for real part, imaginary part, modulus, argument etc.
double real,imag; // ComplexNumber Number is real + i imag
double modulus,argument;
double product,creal,cimag;
String print;
ComplexNumber c;
/** This is the constructor. It initializes the x and y variables */
public ComplexNumber(double x, double y)
{this.real = x;this.imag = y;}
// setter method for real
void setReal (double realIn) {
this.real = realIn;
}
// setter method for imag
void setImag (double imagIn) {
this.imag = imagIn;
}
// getter methods for real an imaginary
double getReal () { return this.real; }
double getImag () { return this.imag; }
// setter method for modulus
void setModulus () {
this.modulus = Math.sqrt(real*real + imag*imag);
}
// setter method for argument
void setArgument () {
this.argument = Math.atan(imag/real);
}
// Add ComplexNumber
public static ComplexNumber add(ComplexNumber a, ComplexNumber b) {
ComplexNumber c = new ComplexNumber(0,0);
c.real = (a.real+b.real);
c.imag = (a.imag+b.imag);
return (c);
}
// Subtract ComplexNumber
public static ComplexNumber subtract(ComplexNumber a, ComplexNumber b) {
ComplexNumber c = new ComplexNumber(0,0);
c.real = (a.real-b.real);
c.imag = (a.imag-b.imag);
return (c);
}
// Multiply ComplexNumber - this is a method which will be called ComplexNumber.multiply
// when used in other objects
public static ComplexNumber multiply(ComplexNumber a, ComplexNumber b) {
ComplexNumber c = new ComplexNumber(0,0);
c.real = (a.real*b.real-a.imag*b.imag);
c.imag = (a.real*b.imag+a.imag*b.real);
return (c);
}
// Divide ComplexNumber
public static ComplexNumber divide(ComplexNumber a, ComplexNumber b)
{
ComplexNumber c = new ComplexNumber(0,0);
c.real= (a.real*b.real+a.imag*b.imag)/(b.real*b.real+b.imag*b.imag);
c.imag= (a.imag*b.real-a.real*b.imag)/(b.real*b.real+b.imag*b.imag);
return (c);
}
// setter method for writing out the ComplexNumber number as a string
void printString () {
this.print = "{" + real + "+" + imag + "i" + "}" ;
System.out.println(this.print);
}
// equals
public boolean equals(ComplexNumber o)
{
if (o.real==real&&o.imag==imag)
return true;
else
return false;
}
}
// End of the ComplexNumber class
class ComplexNumberTest
{
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
public static void main (String argv []) throws IOException
{
ComplexNumber cx = new ComplexNumber(8,9);
System.out.println("input real part of the first number");
double x = new Double(keyboard.readLine()).doubleValue();
System.out.println("input imaginary part of the first number");
double y = new Double(keyboard.readLine()).doubleValue();
System.out.println("input real part of second number");
double x1 = new Double(keyboard.readLine()).doubleValue();
System.out.println("input imaginary part of second number");
double y1 = new Double(keyboard.readLine()).doubleValue();
System.out.println("The first ComplexNumber number you typed is");
// define the ComplexNumber number
cx.real = x;
cx.imag = y;
// Now use methods of ComplexNumber class to set other things
cx.setModulus();
cx.setArgument();
cx.printString();
System.out.println("The second ComplexNumber number you typed is");
// define the ComplexNumber number
ComplexNumber cy = new ComplexNumber(0,0);
cy.real = x1;
cy.imag = y1;
cy.setModulus();
cy.setArgument();
cy.printString();
while(true)
{
System.out.println("You may perform one of the following opperations:");
System.out.println("1. Add");
System.out.println("2. Subtract the first number from the second number");
System.out.println("3. Subtract the second number from the first number");
System.out.println("4. Multiplication");
System.out.println("5. Divide the first number by the second number");
System.out.println("6. Divide the second number by the first number");
System.out.println("7. Quit");
int i;
i=Integer.parseInt(keyboard.readLine());
if(i==7)
System.exit(1);
ComplexNumber complex = new ComplexNumber(8,9);
switch(i)
{
case 1:
System.out.println("Sum of two Complex numbers is");
ComplexNumber.add(cx,cy).printString();
break;
case 2:
System.out.println("Difference of first number from the second number is");
ComplexNumber.subtract(cx,cy).printString();
break;
case 3:
System.out.println("Difference of second number from the first number is");
ComplexNumber.subtract(cy,cx).printString();
break;
case 4:
System.out.println("Product of two Complex numbers is ");
ComplexNumber.multiply(cx,cy).printString();
break;
case 5:
System.out.println("Division of first number from the second number is");
ComplexNumber.divide(cx,cy).printString();
break;
case 6:
System.out.println("Division of second number from the first number is");
ComplexNumber.divide(cy,cx).printString();
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.