Sunday, June 16, 2019
Lab 11 Exercise Questions at Java Programming
class Complex {
private double real;
private double imaginary;
private static double LIMIT = 10.0;
public Complex() {
this.real = 0;
this.imaginary = 0;
}
public Complex(double real) {
this.real = real;
this.imaginary = 0;
}
public Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public double getReal() {
return this.real;
}
public double getImaginary() {
return this.imaginary;
}
public void setReal(double real) {
this.real = real;
}
public void setImaginary(double imaginary) {
this.imaginary = imaginary;
}
public String toString() {
return real + " " + imaginary + "i";
}
public boolean equals(Complex c)
{
return ((this.real == c.real) && (this.imaginary == c.imaginary));
}
public Complex add(Complex c)
{
return new Complex(this.real + c.real, this.imaginary + c.imaginary);
}
public Complex add(double value)
{
return new Complex(this.real + value, this.imaginary);
}
public boolean isBig() {
return (Math.sqrt(this.real*this.real+this.imaginary*this.imaginary) > LIMIT);
}
public static Complex add(Complex c1, Complex c2)
{
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}
}
//Complex Tester class
//Author: Kevin Soule
class ComplexTester {
public static void main(String args[] ) {
//create complex numbers testing all three constructors
Complex one = new Complex();
Complex two = new Complex(5);
Complex three = new Complex(2.1,3.5);
// test toString
System.out.println("First Complex number : " + one.toString());
System.out.println("Second Complex number : " + two.toString());
System.out.println("Third Complex number : " + three);
//test accessors
System.out.println("Real for third number : "+ three.getReal());
System.out.println("Imaginary for third number : " + three.getImaginary());
//test mutators
System.out.println("Using mutators, change first number to 1.7 + 3.4i");
one.setReal(1.7);
one.setImaginary(3.4);
System.out.println("First Complex number : " + one);
System.out.println();
//test two instance add methods
Complex four = one.add(three);
System.out.println(one + " plus " + three + " = " + four);
Complex five = two.add(3);
System.out.println("Adding 3.0 to Second complex number = " + five);
System.out.println();
//test equals method
System.out.println("Third Complex number : " + three);
Complex six = new Complex(2.1,3.5);
System.out.println("Sixth Complex number : " + six);
System.out.println("Third equals sixth : " + three.equals(six));
System.out.println("Third equals first : " + three.equals(one));
System.out.println();
//test static add method
System.out.println("Fourth Complex number : " + four);
System.out.println("Fifth Complex number : " + five);
Complex seven = Complex.add(four,five);
System.out.println("Fourth plus Fifth = " + seven);
//testing toBig method
System.out.println();
System.out.println("Seventh Complex number : " + seven);
System.out.println("Is Fourth to Big : " + four.isBig());
System.out.println("Is Seventh to Big : " + seven.isBig());
}
}
http://erdoganakbiyik.blogspot.com/
https://www.youtube.com/channel/UCDsUnmBfVdEPkcC8FlzPKcg
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment