1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
galben [10]
3 years ago
13

Write an application that instantiates five Recording objects and prompts the user for values for the data fields. Then prompt t

he user to enter which field the Recordings should be sorted by—(S)ong title, (A)rtist, or playing (T)ime. Perform the requested sort procedure, and display the Recording objects. Validations:

Computers and Technology
1 answer:
tangare [24]3 years ago
7 0

Answer:

Here is the JAVA application

Recording.java:

class Recording {  //class name

String title;  //String type variable to store the title of the song

String artist;  //String type variable to store the artist name of the song

int playingTime;  //int type variable to store the playing time of the song in seconds  

public String getTitle() {  //accessor method to get the title of the song

   return title; }   //returns the current title of song

public void setTitle(String title) {  //mutator method to set the title of song

   this.title = title;  }   // sets the title of song

public String getArtist() {  //accessor method to get the artist name of the song

  return artist; }   //returns the current artist name of song

public void setArtist(String artist) {  //mutator method to set the artist name of song

  this.artist = artist; }   //sets the artist name

public int getPlayingTime() {  //accessor method to get the playing time of the song

   return playingTime; } /  /returns the playing time of song

public void setPlayingTime(int pt) {  //mutator method to set the playing time of song

   this.playingTime = pt; }   //sets the playing time of song

public Recording(String title, String artist, int playingTime) {  //parmeterized constructor of Recording class that takes title, artist and playingTime as parameters

this.title = title;  

this.artist = artist;

this.playingTime = playingTime;  }  }

Explanation:

Here is the Main class:  

import java.util.Scanner;   //to accept input from user

public class Main {  

public static void main(String[] args) {  // start of main function

 Recording[] song = new Recording[5];  //creates object of Recording class named song to instantiate five Recording objects

 Scanner input = new Scanner(System.in);  //creates Scanner class object named input

 int i = 0;  // declare i and initialize it to -

 for (i = 0; i < song.length; i++) {  //iterates through the list of song

  int j = i + 1;  

  System.out.print("Enter the title of song " + j + ": ");  //prompts user to enter the title of song

 String title = input.nextLine();   //reads the title string from user

 System.out.print("Enter the artist of song " + j + ": ");  //prompts user to enter the artist of song

 String artist = input.nextLine();   //reads the artist string from user

System.out.print("Enter the playing time of song " + j + " in seconds: ");   //prompts user to enter the playing time of song

 String playtime = input.nextLine();  //reads the playing time string value from user

 int playingTime = Integer.parseInt(playtime);   // converts String value of playtime to integer

 song[i] = new Recording(title, artist, playingTime); }   //calls constructor of Recording class by passing title, artist and playingTime to it  

 int sort;  // for choosing which field to sort

do {  //continues to ask user to enter which field the Recordings should be sorted by

System.out.println("How should these songs be sorted? 1 for title 2 for artist 3 for playing time");  

sort = input.nextInt();  //reads user choice to sort

       if (sort > 0 && sort < 4) {  //to check if user enters a valid choice

 int a, b;  

 int max = song.length - 1;  

for (a = 0; a < max; a++) {  //iterates through the length of the song list setting an index variable a

      for (b = 0; b < max; b++) {    //iterates through the length of the song list setting an index variable b

  int c = b + 1;

     if (sort == 1) {  //if the user selects title field

       if (song[b].getTitle().compareTo(song[c].getTitle()) > 0) {  // compares title of one song with the song that comes after it

   Recording temp = song[b];  //sorts the titles using bubble sort by ordering each title to the immediate next and swapping them if they are not sorted already

   song[b] = song[c];

   song[c] = temp; } }  

   else if (sort == 2) {    //if the user selects artist field

  if (song[b].getArtist().compareTo(song[c].getArtist()) > 0) {  // compares artist of one song with the song artist  that comes after it

//sorts the artists using bubble sort by ordering each title to the immediate next and swapping them if they are not sorted already

     Recording temp = song[b];

     song[b] = song[c];

     song[c] = temp;  }  }  

   else if (sort == 3) {   //if the user selects playing time field

  if (song[b].getPlayingTime() > song[c].getPlayingTime()) {

/* compares artist of one song with the song artist  that comes after it sorts the artists using bubble sort by ordering each title to the immediate next and swapping them if they are not sorted already */

       Recording temp = song[b];

        song[b] = song[c];

         song[c] = temp;  } } } }  }  

             else {  //if user enters any invalid choice

 System.out.println("Invalid choice! Enter right choice"); }        

       } while (sort < 1 || sort > 3);  //keeps asking user to enter which field the recording to sort by until the value of sort in range of 1 to 3

for (i = 0; i < song.length; i++) {  //displays the sorted song list according to selected field

System.out.println("Song: Title: " + song[i].getTitle() + ". Artist: " + song[i].getArtist() + ". Playing time: " + song[i].getPlayingTime() + " seconds.");  }

input.close();  }  }

The output of the program is attached.

You might be interested in
(TCO 4) What will be the value of input_value if the value 5 is input at run time? cin &gt;&gt; input_value; if (input_value &gt
ICE Princess25 [194]

Answer:

15

Explanation:

if ..else is the conditional statement which is used to check the condition is true or not, if the condition is true that execute the particular statement and if not it moves to else part for execution.

if condition is more than two we can use continuous if else statement

Syntax:

if(condition)

{

 statement;

}else if(condition)

{

  statement;

}else

{

 statement;

}

In the code:

The value of the input is 5.

first it goes to if part and check condition if 5 > 5, condition false it equal to 5 not greater than 5.

it then moves to else if part and check condition if 5 > 2, condition is true,

it execute the code inside the else if part. so, input_value become

5+10 which 15.

after that, program control terminate the if else statement it does not check further.

6 0
3 years ago
Which of these monitor connector types is the oldest and least desirable to use?
ella [17]

Answer:

DVI is the oldest out of the four.

Explanation:

DVI and VGA are the oldest, but DVI is oldest and least desirable to use since it is outdated.

5 0
3 years ago
Read 2 more answers
Write a program that demonstrates how various exceptions are caught with catch(Exception exception). This time define classes Ex
SOVA2 [1]

Answer:

Sorry mate I tried but I got it wrong!

Explanation:

Sorry again

6 0
3 years ago
The ___ provide(s) shortcuts to commonly used elements
Genrish500 [490]

The quick access toolbar provides shortcuts to commonly used elements.

The Home Tab brings you to the home page and the File Tab allows you to save the file or print it.

I hope this helps! :)

7 0
3 years ago
Read 2 more answers
LIst types of computer process ?
vagabundo [1.1K]

Answer:

Transaction Processing.

Distributed Processing.

Real-time Processing.

Batch Processing.

Multiprocessing.

4 0
2 years ago
Other questions:
  • You visit two websites about inventions of the 21st century. One has .edu in the URL address and the other has .com. Which site
    6·2 answers
  • Which of these networks is primarily for posting and viewing photos?
    8·2 answers
  • Which of these items will least likely increase pressure in the evaporative emission control system?
    9·1 answer
  • Write a program that accepts a time as an hour and minute. Add 15 minutes to the time
    14·1 answer
  • Can include the 5-tuple information, which is the source and destination ip addresses, source and destination ports, protocols i
    9·1 answer
  • Use the_____icon to insert a new record.<br><br> A. *<br> B. X<br> C. =<br> D. &amp;
    11·1 answer
  • In case of a suspected data breach, what course of action should a chief information security officer (CISO) take
    9·1 answer
  • I need some questions and answers in spreadsheet​
    15·1 answer
  • Carmen has met new people online that she enjoys talking to. One of these people has asked her to meet at the park in person and
    15·1 answer
  • A manager suspects that one of his team members has been fraudulently accessing confidential and sensitive information and breac
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!