05.0 Java Notes‎ > ‎

Java: Min, Max, Average




import java.util.Scanner;
/**
 * Write a description of class MinMaxAverage here.
 * 
 * @author (Mister V) 
 * @version (October 2013)
 */
public class MinMaxAverage
{
    // instance variables 
    private int min;
    private int max;
    private double average;
    private int mark;
    private int numberOfMarks;
    private Scanner kb;

    /**
     * Constructor for objects of class MinMaxAverage
     */
    public MinMaxAverage()
    {
       initialize();
       setData();
       displayData();
    }
    
    private void initialize(){
       min = 100;
       max = 0;
       average = 0;
       numberOfMarks = 5;
       kb = new Scanner(System.in);    
    }

    private void setData()
    {   int totalMarks =0;
        for (int x = 0; x<numberOfMarks; x++){
            setMark();
            totalMarks = totalMarks + mark;
            if (mark > max) max = mark;
            if (mark < min) min = mark;
        }
        average = totalMarks / numberOfMarks;
    }
    
    private void setMark(){
        System.out.print("Enter a Mark:  ");
        mark = kb.nextInt();
    }
    
    private void displayData(){
    System.out.println("the average mark is:  " + average);
    System.out.println("the highest mark is:  " + max);
    System.out.println("the lowest mark is:  " + min);    
    
    }
}


Comments