Complex Number IO Help

  • Thread starter janicethorne via JavaKB.com
  • Start date
J

janicethorne via JavaKB.com

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

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200604/1
 
R

Remon van Vliet

janicethorne via JavaKB.com said:
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

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200604/1

You'll either have to create a simple GUI for your uses (google on AWT) or
use System.in
 
A

Alex Hunsley

janicethorne said:
Remon:

Thank you. I can do that.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top