CSE 214,Java,复数帮助

发布于 2024-08-07 09:18:21 字数 2379 浏览 1 评论 0原文

这是一份 CSE 作业,我希望有一些友好的小伙子和女孩们可以快速浏览一下,看看它是否适合交上来,谢谢大家。

以下是我编写的说明和代码,

-Kyle

编写一个 ComplexNumber 类,其中包含:

(1) 一个不带任何参数的构造函数(在本例中,复数的默认值应为 0 + 0i。)

(2 ) 另一个构造函数,它采用 int 类型的实部和虚部作为参数

(3) 一个 add 方法,它采用另一个复数 c2 作为参数,并将 c2 添加到 当前的复数,即 this,并返回结果复数。 (4)subtract 方法,以另一个复数 c2 作为参数,从当前复数 this 中减去 c2,并返回结果复数。

(5) 乘法方法,以另一个复数 c2 作为参数,将 c2 与当前复数 this 相乘,并返回结果复数。

(6) 除法方法,它以另一个复数 c2 作为参数,将当前复数 this 除以 c2,并返回结果复数。

(7) toString1 方法,它将以 a + bi 的形式打印当前复数 String,其中 a 和 b 将是自然数的实部和虚部的值。

/*
 * Kyle Arthur Benzle
 * CSE 214
 * 10/13/9
 * Tagore 
 * 
 * This program takes two int variables and performs 
 * four mathematical operations (+, -, *, /) to them before returning the result from a     toString1 method. 
 */


//our first class Complex#
public class ComplexNumber {

// two int variables real and imagine
int real;
int imagine;

// Constructor, no parameters, setting our complex number equal to o + oi
ComplexNumber() {
    real = 0;
    imagine = 0;        }

// Constructor taking two int variables as parameters.
ComplexNumber(int rePart, int imaginePart) {
    real = rePart;
    imagine = imaginePart;      }

// This is the add method, taking object c2 as parameter, and adding it to .this to return 
public ComplexNumber add(ComplexNumber c2) {
    return new ComplexNumber(this.real + c2.real, this.imagine + c2.imagine);      }

// Now the subtract method, followed by the methods to multiply and divide according to hand-out rules.
public ComplexNumber substract(ComplexNumber c2) {
    return new ComplexNumber(this.real - c2.real, this.imagine - c2.imagine);     }

public ComplexNumber multiply(ComplexNumber c2) {
    ComplexNumber c3 = new ComplexNumber();
    c3.real = this.real * c2.real - this.imagine * c2.imagine;
    c3.imagine = this.real * c2.imagine + this.imagine * c2.real;
    return c3;      }

public ComplexNumber divide(ComplexNumber c2) {
    ComplexNumber c3 = new ComplexNumber();
    c3.real = this.real / c2.real - this.imagine / c2.imagine;
    c3.imagine = this.real / c2.imagine + this.imagine / c2.real;
    return c3;      }

// toString1 method to return "a+bi" as a String.
public String toString1() {
    return this.real + " + " + this.imagine + "i";
}
/* And we are all done, except for this last little } right here. */        }

This is a CSE homework, I was hoping there might be some friendly guys and gals out there that might take a quick look and see if it looks good to turn in, thanks Y'all.

Here are the instructions and the code I wrote,

-Kyle

write a ComplexNumber class with:

(1) a constructor which does not take any parameters (where the default value of a complex number should be 0 + 0i in this case.)

(2) another constructor that takes real and imaginary parts of type int as parameters

(3) an add method which takes another complex number, c2, as a parameter and adds c2 to
the current complex number, which is this, and returns the resulting complex number.
(4) a subtract method which takes another complex number, c2, as a parameter and subtracts c2 from the current complex number this, and returns the resulting complex number.

(5) a multiply method which takes another complex number, c2, as a parameter and multiplies c2 with the current complex number this, and returns the resulting complex number.

(6) a divide method which takes another complex number, c2, as a parameter and divides the current complex number this by c2, and returns the resulting complex number.

(7) a toString1 method that will print a String that is the current complex number in the form of a + bi, where a and b will be the values of the real and imaginary parts of the natural number.

/*
 * Kyle Arthur Benzle
 * CSE 214
 * 10/13/9
 * Tagore 
 * 
 * This program takes two int variables and performs 
 * four mathematical operations (+, -, *, /) to them before returning the result from a     toString1 method. 
 */


//our first class Complex#
public class ComplexNumber {

// two int variables real and imagine
int real;
int imagine;

// Constructor, no parameters, setting our complex number equal to o + oi
ComplexNumber() {
    real = 0;
    imagine = 0;        }

// Constructor taking two int variables as parameters.
ComplexNumber(int rePart, int imaginePart) {
    real = rePart;
    imagine = imaginePart;      }

// This is the add method, taking object c2 as parameter, and adding it to .this to return 
public ComplexNumber add(ComplexNumber c2) {
    return new ComplexNumber(this.real + c2.real, this.imagine + c2.imagine);      }

// Now the subtract method, followed by the methods to multiply and divide according to hand-out rules.
public ComplexNumber substract(ComplexNumber c2) {
    return new ComplexNumber(this.real - c2.real, this.imagine - c2.imagine);     }

public ComplexNumber multiply(ComplexNumber c2) {
    ComplexNumber c3 = new ComplexNumber();
    c3.real = this.real * c2.real - this.imagine * c2.imagine;
    c3.imagine = this.real * c2.imagine + this.imagine * c2.real;
    return c3;      }

public ComplexNumber divide(ComplexNumber c2) {
    ComplexNumber c3 = new ComplexNumber();
    c3.real = this.real / c2.real - this.imagine / c2.imagine;
    c3.imagine = this.real / c2.imagine + this.imagine / c2.real;
    return c3;      }

// toString1 method to return "a+bi" as a String.
public String toString1() {
    return this.real + " + " + this.imagine + "i";
}
/* And we are all done, except for this last little } right here. */        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩动你心 2024-08-14 09:18:21

没有人会指出他的部门已经关闭了吗?

a + bi / x + yi 不仅仅是 a/x + b/y + "i"

正确的形式为

(a*x + b*x) - (a*y - b*y*(-1)) / (X^2 + y^2)

如果我遗漏了什么,请纠正我。

Is nobody going to point out that his division is off?

a + bi / x + yi is not simply a/x + b/y + "i" .

The correct form would be

(a*x + b*x) - (a*y - b*y*(-1)) / (X^2 + y^2).

please correct me if i am missing something.

贱贱哒 2024-08-14 09:18:21

凯尔,

很高兴您有兴趣验证您的代码!您听说过测试驱动开发吗?这是在编写代码之前编写单元测试的过程。

一般来说,测试有助于验证您的代码是否执行了它应该执行的操作...因此,即使您之后进行测试,您也知道您的代码执行了它应该执行的操作(在大多数情况下)。

我的建议是:编写一些 j 单元测试(非常快速且易于实现)并实际测试解决方案!一旦您实现了这些测试,那就太棒了,因为如果代码发生更改,您可以重新运行这些测试......

无论您相信与否,它都会使您成为一名出色的开发人员。尽早开始做这件事!业内许多人不测试代码,这导致了很多问题。

Kyle,

Glad you're interested in verifying your code! Have you heard about test driven development? It's the process of writing unit tests before you write your code.

In general, testing helps verify that your code does what it's supposed to do...so even if you test after, you know your code does what it's supposed to do (for the most part).

What I suggest: write some j-unit tests (very quick and easy to implement) and actually test out solutions! Once you've implemented these tests, it's awesome because you can re-run these tests if code is ever changed...

and believe it or not, it makes you an amazing developer. Start doing this early! Many people in the industry don't test code and it has led to a lottttt of issues.

独行侠 2024-08-14 09:18:21

您的实例变量(真实的和想象的)可能应该是私有的。

对于乘法和除法,您可以直接构造 c3,而不是使用零参数构造函数,就像减法和加法一样。如果您认为这看起来很难看,请使用临时变量来保存实部和虚部。

传入,在除法情况下会发生什么

new Complex() 

如果您作为参数 ?至少,如果确实发生的事情是您想要的,请记录下来。

Your instance variables, real and imagine should probably be private.

For mulitply and divide you might construct your c3s directly, rather than use the zero-args constructor, just as you did for subtract and add. If you think that looks ugly, use temporary variables to hold the real and imgainary parts.

What will happen in the divide case if you pass in

new Complex() 

as the argument? At the very least, if what does happen is what you intend, document that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文