Tuesday, March 31, 2015

Averaging Program - Grades

Learning Java...

Continuing off from last time, I've started learning Java and using some video tutorials to make basic programs and run them. As noted before, I work in IT but have little to no programming experience beyond the HTML/CSS necessary to edit existing web pages.

The video gave the premise of a teacher using a simple averaging program for finding out the average of 10 grades, and used the following code to accomplish it
import java.util.Scanner;

class apples {
       public static void main(String args[]){
              Scanner input = new Scanner(System.in);
              int total = 0;
              int grade;
              int average;
              int counter = 0;
             
              while (counter < 10){
                     grade = input.nextInt();
                     total = total + grade;
                     counter++;
              }
              average = total/10;
              System.out.println("Your average is " + average);
             
       }
}

I saw this and immediately thought that it seemed a bit clunky, not as code but as a teaching tool. The output that you see gives you no indication of what’s going on in the code until the end, which made it harder to track what was happening.

The first change I made was to consolidate the variables and make them doubles instead of integers. since a grade can be a percentage with a decimal.
              double total = 0;
              double grade, average;


Next I tried to put in a println statement that would let you know which grade you’re working on as the counter increases.
                     while (counter < 10){
                            System.out.println("Enter grade " + counter);
                            grade = input.nextInt();
                            total = total + grade;
                            counter++;
                     }

Then I realized that the counter started at 0 instead of 1, which made it a bit weird. My first attempt to fix this resulted in a clunky increment increase for the println, then a decrease to undo the increment increase, and then an increase to move the counter up for the next iteration of the while statement.
                     while (counter < 10){
                            System.out.println("Enter grade " + ++counter);
                            grade = input.nextInt();
                            total = total + grade;
                            counter--;
                            counter++;
                     }

That worked, but it still seemed a bit odd. Then it hit me that if I changed the while statement to say <= instead of just < I could set the counter variable to start at 1 and still get 10 grades entered without the need to add, then subtract, then add again.
                     int counter = 1;
                    
                     while (counter <= 10){
                            System.out.println("Enter grade #" + counter);
                            grade = input.nextInt();
                            total = total + grade;
                            counter++;
                     }

The final code works better for me as a teaching tool because it asks for each grade by number, and then produces the result at the end, but it doesn't use any code or tricks that haven’t been taught by the video series up to this point.  The connection between what happens after every input is much clearer.

From my own perspective I like the code to respond to input, or ask for what it wants rather than just sit there and expect you to know what to do.

No comments:

Post a Comment