Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

design a class ‘complex ‘with data members for real and imaginary part. provide default and parameterized constructors. write a program to perform arithmetic operations of two complex numbers.

#include<iostream>
using namespace std;
class Complex
{
 private:
  float real,img;
 public:
  Complex()
  {
   real=0;
   img=0;
  }
  void accept()
  {
   cout<<"Enter the complex number:"<<"
";
   cout<<"Real:";
   cin>>real;
   cout<<"Imaginary:";
   cin>>img;
  }
  void display()
  {
   cout<<"complex number is:";
   cout<<real<<"+"<<img<<"i"<<"
";
  }
  Complex(float a,float b)
  {
   real=a;
   img=b;
  }
  friend Complex operator +(Complex c1,Complex c2)
  {
   c1.real=c1.real+c2.real;
   c1.img=c1.img+c2.img;
   return c1;
  }

  friend Complex operator -(Complex c1,Complex c2)
  {
   c1.real=c1.real-c2.real;
   c1.img=c1.img-c2.img;
   return c1;
  }
  Complex operator *(Complex c2);
  Complex operator /(Complex c2);
};
Complex Complex::operator *(Complex c2)
{
 Complex c3;
 c3.real=(real*c2.real)-(img*c2.img);
 c3.img=(real*c2.img)+(img*c2.real);
 return c3;
}
Complex Complex::operator /(Complex c2)
{
 Complex c3;
 c3.real=((real*c2.real)+(img*c2.img))/((c2.real*c2.real)+(c2.img*c2.img));
 c3.img=((real*c2.img)+(img*c2.real))/((c2.real*c2.real)+(c2.img*c2.img));
 return c3;
}
int main()
{
 int ch;
 Complex c3;		//default constructor
 Complex c4(4,5);		//parameterized constructor
 
 Complex c5;
 c5.accept();
 cout<<"1st";
 c4.display();
 cout<<"
";
 cout<<"2nd";
 c5.display();
 cout<<"
";

 
 do
 {
 cout<<"
"<<"Enter your choice:";
 cout<<"1.Addition"<<"
"<<"2.Substraction"<<"
"<<"3.Multiplication"<<"
"<<"4.Division"<<"
";
 cin>>ch;
 switch(ch)
 {
  case 1: 	cout<<"Addition:";
 		//c3=c4+c5;
 		c3=operator+(c4,c5);
 		c3.display();
 		cout<<"
";
 		break;
  case 2:   cout<<"Substraction:";
		// c3=c4-c5;
 		c3=operator-(c4,c5);
 		c3.display();
		cout<<"
";
		break;
  case 3:   cout<<"Multication:";
 		c3=c4*c5;
 		//c3=c4.operator*(c5);
 		c3.display();
 		cout<<"
";
		break;
  case 4:	cout<<"Division:";
		// c3=c4/c5;
 		c3=c4.operator/(c5);
 		c3.display();
 		cout<<"
";
		break;
  default:
 		cout<<"EXIT";
 }
 }while(ch<=4);
 return 0;
}
Comment

PREVIOUS NEXT
Code Example
Java :: How to activate an entity listener for all entities 
Java :: align button to the left in JPanel 
Java :: java method parameters 
Java :: what is constructor in java 
Java :: Error:java: error reading C:UsersMeriem.m2 epositoryorgapachecommonscommons-lang33.12.0commons-lang3-3.12.0.jar; error in opening zip file 
Java :: java map tostring 
Java :: How to merge two sorted arrays in Java? 
Java :: crit chance in java 
Java :: javafx edit list 
Java :: generate random number in java within a range without repeating with android studio 
Java :: mobile network ip address permission android 
Java :: how to set path for java in windows 10 
Java :: get free player inventory slots spigot 
Java :: quebra de linha java 
Java :: java compare lists 
Java :: give ram to java 
Java :: fullscreen libgdx 
Java :: android java show hide keyboard in AndroidManifest 
Java :: reader from string java 
Java :: javafx get button id 
Java :: Java Program to Change the Border of a JFrame: 
Java :: java system.out.println shortcut 
Java :: jhow to check if a string is a punctuation java 
Java :: exception in java 
Java :: share location in android programmatically 
Java :: java abstract modifier 
Java :: fibonacci of 6 
Java :: java list of a class has a string that is equal to 
Java :: java string util if empty default 
Java :: how to print array in one line in java 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =