Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Exam_Preparation
- {
- public static void main(String[] args)
- {
- String messages[] = {
- "\n\n1.\tEnter length of each song on the CD",
- "\n2.\tDisplay all song lengths",
- "\n3.\tDisplay the length of the longest song",
- "\n4.\tDisplay the length of the shortest song",
- "\n5.\tCalculate and display the average song length",
- "\n6.\tExit system\n"
- };
- Scanner input = new Scanner(System.in);
- int option = 0;
- System.out.print("Please enter the number of tracks on the CD : ");
- int size = input.nextInt();
- double songs[] = new double[size];
- do
- {
- // No reason to do this other than i can :P
- for(int i = 0; i < messages.length; i++)
- {
- System.out.print(messages[i]);
- }
- option = input.nextInt();
- switch (option)
- {
- case 1 :
- AddTracks(songs);
- break;
- case 2 :
- GetTrackList(songs);
- break;
- case 3 :
- GetLongestTrack(songs);
- break;
- case 4 :
- System.out.print("\n\tShortest track is " + GetShortestTrack(songs));
- break;
- case 5 :
- System.out.print("\n\tThe average track length is " + GetAverageLength(songs));
- break;
- case 6 :
- System.out.print("\nClosing program...");
- System.exit(0);
- break;
- default:
- System.out.print("Please select an option 1 - 6.");
- }
- }
- while(option != 50);
- }
- /* Method AddTracks()
- Adds elements to the array, it will keep looping
- until the array is full. */
- public static void AddTracks(double[] track)
- {
- Scanner input = new Scanner(System.in);
- for(int i =0; i < track.length; i++)
- {
- System.out.print("\tPlease enter track length in minutes : ");
- double newTrack = input.nextDouble();
- track[i] = newTrack;
- }
- }
- /* Method ListAll()
- Iterates thru the array and prints it to the console. */
- public static void GetTrackList(double[] track)
- {
- for(int i = 0; i < track.length; i++)
- {
- System.out.println("\tTrack number " + (i + 1) + " is " + track[i] + " minutes long.");
- }
- }
- /* Method LongestTrack()
- Sorts the elements in alphabetical order and iterates thru
- to the last element and prints it to the console. */
- public static void GetLongestTrack(double[] track)
- {
- // Lazy way.
- Arrays.sort(track);
- System.out.println("\tLargest track is " + track[track.length - 1] );
- /*
- double longest = 0.0;
- for(int i = 0; i < track.length; i++)
- {
- if(track[i] > longest)
- longest = track[i];
- }
- System.out.println("\tLargest track is " + longest );
- */
- }
- /* Method ShortestTrack()
- Sorts the elements in alphabetical order and returns the first
- element */
- public static double GetShortestTrack(double[] track)
- {
- // Lazy way.
- Arrays.sort(track);
- return track[0];
- /* best way to learn how to do it.
- double shortest = 5.0;
- for(int i = 0; i < track.length; i++)
- {
- if(track[i] < shortest)
- shortest = track[i];
- }
- return shortest;
- */
- }
- /* Method AverageLength()
- iterates thru the array adding up the total and divides it by the
- array length to get the average */
- public static double GetAverageLength(double[] track)
- {
- double total = 0;
- for(int i = 0; i < track.length; i++)
- {
- total = total + track[i];
- }
- return( total / track.length );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement