Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. /**
  3.  *
  4.  * Exercise 8
  5.  * Write a Java application to test the LinkedList class and
  6.  * perform the following:
  7.  * -Insert TEN (10) integers
  8.  * -Calculate the sum
  9.  * -Find the minimum and maximum.
  10.  * -Compute the average.
  11.  * -Count the number of odd and even integers.
  12.  *
  13.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  14.  * @version 10 OCTOBER 2012
  15.  */
  16. import java.util.*;
  17. import java.text.DecimalFormat;
  18. public class exercise8
  19. {
  20.     public static void main(String [] args)
  21.     {
  22.         //local declaration
  23.         LinkedList list = new LinkedList();
  24.         Scanner sc = new Scanner(System.in);
  25.         DecimalFormat dc = new DecimalFormat("0.0000");
  26.         int NoOfInt, sum=0, minimum, maximum, odd=0, even=0;
  27.         double average;
  28.        
  29.        
  30.         //Insert TEN (10) integers
  31.         System.out.println("Enter the number of integers to be input.[E.g: 10]:");
  32.         NoOfInt = sc.nextInt();
  33.        
  34.         System.out.println("Enter "+NoOfInt+" integers");
  35.         for (int i=1; i<=NoOfInt; i++)
  36.         {
  37.             System.out.print(i+": ");
  38.             list.insertAtBack(sc.nextInt());
  39.         }
  40.         System.out.print("\nList:"); list.display();//check list
  41.        
  42.  
  43.         //math.hws.edu/javanotes/c9/s2.html
  44.             Node temp = list.getFirst();
  45.             minimum = maximum = temp.getData();
  46.             while (temp != null)
  47.             {
  48.                 int checkInt = temp.getData();
  49.                        
  50.                 sum += checkInt;//Calculate the sum
  51.                 if (checkInt>maximum)//Find the minimum and maximum.
  52.                     maximum=checkInt;
  53.                 if (checkInt<minimum)
  54.                     minimum=checkInt;
  55.                 //Count the number of odd and even integers.
  56.                 if (checkInt%2==0)
  57.                     even++;
  58.                 else odd++;
  59.                 temp = temp.getLink();
  60.             }
  61.             System.out.println();
  62.             average = sum *1.0 / list.size();//Compute the average.
  63.         System.out.println("Sum of linked list element: "+sum);//display sum
  64.         System.out.println("Minimum of linked list element: "+minimum);//display sum
  65.         System.out.println("Maximum of linked list element: "+maximum);//display sum
  66.         System.out.println("Average of linked list element: "+dc.format(average) );//display sum
  67.         System.out.println("Number of Odd integers of linked list element: "+odd);//display sum
  68.         System.out.println("Number of Even integers of linked list element: "+even);//display sum
  69.         //closeScanner
  70.         sc.close();
  71.     }
  72. }