Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Complex Number IO Help

Thread view: 
janicethorne - 09 Apr 2006 20:47 GMT
Hello:
My current assignmen is to create methods of arithmetic operations for two
sets of complex numbers (ordered pairs ab and cd) with the default value set
to (0, 0) for both. I was able (with some help from the internet) to
establish the code for this. However, I now need to take in values from a
user to override the default values. So if the default values of ab and cd
are (0, 0) and (0, 0), a user can type in (1, 2) and (3, 4) and add, subtract,
multiply and divide these complex numbers. As I said, I have the code for the
arithmetic, but can't get the user input part. I checked out a link that was
on this site that gave IO code and was stumped by the code - it didn't make
any sense to me.

I hope someone could help me - as you can tell by my previous posts, I am a
newbie. By the way, is it ok to post the code? As I said, I am a newbie.
Thanks so much.

import java.text.*;
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Program9  {

     public double real;
     public double imagn;

     //Constructor that initializes the values
    Program9  (double a, double b) {
          real = a;
       imagn = b;
    }

     //Get methods for real & imaginary numbers
     public double realPart() {
       return real;
     }

    public double imagnPart() {
       return imagn;
    }

    //Add method
    public void add (Program9 cvalue) {
          real = real + cvalue.real;
       imagn  = imagn  + cvalue.imagn;
     }//end add

     //Subtract method
    public void subtract (Program9 cvalue) {
       real = real - cvalue.real;
       imagn  = imagn  - cvalue.imagn;
    }//end subtract

     //Define a static add method that creates a new complex object with the
sum.
     public static Program9 add (Program9 cvalue1, Program9 cvalue2) {
       double a = cvalue1.real + cvalue2.real;
          double b = cvalue1.imagn  + cvalue2.imagn;
       return new Program9 (a,b);
     }//end sum method

     //Define a static subtraction method that creates a new complex object
with the product
   public static Program9 subtract (Program9 cvalue1, Program9 cvalue2) {
          double a = cvalue1.real - cvalue2.real;
         double b = cvalue1.imagn  - cvalue2.imagn;
       return new Program9 (a,b);
     }//end subtract method

     //Provide the absolute value of the ordered pairs
     public double modulus () {
       return Math.sqrt((real * real) + (imagn * imagn));
     }//end abvalue

     //Multiply this object by the argument
    public void multiply (Program9 cvalue) {
       double a2 = real * cvalue.real - imagn * cvalue.imagn;
       double b2 = real * cvalue.imagn  + imagn * cvalue.real;
           real = a2;
           imagn = b2;
     }//end multiply  arg

     //Define a static multiply method that creates a new object with the
product.
    public static Program9 multiply (Program9 cvalue1, Program9 cvalue2) {
       double a2 = cvalue1.real * cvalue2.real - cvalue1.imagn * cvalue2.imagn;
       double b2 = cvalue1.imagn * cvalue2.real + cvalue1.real * cvalue2.imagn;
          return new Program9(a2, b2);
    }//end multiply method

     //Divide this object by the argument
    public void divide (Program9 cvalue) {
       double dvd = cvalue.real * cvalue.real + cvalue.imagn * cvalue.imagn;
       double a = real * cvalue.real + imagn * cvalue.imagn;
        double b = imagn * cvalue.real - real * cvalue.imagn;
           real = a/dvd;
           imagn  = b/dvd;
     }//end divide arg

    //Define a static divide method that creates a new object with the result of
     public static Program9 divide (Program9 cvalue1, Program9 cvalue2) {
       double dvd = cvalue2.real * cvalue2.real + cvalue2.imagn  * cvalue2.
imagn;
        double a = cvalue1.real * cvalue2.real + cvalue1.imagn  * cvalue2.imagn;
        double b = cvalue1.imagn * cvalue2.real - cvalue1.real * cvalue2.imagn;
       return new Program9 (a/dvd, b/dvd);
    } // end divide method

    // Return a string representation of the complex value
    public String toString () {
       String imagn_sign = (imagn < 0) ? " - " : " + ";
       return (real +  imagn_sign + imagn + "i");
     }//end string method

    public static void main ( String args[] ) throws IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))
;

            // Create complex objects
            Program9 ab = new Program9 (0, 0);
            Program9 cd = new Program9 (0, 0);

        Program9 testAdd = Program9.add (ab,cd);
          Program9 testMult = Program9.multiply (ab,cd);
          Program9 testDivide = Program9.divide (ab,cd);
          Program9 testSubtract = Program9.subtract (ab,cd);

          System.out.println("Complex numbers " + ab + " and " + cd + " added are
" + testAdd);
          System.out.println("Complex number " + ab + " subtracted from " + cd + "
is " + testSubtract);
          System.out.println("Complex numbers " + ab + " and " + cd + " multiplied
together is " + testMult);
          System.out.println("Complex numbers " + ab + " divided by " + cd + "
equals " + testDivide);
    }//end of main
}//end of Program9

Signature

"The man who does not read good books has no advantage over the man who
cannot read them."

Remon van Vliet - 10 Apr 2006 12:35 GMT
> Hello:
> My current assignmen is to create methods of arithmetic operations for two
[quoted text clipped - 146 lines]
> }//end of main
> }//end of Program9

You'll either have to create a simple GUI for your uses (google on AWT) or
use System.in
janicethorne - 10 Apr 2006 18:59 GMT
>You'll either have to create a simple GUI for your uses (google on AWT) or
>use System.in

Remon:

Thank you. I can do that.

Janice

Signature

"The man who does not read good books has no advantage over the man who
cannot read them."

Alex Hunsley - 10 Apr 2006 23:55 GMT
>> You'll either have to create a simple GUI for your uses (google on AWT) or
>> use System.in
[quoted text clipped - 4 lines]
>
> Janice

Don't forget the possibility of using Swing as an alternative! (Unless I
missed something about you not being able to use Swing.) Swing is built
on top of AWT.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.