Exploring Java
Objectives
At the end of this exercise, students will:
- Be able to read a simple Java program.
- Be able to recognize basic features of a Java program; comments,
declarations, manipulation and output.
- Be able to equate steps in an algorithm with the corresponding
Java structures.
- Will become familiar with features of the textbook.
Roles
- Coordinator
- Presenter
- Recorder
- Reflector
Getting ready
- Coordinator, get enough copies of the response sheets for
each
member of your team. Pick up one copy of the scavenger hunt for
each
member of the team. Pick up one copy of the exercises for your
team.
- Coordinator, get one copy of the Exit Pass.
- Recorder, write the names of each of your team members on one of
the response sheets and circle your name. You will turn this one
in at the end of class.
- All team members - record your notes on your own response sheet.
- Reflector or all team members - record the "went well", "needs
improvement", and "fuzzy" items on the Exit pass and turn in at the end
of class.
First activity
1. You have your textbooks. Now see what resources are
available on the book and accompanying CS.
2. Individually, do the Textbook Scavenger Hunt. As a
group, consider the question, How do you think you should read this
book?
3. BOARD, Recorder, put your group ideas up on the board.
Background on programming
We have looked at algorithms and some of the properties of algorithms.
We have refined our algorithms to be more specific about labeling
values that we are using in our algorithms. Now it is time to see
how these are implemented in code.
The model
The algorithm for calculating the weighted average of three grades.
1. Declare your containers:
a. Create 7 variable decimal
containers named: midterm1, midterm2, final, weightMid1, weightMid2,
weightFinal, grade.
b. Create 3 constant
containers named MIDTERM1_WEIGHT, MIDTERM2_WEIGHT, FINAL_WEIGHT.
c. Create 2 literal containers
containing the values .3 and .4.
d. Create 2 literal contains
containing the values “The final grade is “ and
“.”
2. Load the constants
a. Store the literal .3 in
MIDTERM1_WEIGHT.
b. Store the literal .4 in
MIDTERM2_WEIGHT.
c. Store the literal .3 in
FINAL_WEIGHT.
3. Input the scores
a. Input the midterm grade and
store it in the container named midterm1.
b. Input the second midterm grade
and store it in the container named midterm2.
c. Input the final exam grade and
store it in the container named final.
4. Calculate the grade
a. Multiply midterm1 by
MIDTERM1_WEIGHT and store the result in weightMid1.
b. Multiply midterm2 by
MIDTERM2_WEIGHT and store the result in weightMid2.
c. Multiply final by FINAL_WEIGHT
and store the result in weightFinal.
d. Add weightFinal, weightMid1,
and weightMid2 and store the result in grade.
5. Output
a. Output “The final grade
is “, grade, and “.”.
The corresponding Java program
- /**********************************************************
- * Grade is a
program that reads in three grades and computes
- * the weighted
average of the grades
- *
- * @author Nancy
Harris
- * @version V1
09/01/2009
- ******************************************************/
- public class Grade
- {
- public
static void main (String args[])
- {
-
// declare your containers
-
double midterm1;
-
double midterm2;
-
double finalExam;
-
double weightMid1;
-
double weightMid2;
-
double weightFinal;
-
double grade;
-
-
// declare and load the constants
-
final double MIDTERM1_WEIGHT = .3;
-
final double MIDTERM2_WEIGHT = .4;
-
final double FINAL_WEIGHT = .3;
-
-
// declare the input object
-
Scanner keyboard;
-
-
// input the scores
-
keyboard = new Scanner(System.in);
-
-
midterm1 = keyboard.nextDouble();
-
midterm2 = keyboard.nextDouble();
-
finalExam = keyboard.nextDouble();
-
-
// Calculate the grade
-
weightMid1 = midterm1 * MIDTERM1_WEIGHT;
-
weightMid2 = midterm2 * MIDTERM2_WEIGHT;
-
weightFinal = finalExam * FINAL_WEIGHT;
-
-
grade = weightMid1 + weightMid2 + weightFinal;
-
-
// output
-
System.out.println("The final grade is " + grade +
".";
- }
- }
Exploring the model
- Which line(s) in the Java program correspond to line 1a in the
algorithm? In other words, what lines are defining variable
containers?
- Which line(s) in the Java program correspond to line 1.b in the
algorithm?
- What kind of containers are defined in Step 2?
- How are these lines (step 2 lines) different than the lines you
found in Step 1
- What line is calculating the final result?
- What line is displaying (printing) the final result?
Extending the model
- Look at lines 12 and 21 in the Java program? What do you
think these lines do?
- For whose benefit are these lines, the computer to run the
program or the programmer who must read the program?
- What does the "+" symbol do in line 41 of the Java progarm?
(Think of the calculation)
- What does the "+" symbol do in line 44 of the Java program?
(Think of the output)
Additional questions
- What would the output statement look like to produce the output,
"The final grade for grades, X, Y, and Z, is GPA."
where X, is midterm 1, Y is midterm 2, and Z is the final exam.
The GPA corresponds to grade in the former algorithm.
2, What line(s) would you change if the weight
of the midterm 1 was only 25% and the final exam changed to 35%.
What would that/those line(s) look like once the change was made.
Turning it in
Turn in one copy of the question sheet, with all team members names to
the folder in the front. Put your Exit Pass in the folder to
reflect on questions remaining from this exercise. Turn in the
recorder's copy of the response sheet and the Exit pass
before leaving today.