This preview shows pages 1–3. Sign up to view the full content.
Handout #12
CS 106A
June 29, 2009
Assignment #2—Simple Java Programs
Based on Assignment by Eric Roberts
Due: Monday, July 6 at 10:30am SHARP
Your job in this assignment is to write programs to solve each of these problems.
1.
In highschool algebra, you learned that the standard quadratic equation
a
x
2
+
b
x
+
c
=
0
has two solutions given by the formula
x
=
–b ±
b
2
– 4ac
2a
The first solution is obtained by using + in place of ±; the second is obtained by
using – in place of ±.
Most of this expression contains simple operators covered in
Chapter 3.
The one piece that’s missing is taking square roots, which you can do by
calling the standard function
Math.sqrt
.
For example, the statement
double y = Math.sqrt(x);
sets
y
to the square root of
x
.
Write a
ConsoleProgram
that accepts values for
a
,
b
, and
c
, as
ints
and then
calculates the two solutions, as
doubles
.
If the quantity under the square root sign is
negative, the equation has no real solutions, and your program should display a
message to that effect.
You may assume that the value for
a
is nonzero.
Your
program should be able to duplicate the following sample run:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document– 2 –
2.
Write a
ConsoleProgram
that reads in a list of integers, one per line, until a sentinel
value of 0 (which you should be able to change easily to some other value).
When the
sentinel is read, your program should display the smallest and largest values in the
list, as illustrated in this sample run:
Your program should handle the following special cases:
•
If the user enters only one value before the sentinel, the program should report
This is the end of the preview. Sign up
to
access the rest of the document.
 Fall '08
 SAHAMI,M

Click to edit the document details