首页 > 编程知识 正文

虚数在java中如何表示,虚数怎么定义

时间:2023-12-26 02:46:18 阅读:322469 作者:MNDY

本文目录一览:

在java语言中多少表示y,多少表示n

大写的Y是89,大写的N是78.

小写的y是121,小写的n是110.

记住小写的a是97,大写的A是65就可以,后面的字母依次类推。

用Java语言定义复数类Complex,必须满足如下要求:

public class Complex{

public int RealPart;

public int ImaginPart;

public Complex(){

this.RealPart = 0;

this.ImaginPart = 0;

}

public Complex(int r, int i){

this.RealPart = r;

this.ImaginPart = i

}

public Complex complexAdd(Complex a){

this.RealPart += a.RealPart;

this.ImaginPart += a.ImaginPart;

//这里返回什么?复数值是指哪个?还是复数对象?没有说清楚。我这里返回复数对象。

return this;

}

public String ToString(){

return ""+this.RealPart + this.ImaginPart;//return "" + 1 + 2 = "12";不知道这里要求是不是这样。有些话写的我没法理解。你的a + bi是个啥?如果是返回实数和虚数的和就给 1 + 2的部分加上括号。

}

}

用JAVA定义个复数类

public class Complex {

private double x;//实部

private double y;//虚部

public Complex(){}

/**构造函数

* @param x 实数部分

* @param y 虚数部分

*/

public Complex(double x,double y){

super();

this.x = x;

this.y = y;

}

/**求模

* @return 该复数的模

*/

public double mod(){

return x * x + y * y;

}

/**复数间加法

* @param complex 加数

* @return 计算结果

*/

public Complex add(Complex complex){

double x = this.x + complex.x;

double y = this.y + complex.y;

return new Complex(x,y);

}

/**复数与实数的加法

* @param a 加数

* @return 计算结果

*/

public Complex add(double a){

return this.add(new Complex(a,0));

}

/**复数间减法

* @param complex 减数

* @return 计算结果

*/

public Complex subtract(Complex complex){

double x = this.x - complex.x;

double y = this.y - complex.y;

return new Complex(x,y);

}

/**复数与实数的减法

* @param a 减数

* @return 计算结果

*/

public Complex subtract(double a){

return subtract(new Complex(a,0));

}

/**复数间乘法

* @param complex 乘数

* @return 计算结果

*/

public Complex multiply(Complex complex){

double x = this.x * complex.x - this.y * complex.y;

double y = this.y * complex.x + this.x * complex.y;

return new Complex(x,y);

}

/**复数间除法

* @param complex 除数

* @return 计算结果

*/

public Complex divide(Complex complex){

double x = (this.x * complex.x + this.y * complex.y) / (complex.mod());

double y = (this.y * complex.x - this.x * complex.y) / (complex.mod());

return new Complex(x,y);

}

public String toString(){

StringBuffer sb = new StringBuffer();

if(x != 0){

sb.append(x);

if(y 0){

sb.append("+" + y + "i");

}else if(y 0){

sb.append(y + "i");

}

}else{

if(y != 0){

sb.append(y + "i");

}

}

if(x == 0 y == 0){

return "0";

}

return sb.toString();

}

public double getX() {

return x;

}

public void setX(double x) {

this.x = x;

}

public double getY() {

return y;

}

public void setY(double y) {

this.y = y;

}

public static void main(String[] args) {

Complex a = new Complex(2,0.5);

Complex b = new Complex(0.5,2);

System.out.println("(" + a + ")+(" + b + ")=" + a.add(b));

System.out.println("(" + a + ")+" + 2 + "=" + a.add(2));

System.out.println("(" + a + ")-(" + b + ")=" + a.subtract(b));

System.out.println("(" + a + ")-" + 2 + "=" + a.subtract(2));

System.out.println("(" + a + ")*(" + b + ")=" + a.multiply(b));

System.out.println("(" + a + ")/(" + b + ")=" + a.divide(b));

}

}

使用JAVA编程实现复数类ComplexNumber

import java.applet.Applet;

import java.awt.Button;

import java.awt.Label;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class ComplexTest extends Applet implements ActionListener{

Label firstReal, firstImg;

TextField firstRealNum, firstImgNum;

Label secondReal, secondImg;

TextField secondRealNum, secondImgNum;

Button add = new Button("Add");

Button subtract = new Button("Subtract");

TextField addResult, subtractResult;

public void init(){

firstReal = new Label("First Complex Real Number: ");

firstRealNum = new TextField(7);

super.add(firstReal);

super.add(firstRealNum);

firstImg = new Label("First Complex Imaginary Number: ");

firstImgNum = new TextField(7);

super.add(firstImg);

super.add(firstImgNum);

secondReal = new Label("Second Complex Real Number: ");

secondRealNum = new TextField(7);

super.add(secondReal);

super.add(secondRealNum);

secondImg = new Label("Second Complex Imaginary Number: ");

secondImgNum = new TextField(7);

super.add(secondImg);

super.add(secondImgNum);

super.add(add);

addResult = new TextField(7);

super.add(addResult);

super.add(subtract);

subtractResult = new TextField(7);

super.add(subtractResult);

add.addActionListener(this);

subtract.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

double firstComplxReal = Double.parseDouble(firstRealNum.getText());

double firstComplxImg = Double.parseDouble(firstImgNum.getText());

double secondComplxReal = Double.parseDouble(secondRealNum.getText());

double secondComplxImg = Double.parseDouble(secondImgNum.getText());

ComplexNumber complxNum1 = new ComplexNumber(firstComplxReal, firstComplxImg);

ComplexNumber complxNum2 = new ComplexNumber(secondComplxReal, secondComplxImg);

addResult.setText(complxNum1.add(complxNum2).toString());

subtractResult.setText(complxNum1.subtract(complxNum2).toString());

}

}

class ComplexNumber{

private double real;

private double imaginary;

public ComplexNumber(double realNum, double imaginaryNum){

this.real = realNum;

this.imaginary = imaginaryNum;

}

// (a+bi) + (c+di) = (a+b) + (c+d)i

public ComplexNumber add(ComplexNumber complexNum2){

double newRealPart = this.real + complexNum2.getReal();

double newImgPart = this.imaginary + complexNum2.getImaginary();

return new ComplexNumber(newRealPart, newImgPart);

}

//(a+bi) - (c+di) = (a-b) - (c-d)i

public ComplexNumber subtract(ComplexNumber complexNum2){

double newRealPart = this.real - complexNum2.getReal();

double newImgPart = this.imaginary - complexNum2.getImaginary();

return new ComplexNumber(newRealPart, newImgPart);

}

public double getImaginary() {

return imaginary;

}

public void setImaginary(double imaginary) {

this.imaginary = imaginary;

}

public double getReal() {

return real;

}

public void setReal(double real) {

this.real = real;

}

public String toString(){

return real + "+" + imaginary + "i";

}

}

在java中复数的结构包括实部和虚部?什么是实部什么是虚部?

复数是形如

a+bi

的数,

其中

a,

b

是实数,a称为实部,b称为虚部,i是虚数单位,

i^2

=

-1.

实数可以用一条直线(数轴)上的点表示,

类似地,

复数可以用一个平面(称为复平面)上的点表示.

在复平面上,a+bi

用点Z(a,b)

表示。Z与原点的距离r称为Z的模|Z|=√(a^2+b^2)

复数a+bi,b=0为实数,b不等于0为虚数,

a=0且b不等于0时为纯虚数。

复数的三角形式是

Z=r(cosx+isinx)

中x,r是实数,rcosx称为实部,rsinx称为虚部,i是虚数单位。Z与原点的距离r称为Z的模,x称为辐角。

java中怎样表示一个无穷大?无穷小

Java中提供了三个特殊的浮点数值:正无穷大、负无穷大、非数,用于表示溢出和出错。

正无穷大:用一个正数除以0将得到一个正无穷大,通过Double或Float的POSITIVE_INFINITY表示。

负无穷大:用一个负数除以0将得到一个负无穷大,通过Double或Float的NEGATIVE_INFINITY表示。

非数:0.0除以0.0或对一个负数开放将得到一个非数,通过Double或Float的NaN表示。

所有的正无穷大的数值都是相等的,所有的负无穷大的数值都是相等;而NaN不与任何数值相等,甚至和NaN都不等。

public class javaLesson5

{

public static void main(String[] args)

{

   float af = 5.2325556f;

   //下面将看到af的值已经发生改变,显示结果为5.2325554.

   System.out.println(af);

   double a = 0.0;

   double c = Double.NEGATIVE_INFINITY;

   float d = Float.NEGATIVE_INFINITY;

   //将看到float和double的负无穷大是相等的。显示结果为:true。

   System.out.println(c == d);

   //0.0除以0.0将出现非数。显示结果为:NaN。

   System.out.println(a / a);

   //两个非数之间是不相等的。显示结果为:false。

   System.out.println(a == Float.NaN);

   //所有正无穷大都是相等的。显示结果为:true。

   System.out.println(6.0 / 0 == 555.0/0);

   //负数除以0.0将得到负无穷大。显示结果为:-Infinity

   System.out.println(-8 / a);

   //下面代码将抛出除以0的异常。

   //System.out.pintln(0 / 0);

}

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。