Question
from my java and algo class. A complex number stores a real part and an imaginary part. Provide an implementation of a BigComplex class, in
from my java and algo class.
A complex number stores a real part and an imaginary part. Provide an implementation of a BigComplex class, in which the data representation is two BigDecimals representing the real and imaginary parts.
Follow the example of the BigRational class and make sure your implementation includes the following:
4 constructors - Assume the string constructor has the format like "a + bi" so it might be "4.6 + 8.35i" or just "a" like "5.4"
I would like both the Add and Subtract methods to work. To do math on rational numbers you add the normal parts together and the imaginary parts together
I would like the equals and toString methods. Be sure to accept an Object as the parameter, then downcast it if appropriate.
I have also included a separate toString method that takes a BigComplex object as a parameter in the hope that it makes it easier to think about.
the code
import java.math.BigDecimal;
import java.util.Random;
public class Assignment_03_31_BigComplex {
public Assignment_03_31_BigComplex() {
//TODO this constructor should represent the number 0+0i
//Note - Be sure to use BigDecimals's!
}
public Assignment_03_31_BigComplex(BigDecimal real) {
//TODO this constructor should represent the number real+0i
}
public Assignment_03_31_BigComplex(BigDecimal real, BigDecimal imaginary) {
//TODO this constructor should represent the number real+imaginary i
}
public Assignment_03_31_BigComplex(String s) {
//TODO this constructor should represent the number shown in the String - Assume the number will be something like "4.6" or "4.6 + 8.35i"
}
public static Assignment_03_31_BigComplex add(Assignment_03_31_BigComplex a, Assignment_03_31_BigComplex b) {
//TODO - this method should create a third Complex number that is the result of adding a and b
//Note: We are doing this slightly different from the book example, in that both a and b are provided as parameters
return null;
}
public static Assignment_03_31_BigComplex subtract(Assignment_03_31_BigComplex a, Assignment_03_31_BigComplex b) {
//TODO - this method should create a third Complex number that is the result of subtracting b from a. I.e. a-b
//Note: We are doing this slightly different from the book example, in that both a and b are provided as parameters
return null;
}
public boolean equals(Assignment_03_31_BigComplex a) {
//TODO - I am adding this equals method so that you can have a slightly easier time thinking about equality before you tackle the next method
return Math.random() < 0.5;
}
@Override
public boolean equals(Object a) {
//TODO - This method should follow a similar pattern to the example in the book - where you check if a is actually a complex number
return Math.random() < 0.5;
}
@Override
public String toString() {
//TODO - this method should return the string representation of the number. My main method assumes there will be a space around both sides of the plus sign, but no space before the i
//NOTE: This method needs to work correctly so that almost all the tests work!!!!!
//NOTE: Also, don't just always put the i part on - only if i isn't 0
//NOTE: You do NOT have to convert the + to a - if the imaginary part is negative!!!
return "TODO";
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started