Computers and Technology

Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, abs for performing complex-number 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. 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.
Your Complex class should also implement the Cloneable
interface.
Write a test program that prompts the user to enter two complex numbers and display the result of their addition, subtraction, multiplication, and division.
Here is the main class. It cannot be changed.
import java. util. Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System. in);
System. out. print("Enter the first complex number: ");
double a = input. nextDouble();
double b = input. nextDouble();
Complex c1 = new Complex(a, b);
System. out. print("Enter the second complex number: ");
double c = input. nextDouble();
double d = input. nextDouble();
Complex c2 = new Complex(c, d);
System. out. println("(" + c1 + ")" + " + " + "(" + c2 + ")" + " = " + c1.add(c2));
System. out. println("(" + c1 + ")" + " - " + "(" + c2 + ")" + " = " + c1.subtract(c2));
System. out. println("(" + c1 + ")" + " * " + "(" + c2 + ")" + " = " + c1.multiply(c2));
System. out. println("(" + c1 + ")" + " / " + "(" + c2 + ")" + " = " + c1.divide(c2));
System. out. println("|" + c1 + "| = " + c1.abs());
Complex c3 = (Complex)c1.clone();
System. out. println(c1 == c3);
System. out. println(c3.getRealPart());
System. out. println(c3.getImaginaryPart());
}
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 17:00, laylay120
The most efficient way to establish top best possible economize position is to measure
Answers: 1
image
Computers and Technology, 22.06.2019 14:00, michelle7511
Which database model is best used for data warehouse and data mining
Answers: 3
image
Computers and Technology, 22.06.2019 17:40, lazerlemon500
Write a modular program (no classes yet, just from what you learned last year), that allows two players to play a game of tic-tac-toe. use a two-dimensional char array with 3 rows and 3 columns as the game board. each element of the array should be initialized with an asterisk (*). the program should display the initial board configuration and then start a loop that does the following: allow player 1 to select a location on the board for an x by entering a row and column number. then redisplay the board with an x replacing the * in the chosen location. if there is no winner yet and the board is not yet full, allow player 2 to select a location on the board for an o by entering a row and column number. then redisplay the board with an o replacing the * in the chosen location. the loop should continue until a player has won or a tie has occurred, then display a message indicating who won, or reporting that a tie occurred. player 1 wins when there are three xs in a row, a column, or a diagonal on the game board. player 2 wins when there are three ox in a row, a column, or a diagonal on the game board. a tie occurs when all of the locations on the board are full, but there is no winner. input validation: only allow legal moves to be entered. the row must be 1, 2, or 3. the column must be 1, 2 3. the (row, column) position entered must currently be empty (i. e., still have an asterisk in it).
Answers: 1
image
Computers and Technology, 23.06.2019 06:40, sardarp1irc5
What are the three uses of a screw?
Answers: 2
Do you know the correct answer?
Design a class named Complex for representing complex numbers and the methods add, subtract, multipl...

Questions in other subjects:

Konu
Mathematics, 12.09.2021 02:40
Konu
Physics, 12.09.2021 02:40
Konu
English, 12.09.2021 02:50
Konu
Mathematics, 12.09.2021 02:50
Konu
Mathematics, 12.09.2021 02:50