Monday, June 17, 2013

Java Program Methods/Arrays Exam Scores Average, Max and Min



Java Program for Arrays. Average, Random Shuffle, Max and Min Arrays for Exam Scores

For use in Eclipse.



import java.util.Scanner;
public class Put Your Class here {

 /**
  * 
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  double[] exam1 = new double[10];
  double average;
  double max, min;
  

  fillArray(exam1);
  printArray(exam1);
  average = getArrayAverage (exam1);
  System.out.printf(" Average Exam 1 Score is %6.2f \n", average);
  max = getMax(exam1);
  min = getMin(exam1);
  randomShuffle(exam1);
  printArray(exam1);
 
  selectionSort(exam1);
  printArray(exam1);
  
  
  
 }//End of main
 public static void fillArray( double[] list) {
  Scanner myIn = new Scanner(System.in);
  for (int i=0; i <list.length; i++){
   System.out.print("Enter a number: ");
   list[i] = myIn.nextDouble();
  }//end of for loop
 }//end of the fill array loop

 public static void printArray(double[] list) {
  System.out.println(" \t Index \t Value \n");//print header
  System.out.println("=======================");
  for (int i=0; i <list.length; i++){
   System.out.println("\t" + i + "\t" + list[i]);
  }//end of for loop
 }//end of print array method
 
 public static void randomShuffle(double[] list) {
  int j; 
  double temp;
  for (int i= 0; i <list.length; i++) {
   j = (int)(Math.random() * 5);
 
  j= (int) (Math.random() * list.length);
  temp = list[i];
  list[i] = list[j];
  list[j] = temp;
 }//end of for
}// end of random shuffle
 
 public static void selectionSort(double[] list) {
  double currentMin;
  int currentMinIndex;
  
  for (int i = 0; i < list.length - 1; i++) {
   currentMin = list[i];
   currentMinIndex = i;
   
   for (int j= i + 1; j < list.length; j++){
    if (list[j] < currentMin) {
     currentMin = list[j]; currentMinIndex = j;
    }//end if
   }//end inner loop
   if (currentMinIndex != i) {
    list[currentMinIndex] = list[i];
    list[i] =currentMin;
   }//end if
  }// end outer loop
 }//end selection sort
 
 public static double  getArrayAverage(double[] list) {
  double avg=0.0, total=0.0;
  
  for (int i=0; i <list.length; i++){
   total = total + list[i];
  }//end of for loop
  if (list.length > 0) avg = total / list.length;
  return avg;
 }//end of the get array average method
 
 public static double getMax(double[] list) {
  double m = Double.MAX_VALUE;
  for (int i =0; i < list.length; i++){
   if (list[i] > m) m = list[i];
  }
  return m;
 }
 public static double getMin(double[] list) {
  double n = Double.MAX_VALUE;
  for (int i =0; i < list.length; i++){
   if (list[i] < n) n = list[i];
  }
  return n;
 }

 
}//End


1 comment:

  1. 3D titanium dioxide in food products - TikTok
    4D titanium dioxide in food products. oakley titanium glasses TikTok. Find best prices ford focus titanium hatchback on titanium bmx frame T-Shirts, TikTok Promo Codes, T-Shirt, and more on titanium iphone case TikTok. See titanium wedding bands more TikTok products for your

    ReplyDelete