gpt4 book ai didi

java - 家庭作业帮助 Pt2(数学复杂课)

转载 作者:太空宇宙 更新时间:2023-11-04 13:08:02 24 4
gpt4 key购买 nike

不确定我这样做是否正确,但这是我在这里所做的程序的延续... Homework Help PT1

我在这个家庭作业上遇到了很多困难......

**(Math: The Complex class) A complex number is a number in the form a + bi,
where a and b are real numbers and i is 2-1. The numbers a and b are known
as the real part and imaginary part of the complex number, respectively. You can
perform addition, subtraction, multiplication, and division for complex numbers
using the following formulas:
a + bi + c + di = (a + c) + (b + d)i
a + bi - (c + di) = (a - c) + (b - d)i
(a + bi)*(c + di) = (ac - bd) + (bc + ad)i
(a + bi)/(c + di) = (ac + bd)/(c2 + d2) + (bc - ad)i/(c2 + d2)
You can also obtain the absolute value for a complex number using the following
formula:
a + bi = 2a2 + b2
Design a class named Complex for representing complex numbers and the
methods add, subtract, multiply, divide, and abs for performing complexnumber
operations, and override toString method for returning a string representation
for a complex number. The toString method returns (a + bi) as a
string. If b is 0, it simply returns a. Your Complex class should also implement the
Cloneable interface.
Provide three constructors Complex(a, b), Complex(a), and Complex().
Complex() creates a Complex object for number 0 and Complex(a) creates
a Complex object with 0 for b. Also provide the getRealPart() and
getImaginaryPart() methods for returning the real and imaginary part of the
complex number, respectively.
Write a test program that prompts the user to enter two complex numbers and
displays the result of their addition, subtraction, multiplication, division, and absolute
value.**

这是我到目前为止所拥有的。两个类...

// ComplexTest.java

import java.util.Scanner;

public class ComplexTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter the first complex number: ");
double realPart = input.nextDouble();

System.out.println("Enter the second complex number: ");
double imaginaryPart = input.nextDouble();

Complex cn1 = new Complex(realPart, imaginaryPart);
Complex cn2 = new Complex(realPart);
Complex cn3 = new Complex();

if (realPart == 0) {
System.out.println(cn3.toString());
}
if (imaginaryPart == 0) {
System.out.println(cn2.toString());
}
if(realPart != 0 && imaginaryPart != 0) {
System.out.println(cn1.toString());
}
}
}

// Complex.java

import java.util.Scanner;

public class Complex {

// cloneable interface
public interface Cloneable { }

// Instance Real + Getters and Setters (Accessors and Mutators)
private double realPart;

public double getReal() {
return realPart;
}

public void setReal(double real) {
this.realPart = real;
}

// Instance Real + Getters and Setters (Accessors and Mutators)

private double imaginaryPart;

public double getImaginary() {
return imaginaryPart;
}

public void setImaginary(double imaginary) {
this.imaginaryPart = imaginary;
}

// Constructor Method CN1
public Complex(double a, double b) {
realPart = a;
imaginaryPart = b;
}

// Constructor Method CN2
public Complex(double a) {
realPart = a;
imaginaryPart = 0;
}

// Constructor Method CN3
public Complex() { }

// Add Complex Numbers
public Complex add(Complex comp1, Complex comp2) {
double real1 = comp1.getReal();
double real2 = comp2.getReal();
double imaginary1 = comp1.getImaginary();
double imaginary2 = comp2.getImaginary();

return new Complex(real1 + real2, imaginary1 + imaginary2);
}

// Subtract Complex Numbers
public Complex subtract(Complex comp1, Complex comp2) {
double real1 = comp1.getReal();
double real2 = comp2.getReal();
double imaginary1 = comp1.getReal();
double imaginary2 = comp2.getReal();

return new Complex(real1 - real2, imaginary1 - imaginary2);
}

// Multiply Complex Numbers
public Complex multiply(Complex comp1, Complex comp2) {
double real1 = comp1.getReal();
double real2 = comp2.getReal();
double imaginary1 = comp1.getReal();
double imaginary2 = comp2.getReal();

return new Complex(real1 * real2, imaginary1 * imaginary2);
}

// Divide Complex Numbers
public Complex divide(Complex comp1, Complex comp2) {
double real1 = comp1.getReal();
double real2 = comp2.getReal();
double imaginary1 = comp1.getReal();
double imaginary2 = comp2.getReal();

return new Complex(real1 / real2, imaginary1 / imaginary2);
}

// toString to Change Display
public String toString() {
String result;
result = realPart + " + " + imaginaryPart + "i";
return result;
}
}

这是在 Jan 的帮助下我更新的代码。我创建了另外 3 种方法(减法、乘法和除法)。我不应该在每个方法中使用 comp1 和 comp2 而是将它们彼此分开命名吗?目标是在最后同时打印每个方法的结果。这些具有相同名称的会造成困惑吗?

我还想知道什么时候应该实现可克隆接口(interface)。

最后,根据文本,复数实际上看起来像是用空格分隔的两个数字。 (即 3.5 5.0 而不仅仅是 3.5)。如果我为两个复数的后半部分添加两个扫描仪输入,我将不得不更改我的代码。我是否必须创建新的 getter 和 setter 才能接收此号码?比如imaginaryPart2和realPart2?

再次感谢您的帮助。

最佳答案

一些值得深思的主题:

变量范围

传递到方法中的参数仅在整个方法中可见。因此,为每个和所有方法命名两个操作数 comp1comp2 是完全可以的。

但是:

面向对象

您的方法应该只有一个参数。假设您有一个名为 x 的 Complex 实例。您想要添加另一个名为 y 的实例。然后,根据您的代码,x.add(x,y)y.add(x,y) 甚至 z.add(x, y) 的任何操作都会产生相同的结果。

所以:删除你的参数之一。您可能想要添加空检查。

public Complex add(Complex toAdd) {
return new Complex(this.realPart + toAdd.realPart,
this.imaginaryPart + toAdd.imagineryPart);
}

现在你可以写了

Complex z = x.add(y); 

getter 和 setter

由于您的加/减/除/乘操作都返回一个复数,您可能希望使 Contex 不可变 - 即:不提供 setter。可以通过构造函数创建复数。您可以通过调用现有复数的计算来获取新的复数。但您无法更改号码。

所以我的建议:删除 setter 。

复数输入

您可能不想读取 double,而是考虑读取 String 并将该字符串与正则表达式匹配。您可以将其用作 main 中的实用方法,甚至用作 Complex 的构造函数,从而允许使用 String 作为输入。

考虑这个方法来匹配字符串:

    Pattern complexFinder = Pattern.compile("(-?\\d+(\\.\\d*)?)?\\s*([-+]\\s*\\d+(\\.\\d*)?i)?");
Matcher m = complexFinder.matcher(complexString);
if (m.find()) {
double realPart = 0;
double imaginaryPart = 0;
if (m.group(1) != null) {
realPart = Double.parseDouble(m.group(1).replaceAll("\\s", ""));
}
if (m.group(3) != null) {
imaginaryPart = Double.parseDouble(m.group(3).replaceAll("\\s", "").replace("i", ""));
}
Complex c = new Complex(realPart, imaginaryPart);
}

可克隆

Cloneable 是您添加到类声明中的接口(interface):

public class Complex implements Cloneable {

此外,您应该实现一个 clone() 方法:

public Object clone() {
return super.clone();
}

toString()

您的作业要求在字符串输出中省略 0 虚部。所以你可能想再次检查一下。这应该是一个简单的 if()

关于java - 家庭作业帮助 Pt2(数学复杂课),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34243287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com