Question
------------------ TestLab6Bronze? public class TestLab6Bronze { public static void main(String[] args) { System.out.print(A Real number r (should print 123.46): ); Real r = new Real(123.4567);
------------------
TestLab6Bronze?
public class TestLab6Bronze { public static void main(String[] args) { System.out.print("A Real number r (should print \"123.46\"): "); Real r = new Real(123.4567); System.out.println(r);
System.out.print("A Complex number c1 (should print \"3.20+6.70i\"): "); Complex c1 = new Complex(3.2,6.7); System.out.println(c1);
System.out.print("A Complex number c2 (should print \"3.20-6.70i\"): "); Complex c2 = new Complex(3.2,-6.7); System.out.println(c2);
System.out.printf("Magnitude of r (should be 123.45670 ): %8.5f%n",r.magnitude()); System.out.printf("Magnitude of c1 (should be 7.42496 ): %8.5f%n",c1.magnitude()); System.out.printf("Magnitude of c2 (should be 7.42496 ): %8.5f%n",c2.magnitude()); } }
1. Create a Real class in a file Real.java which will represent a single real number. (This is essentially what the Double class already does, but we will need our own version in this lab.) Give this class a. A private instance variable containing the value of the number (a double). b. A constructor which will set this value.- c. AString toString) method which will convert the value to a String which shows exactly 2 digits after the decimal point. [The built-in method String. format( "%4 . 2f", x) will do this for any number x? A double magnitude) method which will return the magnitude (i.e. absolute value) of the number. d. 2. Create a Complex class in a file Complex.java which will be a subclass of the Real class This will represent a complex number. [A complex number is made up of two real numbers the "real part" r and the "imaginary part" c. That's all you need to know about them here.] Give this class: a. An extra private instance variable for the imaginary part of the number (a double) b. A constructor Complex(double r, double c) which creates a complex number with a real part r and an imaginary part c. You will need to use the superclass constructor. c. AString toString) method which will use the superclass's tostring() method for the real part, then add "tci" to it, where c is the imaginary part. The imaginary part should have exactly two digits after the decimal point, the same as the real part. Be careful of the sign - some extra code will be needed.] For example, -' new Complex (2.3,4.5D.toString() should give "2.30+4.50i" (not "2.304.50i")v new Complex (2.3,-4.5).toString)- should give "2.30-4.50i" (not "2.30+-4.50i")>
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