cancel
Showing results for 
Search instead for 
Did you mean: 

Help!!!!

Former Member
0 Kudos

I wrote the complex class, but I'm not sure if it is okay. But I don't know how to write the Icalc class.

Could someone please help me.

thanks in advance.

Here are the instructions:

The Program

Your program will be a four function (+, -, *, /) calculator for complex numbers. It will allow the user to enter complex numbers and do simple arithmetic on them.

Recall that a complex number has the form a + bi where a and b are real numbers and i is the square root of -1. a is called the real part and b is called the imaginary part (a historic but unfortunate choice of terminology).

The program will keep a current value which will initially be 0+0i. The program will begin by displaying the current value and will then go into a loop. In the loop it will display a prompt:

+, -, *, /, c, or q (to quit):

The user will then enter a choice of action. If the user chooses + the program will prompt:

Complex number to add:

and accept a complex number. It will change the current value by adding the user's value to it and then display the current value. It will perform similarly for the other arithmetic operations, i.e. get a complex value from the user, then subtract it from the current value, multiply the current value by it, or divide the current value by it, and display the new current value. If the user chooses c (for clear) the current value will be reset to 0+0i and displayed. The loop will continue until the user chooses q. If the user enters anything other than one of these choices the program will give a "bad input" message and continue with the next pass through the loop.

Complex numbers will be input as a number (integer or fixed point real) for the real part, then +, then another number for the imaginary part, and finally i, all with no intervening spaces. Examples of legal input are:

3+4i

3.14159+17i

-12.0+8i

6+-77i

0+1i

1+0i

Notice that the real and imaginary parts must always be included. For example the number 1.4 viewed as a complex number would be entered as 1.40i and the number i would be entered as 01i.

Running the program may look like:

0.0+0.0i

+, -, *, /, c, or q (to quit): +

Complex value to add: 2+6i

2.0+6.0i

+, -, *, /, c, or q (to quit): /

Complex value to divide by 0+1i

6.0+-2.0i

+, -, *, /, c, or q (to quit): -

Complex value to subtract: -3.1426+-8i

9.1426+6.0i

+, -, *, /, c, or q (to quit): *

Complex value to multiply: 2+0i

18.2852+12.0i

+, -, *, /, c, or q (to quit): c

0.0+0.0i

+, -, *, /, c, or q (to quit): q

Complex Arithemetic

Recall how arithmetic is done on complex numbers. Let abi and cdi be two complex numbers. The arithmetic operations are:

Addition

(a+bi) + (cdi) = (ac) + (b+d)i

Subtraction

(abi) - (cdi) = (a-c) + (b-d)i

Multiplication

(abi)*(cdi) = (ac - bd) + (ad + bc)i

Division

(abi)/(cdi) = (ac + bd)/(c2+d2) + [(-ad + bc)/(c2+d2)]i

The Form

Your program will be written in two classes: Complex and ICalc. The Complex class will represent complex numbers and provide their operations. The ICalc class will be the calculator. It will store complex numbers as instances of Complex and use the public methods of Complex to perform the arithmetic.

The Class Complex

This class will contain two private instance variables, two constructors, two public methods to set the real and imaginary parts of the stored value, a public method for each of the arithmetic operations, and a toString method to be described below.

The instance variables are private doubles real and imag for the imaginary part of the complex value. These are the only instance variables.

One constructor will take no parameters and assign 0 to each of the instance variables. The other will take two doubles as parameters, use the first to initialize real and the other to initialize imag.

The setReal and setImag methods will each take one (double) parameter and assign it to real and imag respectively.

Each of the arithmetic methods will take one Complex parameter. The method (for example add) will create an instance of Complex and give it the value found by adding the value of the current instance (this) and the parameter. This new Complex holding the sum will then be returned. An appropriate add method is:

public Complex add(Complex c)

{

Complex t = new Complex(real, imag);

t.real += c.real;

t.imag += c.imag;

return t;

}

This method could be used elsewhere to add two complex numbers as:

Complex c1 = new Complex(3, 4.1);

Complex c2 = new Complex(1, 1);

Complex sum = c1.add(c2);

In this example we are sending c1 the message "add c2 to yourself and return the sum." This message is then carried out by the method c1.add. The other arithmetic operations are written similarly but require some thought.

You should copy the following toString method directly into your Complex class. Once this method is defined for the class an instance of Complex can be displayed using the System.out.print and System.out.println methods just as you would display an int or a string:

Complex c = new Complex(4,9);

System.out.println(c); // will display "4.0+9.0i"

The method for you to include:

public String toString()

{

return Double.toString(real) + "+" + Double.toString(imag) + "i";

}

The Class ICalc

ICalc will contain your main and a method which can be used to read a Complex from the keyboard.

You will include the following method in this class after main:

private static Complex nextComplex()

{

Complex c = new Complex();

String inStr = kbd.next(); // read input as a String

// find end of "real part" of string

int plusIndex = inStr.indexOf('+');

// extract "real part" as substring

String realPart = inStr.substring(0, plusIndex);

// extract "imaginary part" as substring

String imagPart = inStr.substring(plusIndex + 1, inStr.length() - 1);

// convert substrings to doubles and assign to Complex c

c.setReal(Double.parseDouble(realPart));

c.setImag(Double.parseDouble(imagPart));

return c;

}

This method must be declared static since it is in the class ICalc from which no instances will be created, and because it will be called from main which is static.

You will begin ICalc by creating a Scanner object. This will be global to the class rather than local to main since it will be used by both main and nextComplex(). Use the following line:

private static Scanner kbd = new Scanner(System.in);

Inside main create an instance, current, of Complex and initialize it to 0+0i. The remainder of main is a while loop:

boolean goAgain = true;

while(goAgain)

{

System.out.print("+, -, *, /, c, or q (to quit): ");

String ans = kbd.next();

if (...)

{

// stuff

}

else if (...)

{

// stuff

else if (...)

{

// stuff

}

...

else if (ans.equals("q"))

goAgain = false;

else

System.out.println("bad input");

}

In each of the conditions for the ifs you will check the user's response with ans.equals() with the choice ("+", "-", etc.) as parameter. Inside the block you will take the appropriate action, which in most cases, will be to prompt the user for a new complex number, accept the user's input using the nextComplex method, apply an arithmetic operation by calling a method from Complex to modify current, and display the new value of current. See the above example to see what should be accomplished with each possible choice. The final else will be invoked only if the user made an improper choice and will display an error message.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Claire,

I'm not sure how you get here and if this is the right forum for you.

This is the SAP Web AS Java programming forum and all threads are somehow SAP Java related.

I mean for your solution you even won't need an application server or web container neither such a complex environment as SAP Web AS.

A simple java app will do the job.

I think what's intended by your class'es tutor is to write your first simple architecture:

- Complex.java implements your business logic, here providing the operations on complex numbers (what you say you've implemented already).

- LCalc.java implements a simple user interface to invoke the calculator logic. Here you have to read input from console (the exercise suggested Scanner for some reason, there are other ways too), parse the input, call Complex depending on input and display the result.

For more information I adivse you to visit another, more general Java forum on the net. And also, with the hints already given (I mean 60% of the needed code is already outlined), looking up the rest in the Java API doc would do the job.

Regards,

Carsten