Question
Java project

Project Complex number class. Tuesday April 17 Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read O public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a toString) method. Include get and set methods as well. On my website is an explanation of complex numbers and examples with expected answers along with a main demo program. Study the explanation and use your Book class as a starting point for the Complex number class. Your class should have at least 2 constructors: a default constructor and an explicit constructor with two arguments of type double. Choose descriptive variable names: a, b, c, and d are not acceptable variable names. Do not do arithmetic in the argument to the constructor.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code

-----------------------------------------------------------------------------------------------------------------------------------------------

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package complex;

/**
*
* @author satsahib
*/
public class Complex {
int real,imagine;

//default constructor
public Complex() {
real = 0;
imagine = 0;
}
  
//parmetrized constructor
public Complex(int real, int imagine) {
this.real = real;
this.imagine = imagine;
}
//getter and setter
public int getReal() {
return real;
}

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

public int getImagine() {
return imagine;
}

public void setImagine(int imagine) {
this.imagine = imagine;
}
//tostring() method
@Override
public String toString() {
return "Complex{" + "real=" + real + ", imagine=" + imagine + '}';
}
public Complex add(Complex c1)
{
Complex CSum=new Complex();
CSum.real=real+c1.real;
CSum.imagine=imagine+c1.imagine;
return CSum;
}
  
public Complex subtract(Complex c1)
{
Complex CSub=new Complex();
CSub.real=real-c1.real;
CSub.imagine=imagine-c1.imagine;
return CSub;
}
  
public Complex multiply(Complex c2) {
Complex c3 = new Complex();
c3.real = real * c2.real - imagine * c2.imagine;
c3.imagine = real * c2.imagine + imagine * c2.real;
return c3;   
}

public Complex divide(Complex c2) {
Complex c3 = new Complex();
c3.real = real / c2.real -imagine / c2.imagine;
c3.imagine = real / c2.imagine + imagine / c2.real;
return c3;   
}
  
public boolean equals(Complex c2)
{
if(real==c2.real && imagine==c2.imagine)
return true;
else
return false;
}

public static void main(String[] args) {
// TODO code application logic here
Complex C1=new Complex(4,8);
Complex C2=new Complex(5,7);
Complex C3=new Complex(4,8);
Complex C4=new Complex();
//add two complex number
C4=C1.add(C2);
System.out.println(C4);
//subtract two complex number
C4=C1.subtract(C2);
System.out.println(C4);
//mutiply two complex number
C4=C1.multiply(C2);
System.out.println(C4);
//divide two complex number
C4=C1.divide(C2);
System.out.println(C4);

//compare two complex number
boolean b= C1.equals(C2);
  
if(b== true)
System.out.println("Both complex number are equal");
else
System.out.println("Both complex number are not equal");
  
//compare two complex number
b= C1.equals(C3);
  
if(b== true)
System.out.println("Both complex number are equal");
else
System.out.println("Both complex number are not equal");
  
  
}
  
}

---------------------------------------------------------------------------------------------------------------------------------------------

Output

Complex-NetBeans IDE 82 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help Search (Ctri+I) Proj

Add a comment
Know the answer?
Add Answer to:
Java project Project Complex number class. Tuesday April 17 Write a Complex number class. It will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • Create a class for working with complex numbers. Only 2 private float members are needed, the...

    Create a class for working with complex numbers. Only 2 private float members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • JAVA PROGRAMMING A complex number is a number in the form a + bi, where a...

    JAVA PROGRAMMING A complex number is a number in the form a + bi, where a and b are real numbers and i is V-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 + di a + bi - (c +...

  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

  • Complex number class:: Question Changed to just the Java portion Design a class in and Java...

    Complex number class:: Question Changed to just the Java portion Design a class in and Java that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. (the following is for the c++ and python version, not sure if its needed for this) op: Complex × Complex → Complex op: Complex × double → Complex op: double × Complex → Complex Where op is one of +, -, *, or /. In addition, you will need...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT