Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create the new class called Song.java using the given class diagram. There is no

ID: 3764183 • Letter: C

Question

Create the new class called

Song.java

using the given class diagram.

There is no default constructor, only the one sending in both title and artist.

toString

-

define it so that it prints in the form:

title

by

artist

2.

Write the code

for menu choice 2

to print the

array

to the screen.

Doing this

at the beginning allows you to test that the array has the correct values

whenever a change has been made to it.

You have been given the declarations for:

MAX_ARRAY_SIZE

FILEN

AME

count

(used to keep track of the number of elements currently in the array)

choice (used to capture the menu choice made by the user)

3

.

Write the prompt and read the menu choice from the user.

There are two of

these statements. One before the loop

and another at the bottom of the loop.

3

Run your program to test printing and reading the menu choice to be sure

this is working correctly.

4

.

Open the file, read the input data from the file, create a Song object for each

one, store the song object in the

array. When done, close the file.

Remember

to use a try/catch when attempting to open the file. You will also be keeping

track of how many Song objects are put into the array.

Test this using menu choice 2 to see if everything loaded correctly.

5

.

W

ri

te the code for the following

:

write the output to

SongData.txt

.

Use a try/catch for any

exceptions.

6

.

Write the code for menu choice 1.

Prompt the

user

,

r

ead the data from the keyboard and

create a new Song

object

.

Add the song to the array

o

Don't forget to add

to the count so you know how many items are in the

array

Add the song to the file

7

.

Close the output file.

8

.

Run the code. Add a song, then use menu choice 2 to see if it was added

correctly.

o

If both prompts print at the same time, you may have to

deal with the

newline character between reading choice and the title.

Explanation / Answer

Complete Program:

// File: Song.java
public class Song
{
private String title;
private String artist;

public Song(String aTitle, String aArtist)
{
  title = aTitle;
  artist = aArtist;
}

public String getTitle()
{
  return title;
}

public void setTitle(String aTitle)
{
  title = aTitle;
}


public String getArtist()
{
  return artist;
}

public void setArtist(String aArtist)
{
  artist = aArtist;
}

public String toString()
{
  return title + " by " + artist;
}
}

// File: CDDriver.java
import java.io.*;
import java.util.*;
public class CDDriver
{
public static void main(String[] args)
{
  final int MAX_ARRAY_SIZE = 50;
  final String FILENAME = "Collection.txt";
  
  Scanner keyboard = new Scanner(System.in);
  
  int count = 0;
  int choice = 0;
  
  Song[] songs = new Song[MAX_ARRAY_SIZE];
  
  String aTitle;
  String aArtist;
  
  try
  {
   Scanner infile = new Scanner(new File(FILENAME));
   
   while(infile.hasNextLine() && count < MAX_ARRAY_SIZE)
   {
    aTitle = infile.nextLine();
    aArtist = infile.nextLine();
    Song filesong = new Song(aTitle, aArtist);
    songs[count] = filesong;
    count++;
   }
   infile.close();
   
   PrintWriter outfile = new PrintWriter(new BufferedWriter(new FileWriter(FILENAME, true)));
   
   System.out.println();
   System.out.println("CD Collection Menu");
   System.out.println("1. Add a CD");
   System.out.println("2. Print List");
   System.out.println("3. Quit");
   System.out.print("Enter the menu choice: ");
   choice = Integer.parseInt(keyboard.nextLine());
   
   while(choice != 3)
   {
    switch(choice)
    {
     case 1:
      System.out.print("Enter the title: ");
      aTitle = keyboard.nextLine();
      System.out.print("Enter the artist: ");
      aArtist = keyboard.nextLine();
      
      Song usersong = new Song(aTitle, aArtist);
      
      songs[count] = usersong;
      count++;
      
      outfile.println(aTitle);
      outfile.println(aArtist);
      break;
      
     case 2:
      System.out.println(" Songs stored in the array:");
      for(int i = 0; i < count; i++)
       System.out.println(songs[i]);
      break;
      
     default:
      System.out.println("Invalid menu choice. Please try again.");
    }
    
    System.out.println();
    System.out.println("CD Collection Menu");
    System.out.println("1. Add a CD");
    System.out.println("2. Print List");
    System.out.println("3. Quit");
    System.out.print("Enter the menu choice: ");
    choice = Integer.parseInt(keyboard.nextLine());
   }
   outfile.close();
  }
  catch(FileNotFoundException e)
  {
   e.printStackTrace();
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
}
}

File at the beginning: (Assumed)
title 1
artist 1
title 2
artist 2
title 3
artist 3
title 4
artist 4
title 5
artist 5
title 6
artist 6
title 7
artist 3
title 8
artist 5


Sample Output on console:


CD Collection Menu
1. Add a CD
2. Print List
3. Quit
Enter the menu choice: 2

Songs stored in the array:
title 1 by artist 1
title 2 by artist 2
title 3 by artist 3
title 4 by artist 4
title 5 by artist 5
title 6 by artist 6
title 7 by artist 3
title 8 by artist 5

CD Collection Menu
1. Add a CD
2. Print List
3. Quit
Enter the menu choice: 1
Enter the title: title 9
Enter the artist: artist 2

CD Collection Menu
1. Add a CD
2. Print List
3. Quit
Enter the menu choice: 1
Enter the title: title 10
Enter the artist: artist 4

CD Collection Menu
1. Add a CD
2. Print List
3. Quit
Enter the menu choice: 2

Songs stored in the array:
title 1 by artist 1
title 2 by artist 2
title 3 by artist 3
title 4 by artist 4
title 5 by artist 5
title 6 by artist 6
title 7 by artist 3
title 8 by artist 5
title 9 by artist 2
title 10 by artist 4

CD Collection Menu
1. Add a CD
2. Print List
3. Quit
Enter the menu choice: 4
Invalid menu choice. Please try again.

CD Collection Menu
1. Add a CD
2. Print List
3. Quit
Enter the menu choice: 3


File at the end:
title 1
artist 1
title 2
artist 2
title 3
artist 3
title 4
artist 4
title 5
artist 5
title 6
artist 6
title 7
artist 3
title 8
artist 5
title 9
artist 2
title 10
artist 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote