CSC 171 LAB #2
FALL 2001
GOALS:
1.
Become
familiar with input and output
2.
Become
familiar with variable assignment
3.
Perform
arithmetic operations
4.
Become
familiar with Boolean values and use them to modify a program’s flow of control
5.
Become
familiar with the JoptionPane swing GUI component.
TASKS:
·
Demonstrate
a program, which prompts the user for input and displays output.
Background:
Suppose you want to find both solutions to the
quadratic equation.
![]()
The quadratic formula tells us that the solutions
are:
![]()
Your demonstration program must prompt the user for values to be used for the coefficients a,b, and c. Your program must calculate the solutions using the quadratic formula and display the results. In the output display, your program must indicate the condition of the solution.
1.
If
the value of a = = 0, then your program should not attempt to divide by zero.
Rather, your output should indicate a degenerative case.
2.
Under
the circumstance (i.e. (b2-4ac) > 0), your program should
indicate “2 real solutions” and print the values.
3.
Under
the circumstance (i.e. (b2-4ac) == 0), your program should indicate
“1 real solutions” and print the value.
4.
Under
the circumstance (i.e. (b2-4ac) < 0), your program should
indicate “2 imaginary solutions” and display the values in imaginary form
(a+bi).
Use the JOptionPane swing component for input and
output.
STEPS:
1.
Don’t
panic. The best way to eat an elephant is “one bite at a time”. Although there
are a lot of parts used in this lab, they are all covered either in the Chapter
2 readings or in this lab. By following the steps in this lab, building on the
previous lab, you will learn the components necessary to complete this lab. Be
sure to compile and run your code at each step.
2.
Read
Chapter 2 of D&D before beginning this lab. It will save you lots of time.
3.
Don’t
succumb to the temptation to copy the work of a friend. You have to hand in
your code and we check.
4.
Begin
with the demonstration program (Fig 2.6 of D&D). Type this program in, as
presented in the text and be certain that you can get it to work. In doing so,
you will become familiar with the use of the swing library.
5.
Type
in and run the demonstration program which adds two integers (Fig 2.9 of
D&D). The key items to pay attention to in this example is the use of local
variables in the main() routine.
6.
Modify
the code of the Addition Application.
1.
Make
the application add 3 numbers rather than 2. Change the variable names so that
they are meaningful to the problem at hand. In order to do this, you need to
declare two new variables, invoke a new showInputDialog, and translate with a
new parseInt.
2.
Change
the application to read and add floating point (double) numbers rather than
integers. To do this, change the declarations to doubles, and the
“Integer.parseInt”s to “Double.parseDouble”s.
3.
Modify
the arithmetic operation. Rather than simply adding the numbers, calculate and
output the discriminant value (b2-4ac).
7.
At
this point, you have achieved 4 of the 5 goals of this lab.
8.
It
turns out that the JAVA language has good support for common mathematical
functions. One common mathematical operation is the taking of a square root,
which you need to do this lab. In the future, when we study methods, we will
look at the Java “Math” package. For now, it is enough to know that the
following statement is a legal JAVA instruction.
double y = 9.0 ;
double x = Math.sqrt(y); // x will now contain the
value 3.0
9.
Modify
your application to display the square root of the discriminant. What happens
when (b2 – 4ac) < 0 ?
10.
Re-read
section 2.8 of Chapter 2 to brush up on “Decision making”.
11.
So,
how do we deal with square roots? We can use an if statement, and the following
logic, which you must implement in your program.
1.
If
the value of the discriminant is greater than or equal to zero, then simply
take the square root. Set a result String (see Fig 2.20) to the value of that
square root with a “+” in front.
2.
If
the value of the discriminant is less than zero then
1.
negate
the discriminant
2.
take
the square root of the negation
3.
Set
a result String (see Fig 2.20) to the value of that square root with a “i”
after it. (“i” of course, represents the square root of negative one).
3.
Display
the result string.
12.
At
this point, you are able to control your program based on the value of the
discriminant. Implement 4 if statements – one for each condition in the “Background” section,
above.
13.
Ok,
you are done to get credit for all this work you need to get a piece of paper
ready for your lab TA. This paper must have 2 things on it.
1.
Your
name and lab section
2.
Your
final version of the modified program.
14.
When
you get the paper ready call your lab TA over and run the program for her/him.
(This is called “demoing”). Give the paper to your TA. This completes the hand
in process. The deadline is one week – no late assignments accepted.