Write a class Complex to represent complex numbers in java. Objects of the type
ID: 3740316 • Letter: W
Question
Write a class Complex to represent complex numbers in java.
Objects of the type Complex should be described to to fields, re and im of the type double. The two fields re and im should be declared as private due to the constuction of the class. (fx if you later want to change to polar representation)
The class has to contain the following methods:
public Complex(double re, double im). A constructor for creating a new Complex-object with real part re and imaginary part im.
public Complex(Complex z). A constructor for creating a new Complex-object representing the same complex number as z.
public double getRe(). Returns the real part of the Complex-object.
public double getIm(). Returns the imaginary part of the Complex-object.
public double abs(). Returns modulus of the Complex-object. (?re2+im2)
public Complex plus(Complex other). Returns a Complex-object representing the sum of the current Complex-object and the Complex-object other. Remember: (a+bi)+(c+di) = (a+c)+(b+d)i
public Complex times(Complex other). Returns a Complex-object representing the product of the current Complex-object and the Complex-object other. Remember: (a+bi)·(c+di) = (ac?bd)+(bc+ad)i
public String toString(). Returns a string that represents the Complex-object in proper form (fx. the form "a + ib").
a+bi 2 = (ac + bd) + (bc – ad) c2 + d2 C+ diExplanation / Answer
import java.util.Stack;
public class Evaluator
{
public static int evaluate(String expression)
{
//create a character array to store each character of expression
char[] value = expression.toCharArray();
//create a stack for digits
Stack<Integer> stackVal = new Stack<Integer>();
// create a stack for Operators
Stack<Character> operand = new Stack<Character>();
for (int i = 0; i < value.length; i++)
{
// if char is empty then continue
if (value[i] == ' ')
continue;
//if current character is number then push it to stack stackVal
if (value[i] >= '0' && value[i] <= '9')
{
StringBuffer sbuf = new StringBuffer();
while (i < value.length && value[i] >= '0' && value[i] <= '9')
sbuf.append(value[i++]);
stackVal.push(Integer.parseInt(sbuf.toString()));
}
// if opening brace push to operand stack
else if (value[i] == '(')
operand.push(value[i]);
// if closing brace is encountered then solve the operation between opening and closing brace by calling applyOperation method
else if (value[i] == ')')
{
while (operand.peek() != '(')
stackVal.push(applyOperation(operand.pop(), stackVal.pop(), stackVal.pop()));
operand.pop();
}
// To check if any operand based on priority and push to stack and perform operation
else if (value[i] == '+' || value[i] == '-' ||
value[i] == '*' || value[i] == '/')
{
while (!operand.empty() && hasPrecedence(value[i], operand.peek()))
stackVal.push(applyOperation(operand.pop(), stackVal.pop(), stackVal.pop()));
operand.push(value[i]);
}
}
// To compute extire expression at end
while (!operand.empty())
stackVal.push(applyOperation(operand.pop(), stackVal.pop(), stackVal.pop()));
// Return value at top of stack
return stackVal.pop();
}
// Returns true if 'op2' has higher or same precedence as 'op1',
// otherwise returns false.
public static boolean hasPrecedence(char op1, char op2)
{
if (op2 == '(' || op2 == ')')
return false;
if ((op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-'))
return false;
else
return true;
}
//To apply an operator 'op' on operands 'a'
public static int applyOperation(char op, int b, int a)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
UnsupportedOperationException("Cannot divide by zero");
return a / b;
}
return 0;
}
public static void main(String[] args)
{
System.out.println(Evaluator.evaluate("200 * ( 3 + 16 ) / 10"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.