Advertisement
irishstorm

Exam_Preparation.java

Nov 26th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Exam_Preparation
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         String messages[] = {  
  8.                                 "\n\n1.\tEnter length of each song on the CD",
  9.                                 "\n2.\tDisplay all song lengths",
  10.                                 "\n3.\tDisplay the length of the longest song",
  11.                                 "\n4.\tDisplay the length of the shortest song",
  12.                                 "\n5.\tCalculate and display the average song length",
  13.                                 "\n6.\tExit system\n"
  14.                             };
  15.  
  16.         Scanner input = new Scanner(System.in);
  17.         int option = 0;
  18.  
  19.         System.out.print("Please enter the number of tracks on the CD : ");
  20.         int size = input.nextInt();
  21.  
  22.         double songs[] = new double[size];
  23.  
  24.         do
  25.         {
  26.             // No reason to do this other than i can :P
  27.             for(int i = 0; i < messages.length; i++)
  28.             {
  29.                 System.out.print(messages[i]);
  30.             }
  31.  
  32.             option = input.nextInt();
  33.  
  34.             switch (option)
  35.             {
  36.                 case 1 :
  37.                     AddTracks(songs);
  38.                 break;
  39.  
  40.  
  41.                 case 2 :
  42.                     GetTrackList(songs);
  43.                 break;
  44.  
  45.  
  46.                 case 3 :
  47.                     GetLongestTrack(songs);
  48.                 break;
  49.  
  50.  
  51.                 case 4 :
  52.                     System.out.print("\n\tShortest track is " + GetShortestTrack(songs));
  53.  
  54.                 break;
  55.  
  56.  
  57.                 case 5 :
  58.                     System.out.print("\n\tThe average track length is " + GetAverageLength(songs));
  59.  
  60.                 break;
  61.  
  62.  
  63.                 case 6 :
  64.                     System.out.print("\nClosing program...");
  65.                     System.exit(0);
  66.                 break;
  67.  
  68.  
  69.                 default:
  70.                     System.out.print("Please select an option 1 - 6.");
  71.  
  72.             }
  73.         }
  74.         while(option != 50);
  75.     }
  76.  
  77.  
  78.  
  79.  
  80.    
  81.     /*  Method AddTracks()
  82.         Adds elements to the array, it will keep looping
  83.         until the array is full.    */
  84.     public static void AddTracks(double[] track)
  85.     {
  86.         Scanner input = new Scanner(System.in);
  87.  
  88.         for(int i =0; i < track.length; i++)
  89.         {
  90.             System.out.print("\tPlease enter track length in minutes : ");
  91.             double newTrack = input.nextDouble();
  92.  
  93.             track[i] = newTrack;
  94.         }
  95.     }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.    
  103.     /*  Method ListAll()
  104.         Iterates thru the array and prints it to the console.    */
  105.     public static void GetTrackList(double[] track)
  106.     {
  107.         for(int i = 0; i < track.length; i++)
  108.         {
  109.             System.out.println("\tTrack number " + (i + 1) + " is " + track[i] + " minutes long.");
  110.         }
  111.     }
  112.    
  113.  
  114.  
  115.  
  116.  
  117.  
  118.    
  119.     /*  Method LongestTrack()
  120.         Sorts the elements in alphabetical order and iterates thru
  121.         to the last element and prints it to the console.    */
  122.     public static void GetLongestTrack(double[] track)
  123.     {
  124.         // Lazy way.
  125.         Arrays.sort(track);
  126.         System.out.println("\tLargest track is " +  track[track.length - 1] );
  127.  
  128.         /*
  129.         double longest = 0.0;
  130.        
  131.         for(int i = 0; i < track.length; i++)
  132.         {
  133.             if(track[i] > longest)
  134.                 longest = track[i];
  135.         }
  136.  
  137.         System.out.println("\tLargest track is " +  longest );
  138.         */
  139.     }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.    
  147.     /*  Method ShortestTrack()
  148.         Sorts the elements in alphabetical order and returns the first
  149.         element     */
  150.     public static double GetShortestTrack(double[] track)
  151.     {
  152.         //  Lazy way.
  153.         Arrays.sort(track);
  154.         return track[0];
  155.  
  156.         /* best way to learn how to do it.
  157.         double shortest = 5.0;
  158.        
  159.         for(int i = 0; i < track.length; i++)
  160.         {
  161.             if(track[i] < shortest)
  162.                 shortest = track[i];
  163.         }
  164.  
  165.         return shortest;
  166.         */
  167.     }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.    
  175.     /*  Method AverageLength()
  176.         iterates thru the array adding up the total and divides it by the
  177.         array length to get the average    */
  178.     public static double GetAverageLength(double[] track)
  179.     {
  180.         double total = 0;
  181.  
  182.         for(int i = 0; i < track.length; i++)
  183.         {
  184.             total = total + track[i];
  185.         }
  186.        
  187.         return( total / track.length );
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement