Java I CS 142 Week 02 Assignment.docx

advertisement
Java I CS 142 Week 02 Assignment
Your job will be to:
1.
2.
3.
4.
5.
6.
7.
8.
9.
Classname= “Week02”
As always ,first line of code has your name as comment.
Write a Java program to calculate roots of the quadratic equation 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 = 0.
It should perform all calculations using “double” variables.
It should explain to the user what the program is to do, and prompt for the three coefficients a,
b, and c using JOptionPane.
If there is a single doubled real root, it should display it.
If there is a pair of real roots, it should display them both as approximations, and both in exact
form using the text “sqrt(…)”.
If there is a pair of complex conjugate roots, it should display them both as approximations, and
both in exact form using the text “sqrt(…)” and text like “(…)i”.
Submit your .java file to the OIS dropbox. Click on the “OIS” icon next to CS142 in “My Classes”
in the portal. For full credit, it is due at noon, on Sunday January 15th. It may be submitted up
to one week late for reduced credit.
Sample input and output:
… two more inputs of -7
and 12
should result in (but your program should calculate and display r2 also) 𝑥 2 − 7𝑥 + 12 = 0 should yield:
The quadratic 𝑥 2 − 4𝑥 + 4 = 0 should display something like:
Complex conjugate roots need to be formatted nicely, both exact and approximate. 𝑥 2 + 𝑥 + 1 = 0
should get results like:
Java I CS 142 Week 02 Assignment
p. 1 of 2
Note: The “exact” answers are to make a math teacher happy. The “Approximate” answers are to make
your engineering teacher happy.
Don’t forget to display both roots, even though my examples above only display one root. Of course, if
it is a doubled root, you don’t have to display it twice. And it’s OK to not use exact, since it’s always
rational.
I suggest that it will help if you declare a double with a name like “disc” or “discriminant” and calculate
the value 𝑏 2 − 4𝑎𝑐 you will find that helpful. It is easier to calculate 𝑏 2 in java as b*b than using
Math.pow(…).
Note: my solution requires only 28 lines of code. This isn’t nearly as hard as you think it is.
Some extra credit opportunities:
1. Use the Eclipse Window builder rather than using JOptionPane. If you do this, there should be
three input fields on the window, a “Calculate” button, and a label field which will display
answers.
2. If you want, trim the number of digits to be consistent with the number of digits given as input.
(using DecimalFormat)
3. Note to get extra credit, you must claim it three ways: in the Title and Description fields of the
Dropbox, and the program should display (Console or GUI) the claim of extra credit.
Java I CS 142 Week 02 Assignment
p. 2 of 2
Download