Exploring Java

Objectives

At the end of this exercise, students will:

Roles

Getting ready

  1. 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.
  2. Coordinator, get one copy of the Exit Pass.
  3. 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.
  4. All team members - record your notes on your own response sheet.
  5. 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
  1. /**********************************************************
  2.  * Grade is a program that reads in three grades and computes
  3.  * the weighted average of the grades
  4.  *
  5.  * @author Nancy Harris
  6.  * @version V1 09/01/2009
  7.  ******************************************************/
  8. public class Grade
  9. {
  10.     public static void main (String args[])
  11.     {
  12.         // declare your containers
  13.         double midterm1;
  14.         double midterm2;
  15.         double finalExam;
  16.         double weightMid1;
  17.         double weightMid2;
  18.         double weightFinal;
  19.         double grade;
  20.        
  21.         // declare and load the constants
  22.         final double MIDTERM1_WEIGHT = .3;
  23.         final double MIDTERM2_WEIGHT = .4;
  24.         final double FINAL_WEIGHT = .3;
  25.        
  26.         // declare the input object
  27.         Scanner keyboard;
  28.        
  29.         // input the scores
  30.         keyboard = new Scanner(System.in);
  31.        
  32.         midterm1 = keyboard.nextDouble();
  33.         midterm2 = keyboard.nextDouble();
  34.         finalExam = keyboard.nextDouble();
  35.        
  36.         // Calculate the grade
  37.         weightMid1 = midterm1 * MIDTERM1_WEIGHT;
  38.         weightMid2 = midterm2 * MIDTERM2_WEIGHT;
  39.         weightFinal = finalExam * FINAL_WEIGHT;
  40.        
  41.         grade = weightMid1 + weightMid2 + weightFinal;
  42.        
  43.         // output
  44.         System.out.println("The final grade is " + grade + ".";
  45.     }
  46. }

Exploring the model

  1. Which line(s) in the Java program correspond to line 1a in the algorithm?  In other words, what lines are defining variable containers?
  2. Which line(s) in the Java program correspond to line 1.b in the algorithm?  
  3. What kind of containers are defined in Step 2?
  4. How are these lines (step 2 lines) different than the lines you found in Step 1
  5. What line is calculating the final result?
  6. What line is displaying (printing) the final result?

Extending the model

  1. Look at lines 12 and 21 in the Java program?  What do you think these lines do?
  2. For whose benefit are these lines, the computer to run the program or the programmer who must read the program?
  3. What does the "+" symbol do in line 41 of the Java progarm?  (Think of the calculation)
  4. What does the "+" symbol do in line 44 of the Java program?  (Think of the output)

Additional questions

  1. 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.