Complex Numbers with input from keyboard

(Complex Numbers) Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form
realPart + imaginaryPart * i
where i is
Write a program to test your class. Use int type variables to represent the private data of the class. All values should taken input.
a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Print Complex numbers in the form (realPart, imageinaryPart).

solution:

import java.util.Scanner;
class part{
int real;
int complex;
int real1;
int complex1;
part(){
Scanner input =new Scanner(System.in);
System.out.println("Enter first real value: ");
real=input.nextInt();
System.out.println("Enter first imaginary value: ");
complex=input.nextInt();
System.out.println("Your first value is:"+"a="+"("+real+"x+i"+complex+"y"+")");

System.out.println("Enter your second real value :");

real1=input.nextInt();
System.out.println("Enter second real value: ");
complex1=input.nextInt();
System.out.println("Your second value is:"+"a="+"("+real1+"x+i"+complex1+"y"+")");

}
void add(){
System.out.println("Adding two values: ");
int real2=real+real1;
int complex2=complex+complex1;
System.out.println("After adding:"+real2+"x"+"+i"+complex2+"y");

}
void substruct(){
System.out.println("substracting two values : ");

int real3=real-real1;
int complex3=complex-complex1;
if(complex3>=0)
System.out.println("After substracting values: "+real3+"x"+"-"+complex3+"i");
else
System.out.println("After substracting values: "+real3+"x"+""+complex3+"i");

}

}
public class realcomplex {
public static void main(String arg[]){

part oikoi=new part();

oikoi.add();
oikoi.substruct();

}
}

Advertisements

One thought on “Complex Numbers with input from keyboard

  1. ruhina says:

    kisui bujhte pari nai 😦

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s