q1q5.txt
. You must use the identifiers (variable names, class names, etc.) exactly as we specify, including capitalization. Use good programming style and observe the style guidelines that are given in the Project 1/2 Grading Guides. As you finish the project you should have a much better understanding of classes and Java applications.
Using conventional real numbers, we cannot take the square root of any negative numbers. As it turns out, taking the square root of negative numbers actually happens quite often in higher mathematics and has real physical implications. Sufficed to say, mathematicians needed some way to conceive of square roots of negative numbers. Complex numbers were born!
Complex numbers take the form a + bi
, where i
is the square root of -1. Notice that a complex number is comprised of two components, a real part a
and an imaginary part b
. To get some intuition, consider the quadratic formula:
For an equationConsider the equationax2 + bx + c = 0
, the solution forx
is given by:
x = -b ± sqrt(b^2 -4ac) 2a
x2 - 3x + 3
. Solving for x results in 3 ± sqrt(-3)
which is not a real number. We can represent this result as an imaginary number, 3 ± i*sqrt(3)
.
You can do the same basic arithmetic with imaginary numbers as you could with real numbers. In fact, much of the math is done as if you were treating i
as a variable. Operations include:
(a + bi) + (c + di) = (a + c) + (b + d)i
(a + bi) - (c + di) = (a-c) + (b-d)i
(a + bi) * (c + di) = (ac - bd) + (ad + bc)i
(a + bi) / (c + di) = (ac + bd) / (c2 + d2) + (bc - ad)i / (c2 + d2)
abs(a + bi) = sqrt(a2 + b2)
conjugate(a + bi) = a - bi
a
and b
to represent these values. Think about this before you start coding! Answer the following questions in a plain text file called q1q5.txt
.
a
and b
be instance or static? Why?ComplexNumber
:ComplexNumber cn = new ComplexNumber(5, 2);
double x= cn.a + cn.b;
a
and b
representing the real and imaginary parts of a complex number.
Next, write a constructor for a complex number. This constructor should take two parameters, the real and the imaginary components, and set the instance variables to these parameters. Once you have finished the constructor, you can test it in Dr. Java. Create a new instance of a ComplexNumber and then access the a
and b
fields. Are they what you expect?
In the Interactions PaneNow that you have your constructor, you should implement the methods listed below. Test each method after you write it! Incremental testing of your class will end up saving you time. You do not want to wait until the entire class is written to start testing--likely there'll be an overwhelmingly large number of compilation error messages. Experienced programmers know to test incrementally. You should do the same!
>ComplexNumber c = new ComplexNumber(5, 4);
>c.a
5.0
>c.b
4.0
public void add(ComplexNumber cn)
cn
to the complex number represented by this
instance.public void subtract(ComplexNumber cn)
cn
to the complex number represented by this
instance.public void multiply(ComplexNumber cn)
this
instance by the parameter cn
.public void divide(ComplexNumber cn)
this
instance by the parameter cn
.public double magnitude()
this
complex number.public ComplexNumber conjugate()
this
complex numberpublic boolean isComplex()
this
complex number is non-zeropublic String toString()
this
complex number. For example:
abs(a + bi) = sqrt(a2 + b2).)
In fact, every complex number that lies on the displayed circle has a magnitude of 1, i.e., everyone complex number that lies on a circle of radius r has magnitude r. You can imagine that for every degree around the circle (a circle has 360 degrees or 2π radians), there exists a corresponding imaginary number. More succinctly, any point on a circle is:
rcosθ + rsinθ
complexCircle
. This method should display the complex numbers corresponding to θ = 0, 45, 90, 135, 180, 225, 270, and 315 degrees where r is the magnitude of this
instance. For example, given the complex number 1 + 1i, we have the following 'complex circle':θ (in degrees) | Complex Number |
---|---|
0 | sqrt(2) |
45 | 1 + 1i |
90 | sqrt(2)i |
135 | -1 + 1i |
180 | -sqrt(2) |
225 | -1 - 1i |
270 | -sqrt(2)i |
315 | 1 - 1i |
Math.sin
and Math.cos
take radians, not degrees! To convert from degrees to radians: radians = π * degrees / 180Download Project3.java
from the Projects page. This file contains skeleton code for a Java application. The main
method provides the user with a menu in order to manipulate complex numbers. In addition, there are several other methods in the class. You will need to complete two of the methods. Read the methods and their comments and make sure you understand what they do. Be sure that you understand the provided code before you start filling in the missing parts. Notice that the instance methods you have written in class ComplexNumber
are called by the "Op methods" (see method addOp
for example). You will finish two incomplete methods: promptForComplexNumber
and the main
method.
promptForComplexNumber
method, you should prompt the user for the real and imaginary parts of a complex number and create an instance of a ComplexNumber representing the input values. Use JLiveRead
for your input (see lesson 1-5 of the ProgramLiveCD for more information).main
method, the skeleton code displays a menu for manipulating complex numbers and asks the user for their choice, which is then stored in the int
variable choice
. Write a series of conditional statements that executes the appropriate Op method based on the choiceProject3
and ComplexNumber
, you can try it out in the Interactions Pane. Make sure you have Project3, ComplexNumber, and JLiveRead open. Compile the three files and then type:
>java Project3in the Interactions Pane. This will run your Java application! Try all the operations. Then answer the following questions in
q1q5.txt
:
for
-loop header contains an update that is an increment (or decrement) statement. The for
-loop header in the main
method in Project3.java
does not have an update, yet the for
-loop terminates eventually. Why? The update in this for
-loop is not an increment statement, what is it?DecimalFormat
class in the API.
Submit your Project3
and ComplexNumber
files on-line using CMS (Course Management System) before the project deadline. Make sure you are submitting the correct, up to date, .java files (not .class or .java~). We will not accept any files after the deadline for any reason (except for documented medical reasons). See the CMS link on the web page for instructions on using CMS. If necessary, turn off the DrJava feature that saves the .java~ files (see course webpage announcement on 2/17 for instructions).