Implement complex addition, subtraction, multiplication, division using java

Source: Internet
Author: User

Use java to implement addition, subtraction, multiplication, division of plural numbers. 1. the background teacher assigned several java programming questions in the class. This is one of them. design a Complex class to encapsulate the following operations on a Complex number: (1) a constructor with parameters used to initialize a Complex number member (2) a constructor without parameters, call the constructor of the generation parameter to complete the initialization of the plural member. (3) Implement the addition and subtraction of two complex numbers of static methods and instance methods. (4) In the standard form of the plural number: x + iy outputs the plural number (5) and writes two functions to obtain the real part getReal (), getImage (), and virtual part of the plural respectively. The original question of the teacher is as follows: I used an instance method to perform the addition, subtraction, multiplication, division, and division of two complex numbers. If you want to write a static method, that is, a class method, you must add a static method and modify it based on the corresponding changes. The difference is that the instance method can call both instance variables and instance methods, and class variables and class methods. Only class variables and class methods can be called for class methods. Due to the time relationship, there will be classes tomorrow, and I will write instances for the moment. 3. Code and Explanation

<Span style = "font-family: SimSun; font-size: 14px"> package Four;/*** @ author Kun Sun * @ Date: 2013.10.15 */import java. util. compute; public class Complex {// Complex class double real; // real double image; // virtual Complex () {// constructor with no parameters required input = new partition (System. in); double real = input. nextDouble (); double image = input. nextDouble (); Complex (real, image);} private void Complex (double real, double image) {// call the construction method without parameters // TODO Auto-generated method stub this. real = real; this. image = image;} Complex (double real, double image) {// constructor with parameters this. real = real; this. image = image;} public double getReal () {return real;} public void setReal (double real) {this. real = real;} public double getImage () {return image;} public void setImage (double image) {this. image = image;} Complex add (Complex a) {// add the plural number to double real2 =. getReal (); double image2 =. getImage (); double newReal = real + real2; double newImage = image + image2; Complex result = new Complex (newReal, newImage); return result;} Complex sub (Complex) {// The Plural subtraction double real2 =. getReal (); double image2 =. getImage (); double newReal = real-real2; double newImage = image-image2; Complex result = new Complex (newReal, newImage); return result;} Complex mul (Complex) {// multiply the plural value by double real2 =. getReal (); double image2 =. getImage (); double newReal = real * real2-image * image2; double newImage = image * real2 + real * image2; Complex result = new Complex (newReal, newImage ); return result;} Complex div (Complex a) {// The Plural division is double real2 =. getReal (); double image2 =. getImage (); double newReal = (real * real2 + image * image2)/(real2 * real2 + image2 * image2); double newImage = (image * real2-real * image2) /(real2 * real2 + image2 * image2); Complex result = new Complex (newReal, newImage); return result;} public void print () {// output if (image> 0) {System. out. println (real + "+" + image + "I");} else if (image <0) {System. out. println (real + "" + image + "I");} else {System. out. println (real) ;}}</span>

 

<Span style = "font-family: SimSun; font-size: 14px"> package Four;/*** @ author Kun Sun * @ Date: 2013.10.15 */public class MainClass {// used to test the plural class/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub System. out. println ("Please enter the real and virtual parts of the first plural:"); Complex data1 = new Complex (); System. out. println ("Ask the user to enter the real and virtual parts of the second plural:"); Complex data2 = new Complex (); // The following are the addition, subtraction, multiplication, division, Complex result_add = data1.add (data2 ); complex result_sub = data1.sub (data2); Complex result_mul = data1.mul (data2); Complex result_div = data1.div (data2); merge (); result_sub.print (); result_mul.print (); result_div.print () ;}</span>

 

4. Test Run result
 

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.