Personal tools
You are here: Home Comp 170 Introduction to OOP Lab Materials Lab9 AverageSix
Document Actions

AverageSix

by Mit Patel last modified 2006-03-22 02:20 AM

AverageSix class for download

Click here to get the file

Size 1.6 kB - File type text/plain

File contents

// Chapter 6 of Java How To Program
// Debugging Problem
import java.util.Scanner;

public class AverageSix
{
   public void getGrades()
   {
      Scanner input = new Scanner( System.in );

      System.out.println( "Please enter the number of students you wish to average" );  
      int students2 = input.nextInt();

      int NumOfStudents = students2; 
      int counter = 1;  
      double total = 0, previousNumber = 0, newMaximum = 0, newMinimum = 0; 

      while ( students2 != 0 ) 
      {    
         System.out.println( "Please enter a grade" ); 
         double givenNum = input.nextDouble();
         total = total + givenNum; 

         if ( counter == 1 ) 
         {
            previousNumber = givenNum; 
            newMinimum = givenNum; 
         } // end if

         if ( newMaximum < givenNum )
            newMaximum = maximum( previousNumber, givenNum );

         if ( newMinimum > givenNum )
            newMinimum = minimum( previousNumber, givenNum ); 

         students2--;
         counter++; 
         previousNumber = givenNum;
      } // end while

      double average1 = average( total, NumOfStudents ); 

      System.out.printf( "The maximum: %.2f\nThe average: %.2f\nThe minimum: %.2f\n", 
         newMaximum, average1, newMinimum);
   } // end method getGrades
} // end class AverageSix

public double average( double x, y );
{
   return x / y; 
} // end method average

public double maximum( double x, double y )
{
   Math.max( x, y ); 
} // end method maximum

public double minimum( double x, int y )
{
   return min( x, y );
} // end method minimum

« December 2022 »
Su Mo Tu We Th Fr Sa
123
45678910
11121314151617
18192021222324
25262728293031
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: