Benzin
Member
+576|6216
I cannot get this program to run. It will compile no problem, but it will not run properly and allow me to input the necessary values.

I have to basically build a calculator that will allow me to input double values and calculate complex numbers. My only problem is reading the inputs, it is NOT working. The input has to follow this format: 1.1 1.1 + 1.1 1.1 * 2.2 2.2

The second number of each pair is the Bi number. Obviously the i is being left out because it isn't necessary. The objects at the bottom nested into the if-statements are all in order in the class that contains them. I just need help getting the program to read my inputs.

import java.util.Scanner;

public class Calculator
{
   
    public static void main (String args[])
    {
       
        double Real1;
        double Real2;
        double Real3;
   
        double Imag1;
        double Imag2;
        double Imag3;
   
        double RResult;
        double IResult;
   
        char operator1;
        char operator2;
   
        Scanner input = new Scanner(System.in);

        Complex calc = new Complex();

        System.out.printf( "Geben Sie bitte 3 komplexe Zahlen und 2 Operatoren ein: " );

        Real1 = input.nextDouble();
        Imag1 = input.nextDouble();
       
        String str1 = input.nextLine();
        operator1 = str1.charAt(0);

        //operator1 = operator1.charAt(0);
       
        Real2 = input.nextDouble();
        Imag2 = input.nextDouble();
       
        String str2 = input.nextLine();
        operator2 = str2.charAt(1);

        Real3 = input.nextDouble();
        Imag3 = input.nextDouble();

        if (operator1 == '+' && operator2 == '*')
        {
            calc.complexAddMult(Real1,Real2,Real3,Imag1,Imag2,Imag3);
            calc.displayResult();
        }
       
        if (operator1 == '+' && operator2 == '-')
        {
            calc.complexAddSub(Real1,Real2,Real3,Imag1,Imag2,Imag3);
            calc.displayResult();
        }
       
        if (operator1 == '-' && operator2 == '+')
        {
            calc.complexSubAdd(Real1,Real2,Real3,Imag1,Imag2,Imag3);
            calc.displayResult();
        }
       
        if (operator1 == '-' && operator2 == '*')
        {
            calc.complexSubMult(Real1,Real2,Real3,Imag1,Imag2,Imag3);
            calc.displayResult();
        }
       
        if (operator1 == '*' && operator2 == '+')
        {
            calc.complexMultAdd(Real1,Real2,Real3,Imag1,Imag2,Imag3);
            calc.displayResult();
        }
       
        if (operator1 == '*' && operator2 == '-')
        {
            calc.complexMultSub(Real1,Real2,Real3,Imag1,Imag2,Imag3);
            calc.displayResult();
        }
       
    }

}
Benzin
Member
+576|6216
btw ... I need this solved tonight. The assignment is due today and there are no tutors here in the programming lab until 4pm and I am in class the whole time that the tutors are here. The class is an exercise group and attendance is required, so now I cannot skip it and meet with a tutor.
Benzin
Member
+576|6216
Really could use some help, guys ...
zeidmaan
Member
+234|6632|Vienna

in a sec

edit:

If Im getting you right, you are making a mistake in this part:

String str1 = input.nextLine();
operator1 = str1.charAt(0);

By using nextLine() for the first operator your scanner is reading everything till the end of the line including next 4 numbers and the second operator.
I doesnt seem like a problem because you use the CharAt(0) to get just the operator. But the problem is that after the nextLine() reads all the numbers it "positions" the Scanner at the beggining of the next line (and there is no next line).

simple example:

q w e r t z
y x c v b n

first input.next() would return "q"
second input.next() would return "w"
input.nextLine() would return "e r t z"
But if you used input.next() again you would get "y"


Try just making your operators as strings and using input.next()

Real1 = input.nextDouble();
Imag1 = input.nextDouble();
String operator1 = input.next();

It would help to have the whole text of your assignemt.

Last edited by zeidmaan (2009-04-30 06:17:30)

Benzin
Member
+576|6216
the instructions are all in German ... damn good thing you responded, Zeidmann ... hahaha

Eine komplexe Zahl soll durch eine unveränderliche Klasse namens Complex dargestellt werden.

Diese Klasse besitzt 2 Datenelemente vom Typ double die den Realteil und den Imaginärteil der komplexen Zahl repräsentieren. Diese Datenfelder werden einmal gesetzt und sollen im Programmverlauf nicht mehr verändert werden können. Benutzen Sie hier die entsprechenden Modifier bei der Definition der Datenelemente.

Diese Klasse soll die folgenden (öffentlichen) Methoden besitzen:

    * public Complex add(Complex c), die die Zahl c dazuaddiert und als neues Objekt zurückgibt.
    * public Complex sub(Complex c), die die Zahl c subtrahiert und als neues Objekt zurückgibt.
    * public Complex mul(Complex c), die die komplexe Zahl mit einer komplexen Zahl c multipliziert und das Produkt als neues Objekt zurückgibt. Beachten Sie die Rechenregeln für komplexe Zahlen. Wenn 2 komplexe Zahlen in der Darstellung (realteil;imaginärteil) multipliziert werden, so ist das Produkt (r1;i1)*(r2;i2) = (r1*r2-i1*i2;r1*i2+i1*r2). Siehe auch den Wikipediaeintrag zum Thema Komplexe Zahl.

Der Konstruktor hat 2 Parameter vom Typ double, den Realteil und den Imaginärteil der zu erzeugenden Zahl.

Zuerst wird eine komplexe Zahl als Startwert eingelesen. Danach werden abwechselnd ein Operator und eine komplexe Zahl eingelesen und die angeforderte Rechenopertion durchgeführt. Die Operatorpriorität (Punktrechnung vor Strichrechnung) soll nicht berücksichtigt werden. Sobald der nächste Operator und Operand eingelesen wurde wird sofort ausgewertet und das Ergebnis für eine ev. nachfolgende Operation benutzt (siehe auch Beispiele unten).
Benzin
Member
+576|6216
OK, I did what you said to do but now I am not getting any kind of output. It's reading them in properly whether I press Enter at the end or after each number /operator that I put in, but it's not giving me any output. FUCK!
Benzin
Member
+576|6216
Here is the object that I am calling in the beginning and the methods, maybe the mistake lies there somewhere:


public class Complex
{
   
    double RResult;
    double IResult;
    double RealAdd;
    double ImagAdd;

    public void complexAddMult(double Real1 , double Real2 , double Real3 , double Imag1 , double Imag2 , double Imag3)
    {

        RealAdd = Real1 + Real2;
        ImagAdd = Imag1 + Imag2;
        RResult = ((RealAdd*Real3)-(ImagAdd*Imag3));
        IResult = ((RealAdd*Imag3)+(Real3*ImagAdd));
        System.out.println("WIN");
   
    }
   
    public void complexAddSub(double Real1 , double Real2 , double Real3 , double Imag1 , double Imag2 , double Imag3)
    {
   
        RResult = Real1 + Real2 - Real3;
        IResult = Imag1 + Imag2 - Imag3;
   
    }
   
    public void complexSubAdd(double Real1 , double Real2 , double Real3 , double Imag1 , double Imag2 , double Imag3)
    {
       
        RResult = Real1 - Real2 + Real3;
        IResult = Imag1 - Imag2 + Imag3;
   
    }
   
    public void complexSubMult(double Real1 , double Real2 , double Real3 , double Imag1 , double Imag2 , double Imag3)
    {
       
        RealAdd = Real1 - Real2;
        ImagAdd = Imag1 - Imag2;
        RResult = ((RealAdd*Real3)+(ImagAdd*Imag3));
        IResult = ((RealAdd*Imag3)+(Real3*ImagAdd));
       
    }
   
    public void complexMultAdd(double Real1 , double Real2 , double Real3 , double Imag1 , double Imag2 , double Imag3)
    {
       
        RealAdd = ((Real1+Real2)+(Imag1*Imag2));
        ImagAdd = ((Real1*Imag2)+(Real2*Imag1));
        RResult = RealAdd + Real3;
        IResult = ImagAdd + Imag3;
       
    }
   
    public void complexMultSub(double Real1 , double Real2 , double Real3 , double Imag1 , double Imag2 , double Imag3)
    {
   
        RealAdd = ((Real1+Real2)+(Imag1*Imag2));
        ImagAdd = ((Real1*Imag2)+(Real2*Imag1));
        RResult = RealAdd - Real3;
        IResult = ImagAdd - Imag3;
   
    }

    public void displayResult()
    {
   
        System.out.printf( "Resultat ist: %d %d " , RResult , IResult);
   
    }   

}



Zeidmaan, if you're at TU, would you like to meet for a study session one time?

Last edited by CapnNismo (2009-04-30 07:33:25)

zeidmaan
Member
+234|6632|Vienna

I see.... you are doing it wrong

You are supposed to make is so that it reads the first complex number as a starting value. Than it should go on reading an operator and the second complex number and than do the calculation right away. Than read the next operator and next complex number - calculate - reapeat etc.

Basically you read 2 starting values (in you example) 1.1 1.1
You make a complex number from those 2 doubles. You should have a Constructor in your Complex class that receives 2 doubles (real and imag). Lets call it "finalResult"

Than you read the operator (in your exapmple "+")

than you read 2 more doubles (1.1 1.1) and make a second complex number. lets call it complex1

than you use the method given
public Complex add(Complex c)  that should also be in you Complex.java class (you need to do the math though)

This method returns a Complex number that the sum of those previous 2 complex numbers. You should save it in the "finalResult".
so its actually:
finalResult=finalResult.add(complex1);

Than you read a "*", you read 2 more doubles, make a new Complex (complex2) and than multiply your "finalResult" with "complex2" using
finalResult=finalResult.mul(complex2);
zeidmaan
Member
+234|6632|Vienna

Paste or pm me the whole text, every word

Object class is also wrong

I PMed you my msn.

Last edited by zeidmaan (2009-04-30 08:04:33)

Board footer

Privacy Policy - © 2024 Jeff Minard