栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java黑皮书课后习题13.20 求解二元方程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java黑皮书课后习题13.20 求解二元方程

package Test;

import java.util.Scanner;

public class Complex implements Cloneable{
private double realPart = 0;
private double ImaginaryPart = 0;

Complex(){
	this(0,0);
}

Complex(double a){
	this(a,0);
}

Complex(double a,double b){
	this.realPart = a;
	this.ImaginaryPart = b;
}

public double getRealPart() {
	return realPart;
}

public double getImaginaryPart() {
	return ImaginaryPart;
}

public Complex add(Complex num) {
	double a = this.realPart + num.getRealPart();
	double b = this.ImaginaryPart + num.getImaginaryPart();
	return new Complex(a,b);
}

public Complex substract(Complex num) {
	double a = this.realPart - num.getRealPart();
	double b = this.ImaginaryPart - num.getImaginaryPart();
	return new Complex(a,b);
}

public Complex divide(Complex num) {
	double a = (this.realPart * num.getRealPart() + this.ImaginaryPart * num.getImaginaryPart())
			/ (long)(Math.pow(num.getRealPart(), 2) + Math.pow(num.getImaginaryPart(), 2));
	double b = (this.ImaginaryPart * num.getRealPart() - this.realPart * num.getImaginaryPart())
			/ (long)(Math.pow(num.getRealPart(), 2) + Math.pow(num.getImaginaryPart(), 2));
	return new Complex(a,b);
}

public Complex multiply(Complex num) {
	double a = this.realPart * num.getRealPart() - this.ImaginaryPart * num.getImaginaryPart();
	double b = this.ImaginaryPart * num.getRealPart() + this.realPart * num.getImaginaryPart();
	return new Complex(a,b);
}

public double abs() {
	return Math.pow(Math.pow(this.realPart, 2) + Math.pow(this.ImaginaryPart, 2), 0.5);
}

public String toString() {
	if(this.ImaginaryPart == 0)
		return this.realPart +  "";
	else
		return this.realPart + " + " + this.ImaginaryPart + "i";
}

public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	System.out.print("Enter the frist complex number(eg:1 2,3 0,1.5 -1,...): ");
	double num1 = input.nextDouble();
	double num2 = input.nextDouble();
	Complex com1 = new Complex(num1,num2);
	System.out.print("Enter the second complex number(eg:1 2,3 0,1.5 -1....): ");
	double num3 = input.nextDouble();
	double num4 = input.nextDouble();
	Complex com2 = new Complex(num3,num4);
	System.out.println("(" + com1.toString() + ") + (" + com2.toString() + ") = " + com1.add(com2).toString());
	System.out.println("(" + com1.toString() + ") - (" + com2.toString() + ") = " + com1.substract(com2).toString());
	System.out.println("(" + com1.toString() + ") x (" + com2.toString() + ") = " + com1.multiply(com2).toString());
	System.out.println("(" + com1.toString() + ") / (" + com2.toString() + ") = " + com1.divide(com2).toString());
	System.out.println("|(" + com1.toString() + ")|" + " = " + com1.abs());
}

}

package Test;

import java.util.Scanner;

public class FuncionComplex {
public static void main(String[] args) {
System.out.print(“Enter a, b, c:”);
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double exit = Math.pow(b, 2) - 4 * a * c;
if(exit > 0) {
double result1 = (-b + Math.pow(exit, 0.5)) / ( 2 * a );
double result2 = (-b - Math.pow(exit, 0.5)) / ( 2 * a );
System.out.println("The roots are " + result1 + " and " + result2);
}else if(exit == 0) {
double result = - b / 2 / a;
System.out.println("The root is " + result);
}else {
b = -1 * b;
Complex high = new Complex(-b,Math.pow(-exit, 0.5));
Complex low = new Complex(2 * a, 0);
Complex result1 = high.divide(low);
Complex high2 = new Complex(-b,-Math.pow(-exit, 0.5));
Complex low2 = new Complex(2 * a, 0);
Complex result2 = high2.divide(low2);
System.out.println("The root are " + result1.toString() + " and " + result2.toString());
}
}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981487.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号