Declares and tests a complex number class, which includes multiplication and subtraction of complex numbers

Source: Internet
Author: User
Tags gety

It was an exercise after class. Write down the code here.

Import Java.util.Scanner;
public class Complex {
public int a=0;
public int b=0;
Public Complex () {
System.out.println ("Please enter a plural");
Scanner s=new Scanner (system.in);
String T=s.next ();
This.a=getx (t);
This.b=gety (t);
}
public int GetX (String x) {//Get the previous integer of the input data
String m;
M=x.split ("[+]") [0];
Return Integer.parseint (m);
}
public int GetY (String y)//Gets the last integer of the input data
{
String m;
M=y.split ("[+]") [1].replace ("I", "");
Return Integer.parseint (m);
}
public void Add ()
{
System.out.println ("addition:");
System.out.println ("Please enter a plural");
Scanner s=new Scanner (system.in);
String T=s.next ();
This.a=a+getx (t);
This.b=b+gety (t);
System.out.println ("complex number operation result is:" +a+ "+" +b+ "I");
}
public void subtraction ()
{
System.out.println ("subtraction:");
System.out.println ("Please enter a plural");
Scanner s=new Scanner (system.in);
String T=s.next ();
This.a=a-getx (t);
This.b=b-gety (t);
System.out.println ("complex number operation result is:" +a+ "+" +b+ "I");

}
public void multiplication () {
System.out.println ("multiplication:");
System.out.println ("Please enter a plural");
Scanner s=new Scanner (system.in);
String T=s.next ();
This.a=a*getx (t);
This.b=b*gety (t);
System.out.println ("complex number operation result is:" +a+ "+" +b+ "I");
}
public static void Main (String args[]) {
Complex c=new complex ();
C.add ();
C.subtraction ();
C.multiplication ();
}
}

After the operation, it is found that because the operation process is considered simple, the value of a,b is reused, so the first input cannot be the default value, the equivalent of subtraction and multiplication is to compute the result of addition.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.