I wrote below code to satisfy program requirement as follows:
Average of Three Write a program that reads three whole numbers and
displays the average of the three numbers.Input Notes: Three whole numbers (non-negative integers) are entered
at the console.Output Notes (Prompts and Labels): The program prompts for the three
integers with these strings: “Enter the first integer.”, “Enter the
second integer.”, “Enter the third integer.”. The program then prints
The average of NUMBER1, NUMBER2, and NUMBER3 = AVG where NUMBER1 is the first integer value entered and NUMBER2 and NUMBER3
the subsequent integers, and AVG is the computed average value.SPECIFICATION OF NAMES: Your application class should be called
Average3:
My source code:
import java.util.Scanner;
public class Average3 {
/**
* @param args
*/
public static void main(String[] args) {
int AVG, NUMBER1, NUMBER2, NUMBER3;
System.out.println("Enter the first integer.");
Scanner keyboard = new Scanner(System.in);
NUMBER1 = keyboard.nextInt();
System.out.println("Enter the second integer.");
NUMBER2 = keyboard.nextInt();
System.out.println("Enter the third integer.");
NUMBER3 = keyboard.nextInt();
AVG = (NUMBER1 + NUMBER2 + NUMBER3) / 3;
System.out.println("The average of NUMBER1, NUMBER2, and NUMBER3 = " + AVG);
}
}
My program compiles fine but I know I could have implemented and invoked an object with an associated method but am struggling where to start I understand the methods and objects conceptually but not as far as writing code. Anyone have any suggestions?
First, I would create a class
InputDatathat would store the inputs from the user:This in itself is a useful general technique: collect several related values into a single data structure. You can then either rewrite your
mainmethod to use this class instead of three separateintvariables, or you can go one step further and add some behavior to theInputDataclass. The obvious first behavior to add would be to compute the average:With this, you could rewrite your
mainas follows:Notice that I’ve made
InputDataa static inner class ofAverage3. It could also have been a separate, top-level class in the same file (as long as it wasn’tpublic) or a class in a separate file.An improvement on this would be to use an array instead of separate
intfields in theInputDataclass. Your program might then look like this:Here I’ve added a constructor to
InputDatato initialize the array and given it an argument to set the size of the array.You could then introduce additional improvements like making the number of input values dynamic (using an
ArrayList<Integer>instead of anintarray). But this goes beyond the specific requirements of the assignment. Some people (many, actually) tend to do this kind of generalization automatically. However, advocates of Extreme Programming will point out that it is generally better to keep things simple: design and code for the needs of today instead of those of tomorrow, next week, or next month.In other words, you don’t know what the next assignment will bring, so however you generalize beyond the immediate assignment might be wasted work.