Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- 2. Based on this ArrayListNum class, write a Java application for the following purposes:-
- • Test the method insertAtFront() and display () by inserting THREE (3) integers
- • Test the method insertAtBack() and display() by inserting THREE(3) integers:
- • Test the method removeFromBack() and display() by removing TWO(2) element.
- • Calculate the sum
- • Find the minimum and maximum
- • Compute the average
- • Count how many elements are odd and how many are even.
- *
- * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
- * @version 27 SEPTEMBER 2012
- */
- import java.util.*;
- public class javaApp
- {
- public static void main (String [] args)
- {
- ArrayListNum listo = new ArrayListNum();//empty list
- Scanner sc = new Scanner(System.in);
- int sum = 0, min=0, max=0, odd=0, even=0;
- double average;
- System.out.println("\nEnter 3 integers to test insertAtFront():");
- for (int i=0;i<3;i++)
- listo.insertAtFront( sc.nextInt() );
- listo.display();
- System.out.println("\nEnter 3 integers to test insertAtBack():");
- for (int i=0;i<3;i++)
- listo.insertAtBack( sc.nextInt() );
- listo.display();
- System.out.println("\nRemoving 2 integers [AUTO] to test removeFromBack():");
- for (int i=0;i<2;i++)
- listo.removeFromBack();
- listo.display();
- //Integer element = new Integer(listo.list[0]);
- System.out.println("\nlisto.length="+listo.length+"\n");
- int element = listo.list[0];
- min = element;
- max = element;
- for (int i=0; i< listo.length; i++)
- {
- //element = new Integer(listo.list[i]);
- element = listo.list[i];
- sum = sum + element;
- if (min > element)
- min = element;
- if (max < element)
- max = element;
- if (element%2==0)
- even++;
- else
- odd++;
- }
- average = (sum*1.0) / (listo.length*1.0);
- System.out.print("\nCalculate the sum: "+sum);
- System.out.print("\nMinimum: "+min);
- System.out.print("\nMaximum: "+max);
- System.out.print("\nAverage: "+average);
- System.out.print("\nNo of element for odd number: "+odd);
- System.out.print("\nNo of element for even number: "+even);
- System.out.println("\nEnd Test.");
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement