/**
*
* Exercise 8
* Write a Java application to test the LinkedList class and
* perform the following:
* -Insert TEN (10) integers
* -Calculate the sum
* -Find the minimum and maximum.
* -Compute the average.
* -Count the number of odd and even integers.
*
* @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
* @version 10 OCTOBER 2012
*/
import java.util.*;
import java.text.DecimalFormat;
public class exercise8
{
public static void main(String [] args)
{
//local declaration
LinkedList list = new LinkedList();
Scanner sc = new Scanner(System.in);
DecimalFormat dc = new DecimalFormat("0.0000");
int NoOfInt, sum=0, minimum, maximum, odd=0, even=0;
double average;
//Insert TEN (10) integers
System.out.println("Enter the number of integers to be input.[E.g: 10]:");
NoOfInt = sc.nextInt();
System.out.println("Enter "+NoOfInt+" integers");
for (int i=1; i<=NoOfInt; i++)
{
System.out.print(i+": ");
list.insertAtBack(sc.nextInt());
}
System.out.print("\nList:"); list.display();//check list
//math.hws.edu/javanotes/c9/s2.html
Node temp = list.getFirst();
minimum = maximum = temp.getData();
while (temp != null)
{
int checkInt = temp.getData();
sum += checkInt;//Calculate the sum
if (checkInt>maximum)//Find the minimum and maximum.
maximum=checkInt;
if (checkInt<minimum)
minimum=checkInt;
//Count the number of odd and even integers.
if (checkInt%2==0)
even++;
else odd++;
temp = temp.getLink();
}
System.out.println();
average = sum *1.0 / list.size();//Compute the average.
System.out.println("Sum of linked list element: "+sum);//display sum
System.out.println("Minimum of linked list element: "+minimum);//display sum
System.out.println("Maximum of linked list element: "+maximum);//display sum
System.out.println("Average of linked list element: "+dc.format(average) );//display sum
System.out.println("Number of Odd integers of linked list element: "+odd);//display sum
System.out.println("Number of Even integers of linked list element: "+even);//display sum
//closeScanner
sc.close();
}
}