CIS 120 Problem-solving and Programming Concepts I

advertisement
Name:_______________________________________ Date:________________ Section:______
CIS 120 Problem-solving and Programming Concepts I
Lab Exercise 4: Problem-Solving and Introduction to Java Applets
Purpose:
The purpose of this lab is to provide an experience with a simple applet that uses the selection control
structure (if statements) and relational operators. The problem-solving design will be accomplished
through a flowchart.
Objectives
After this laboratory, you should be able to:
1. Create a flowchart for a simple program that uses input, output, and selection.
2. Create an applet and its HTML file.
3. Use if statements and relational operators to solve a problem.
4. Differentiate between Java applications and applets.
Associated readings:
Textbook:
Chapter 3.
CIS 120 Notes: Module 2.
See page 80 for information on relational operators.
See page 81 for a program that uses if statements.
See page 119 for an example of a simple HTML file that contains an applet.
See page 123 for an example of an applet that performs similar tasks using init and paint methods.
Grading:
0
1
2
Lab not done.
Lab done with substantial problems.
Lab done well.
To turn in:
In addition to your final source code and output results, turn in this handout with your answers to each
of the questions legibly written in the space provided. These items should be turned in at the end of
this lab session or at the beginning of the next lab session. If you need to wait until the beginning of
the next lab to turn in your work, be sure to print all materials before the lab day. Do not print
work at the beginning of the next lab since this disrupts the start of that lab.
Exercise 1. Read and “understand” (Polya) the following problem statement and “design” a solution
by creating a flowchart that solves the problem. The flowchart should be generic in the
sense that it could be implemented as either an applet or an application.
Problem Statement: Write an applet that collects five integer inputs and
displays the largest value and smallest value from the inputs.
Question 1:
Create a flowchart to solve the problem described above.
Exercise 2: Create and compile an applet called MinMaxApplet that contains an empty
init and paint method (see page 123 for an example of these two methods).
Continue this effort until your program compiles without errors. Do not run
the applet yet.
Question 2: What major difference do you see between the structure of a Java
application and an applet?
Exercise 3: In the init method insert code to obtain user input for one integer. Compile
until error free.
Question 3:
Now try to run the program using java. What happens?
Exercise 4: Write the HTML file (use page 119 of your textbook as a guide). The code
to be accessed by this file will be MinMaxApplet.class. Save the file as
applet.htm. Now try to run the applet by entering: appletviewer applet.htm
(also on the command line).
Question 4: What happens this time? (It should run and prompt the user for input. Also
note that applets do not need the System.exit(0) to terminate correctly.)
Exercise 5: Insert the code for the other four inputs. Compile with javac and run using appletviewer.
Exercise 6: Insert the code to find the minimum value in the init method. Compile with javac and
run using appletviewer.
Question 5: What happens if you do not use parentheses around the condition in your if
statements?
Exercise 7: Insert the code to display the minimum value in the paint method. Use the drawString
method. Compile with javac and run using appletviewer.
Question 6: The min identifier should be declared in
 the init method,
 the paint method, or
 at the class level?
Exercise 8: Follow the same procedure to find and display the maximum value. When it compiles
and runs, you’ve just finished your first applet!
Question 7: Why do we “code a little and test a lot?” Does it save time in the long run?
Why is this even more important for larger programs?
Download