Complex Number help

OK so I have a programming assignment and I need to create a class that represents Complex Numbers (good so far), add them to a list(good so far), and then output whether each individual number is real (a+0x), imaginary (0+bx), or complex (a+bx). A Complex object is made by initializing the real portion and imaginary portion

    Complex(int _real, int _imag) { real = _real; imag = _imag; };
    Complex(int _real) {real = _real; imag = 0;};
    Complex() { real, imag = 0;}

The problem is my professor has silly stipulations such as all of the data fields must be private and there can be no get methods to access any portion of an indivdual Complex number from outside the class.

Instead the only public methods I have to work with are plus, subtract, and multiply methods that take another Complex number and return a sum, difference and product of the two respectively and a conjugate method that returns an individual Complex’s conjugate.
With only those four public methods at my disposal, what formula can I use to compute whether an individual number is real, imaginary or complex?

This is less a coding issue and more of a logic issue I know.

10

Let c = a + bi and q be the conjugate of c.

Then c + q = 2a and c - q = 2bi. Now you’ve isolated twice the real and imaginary parts.

If your professor allows you to have an equals method, you can compare these to zero (i.e. Complex(0, 0)), and you have your answers.

If you literally can use only the four methods you mentioned (addition, subtraction, multiplication, and conjugation), determining your answer is provably impossible.

1

Note: This is a hack and not how “real” (ha-ha) code should be written

As Bogie said to Bergman, “We’ll always have toString()”.

Given the overly-restrictive parameters given by OP, the only solution I can think of is to put the information into the toString() method. With no serious thought about good code or testing, something like:

public String toString() {
   StringBuilder sb = new StringBuilder();
   sb.append(this.real);
   sb.append( (this.imag >= 0.0) ? " +" : " ");
   sb.append(this.imag);
   if (this.real == 0.0) {
      if (this.imag != 0.0) 
         sb.append("   imaginary");
   }
   else {
      sb.append( (this.imag == 0.0) ? "   real" : "   complex");
   }

   // TODO  note : not sure what 0.0, 0.0 counts as...

   return sb.toString();
}

This is more of a “gee I’m clever Google Interview Answer”. In real life you’d ask your professor (or your stakeholder) what the heck they actually want.

p.s. Note that my direct comparisons to 0.0 are dubious, you probably want to test if the absolute value is less than a small epsilon.

6

<edit>I think you have misunderstood your professor’s intent. A complex number class without any way to access internal values would be useless. I would expect getRealPart() and getImaginaryPart() would be implemented, and getMagnitude() and getAngle() as well.
As to the down-voter, what exactly is your concern? </edit>

Your professor is probably steering you towards making your Complex class immutable. Take a look at the BigDecimal class – it uses this pattern

Don’t forget that in a member method like with a signature like Complex add(Complex x) you can access the fields of x directly – you don’t need getters, as the access rules of Java allow access.

I’m not sure why you need to test for real, imaginary or complex. If you need an isReal() member method, just test the imaginary part for zero.

Your class will have an equals() method, as it is a method of all Java objects. You will need to override this, and you should also override hashCode() while in the area.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Complex Number help

OK so I have a programming assignment and I need to create a class that represents Complex Numbers (good so far), add them to a list(good so far), and then output whether each individual number is real (a+0x), imaginary (0+bx), or complex (a+bx). A Complex object is made by initializing the real portion and imaginary portion

    Complex(int _real, int _imag) { real = _real; imag = _imag; };
    Complex(int _real) {real = _real; imag = 0;};
    Complex() { real, imag = 0;}

The problem is my professor has silly stipulations such as all of the data fields must be private and there can be no get methods to access any portion of an indivdual Complex number from outside the class.

Instead the only public methods I have to work with are plus, subtract, and multiply methods that take another Complex number and return a sum, difference and product of the two respectively and a conjugate method that returns an individual Complex’s conjugate.
With only those four public methods at my disposal, what formula can I use to compute whether an individual number is real, imaginary or complex?

This is less a coding issue and more of a logic issue I know.

10

Let c = a + bi and q be the conjugate of c.

Then c + q = 2a and c - q = 2bi. Now you’ve isolated twice the real and imaginary parts.

If your professor allows you to have an equals method, you can compare these to zero (i.e. Complex(0, 0)), and you have your answers.

If you literally can use only the four methods you mentioned (addition, subtraction, multiplication, and conjugation), determining your answer is provably impossible.

1

Note: This is a hack and not how “real” (ha-ha) code should be written

As Bogie said to Bergman, “We’ll always have toString()”.

Given the overly-restrictive parameters given by OP, the only solution I can think of is to put the information into the toString() method. With no serious thought about good code or testing, something like:

public String toString() {
   StringBuilder sb = new StringBuilder();
   sb.append(this.real);
   sb.append( (this.imag >= 0.0) ? " +" : " ");
   sb.append(this.imag);
   if (this.real == 0.0) {
      if (this.imag != 0.0) 
         sb.append("   imaginary");
   }
   else {
      sb.append( (this.imag == 0.0) ? "   real" : "   complex");
   }

   // TODO  note : not sure what 0.0, 0.0 counts as...

   return sb.toString();
}

This is more of a “gee I’m clever Google Interview Answer”. In real life you’d ask your professor (or your stakeholder) what the heck they actually want.

p.s. Note that my direct comparisons to 0.0 are dubious, you probably want to test if the absolute value is less than a small epsilon.

6

<edit>I think you have misunderstood your professor’s intent. A complex number class without any way to access internal values would be useless. I would expect getRealPart() and getImaginaryPart() would be implemented, and getMagnitude() and getAngle() as well.
As to the down-voter, what exactly is your concern? </edit>

Your professor is probably steering you towards making your Complex class immutable. Take a look at the BigDecimal class – it uses this pattern

Don’t forget that in a member method like with a signature like Complex add(Complex x) you can access the fields of x directly – you don’t need getters, as the access rules of Java allow access.

I’m not sure why you need to test for real, imaginary or complex. If you need an isReal() member method, just test the imaginary part for zero.

Your class will have an equals() method, as it is a method of all Java objects. You will need to override this, and you should also override hashCode() while in the area.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Complex Number help

OK so I have a programming assignment and I need to create a class that represents Complex Numbers (good so far), add them to a list(good so far), and then output whether each individual number is real (a+0x), imaginary (0+bx), or complex (a+bx). A Complex object is made by initializing the real portion and imaginary portion

    Complex(int _real, int _imag) { real = _real; imag = _imag; };
    Complex(int _real) {real = _real; imag = 0;};
    Complex() { real, imag = 0;}

The problem is my professor has silly stipulations such as all of the data fields must be private and there can be no get methods to access any portion of an indivdual Complex number from outside the class.

Instead the only public methods I have to work with are plus, subtract, and multiply methods that take another Complex number and return a sum, difference and product of the two respectively and a conjugate method that returns an individual Complex’s conjugate.
With only those four public methods at my disposal, what formula can I use to compute whether an individual number is real, imaginary or complex?

This is less a coding issue and more of a logic issue I know.

10

Let c = a + bi and q be the conjugate of c.

Then c + q = 2a and c - q = 2bi. Now you’ve isolated twice the real and imaginary parts.

If your professor allows you to have an equals method, you can compare these to zero (i.e. Complex(0, 0)), and you have your answers.

If you literally can use only the four methods you mentioned (addition, subtraction, multiplication, and conjugation), determining your answer is provably impossible.

1

Note: This is a hack and not how “real” (ha-ha) code should be written

As Bogie said to Bergman, “We’ll always have toString()”.

Given the overly-restrictive parameters given by OP, the only solution I can think of is to put the information into the toString() method. With no serious thought about good code or testing, something like:

public String toString() {
   StringBuilder sb = new StringBuilder();
   sb.append(this.real);
   sb.append( (this.imag >= 0.0) ? " +" : " ");
   sb.append(this.imag);
   if (this.real == 0.0) {
      if (this.imag != 0.0) 
         sb.append("   imaginary");
   }
   else {
      sb.append( (this.imag == 0.0) ? "   real" : "   complex");
   }

   // TODO  note : not sure what 0.0, 0.0 counts as...

   return sb.toString();
}

This is more of a “gee I’m clever Google Interview Answer”. In real life you’d ask your professor (or your stakeholder) what the heck they actually want.

p.s. Note that my direct comparisons to 0.0 are dubious, you probably want to test if the absolute value is less than a small epsilon.

6

<edit>I think you have misunderstood your professor’s intent. A complex number class without any way to access internal values would be useless. I would expect getRealPart() and getImaginaryPart() would be implemented, and getMagnitude() and getAngle() as well.
As to the down-voter, what exactly is your concern? </edit>

Your professor is probably steering you towards making your Complex class immutable. Take a look at the BigDecimal class – it uses this pattern

Don’t forget that in a member method like with a signature like Complex add(Complex x) you can access the fields of x directly – you don’t need getters, as the access rules of Java allow access.

I’m not sure why you need to test for real, imaginary or complex. If you need an isReal() member method, just test the imaginary part for zero.

Your class will have an equals() method, as it is a method of all Java objects. You will need to override this, and you should also override hashCode() while in the area.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật