Need Java help creating a class CD?

Im just asking for help to get started on how to make a CLASS representing a cd containing strings for titles, and artists aswell as an array of strings to hold up tracktitles. this class must implement methods for:

-its constructor which will initialize title and artist

-AddTrackString tracktitle to add tracktitle

-Display(), which uses println to display the artist, title, and each numbered track on a seperate line.

basically if someone could help me by offering a simple outline of where methods, fields, data, constructors go it would really help me alot by learning Java. Thanks!

Answer
🥇 Favorite Answer

  • I’ll show you the order of where elements usually go. Below that I’ll put some **** and there it will be the program you are asking about, so you can try it on your own and see if you did it right. I’ll also comment my code so you can understand it. (P.S. I’ll assume this is a beginners java class so I used arrays, but it almost sounds like it would be better using arraylists, since it may not have tracks, right now I just didn’t print out the ones the are null). Message me if you still don’t get it or want to use arraylists instead.

    //Defines class

    public class name {

    //Variables are defined here

    variables

    //Constructor. You can initialize variables here

    constructor(){

    }

    //Put both of your methods after constructor

    methods(){

    }

    //Main to run your program

    public static void main(String[] args) {

    }

    }

    *************MY PROGRAM**************

    See also  how to close a user form with a command button?

    public class CD {

    //Create variables

    public String artist;

    public String title;

    public String[] trackListing = new String[];

    int current = ;//Need this to put songs in correct spot in array

    //Constructor. Initializes the artist and title variables based on parameters ()

    public CD(String artist, String title){

    this.artist = artist;

    this.title = title;

    }

    //Method that adds a track to the array based on whats in parameters

    public void addTrackListing(String trackTitle){

    trackListing[current] = trackTitle;

    current++;

    }

    //Prints the artist and title, then loops through array and prints out song titles

    public void display(){

    System.out.println(“Artist: ” + artist);

    System.out.println(“Title: ” + title);

    for(int i = ; i < trackListing.length; i++){

    if(trackListing[i] != null){

    System.out.println(“Track ” + (i+) + “: ” + trackListing[i]);

    }

    }

    }

    public static void main(String[] args) {

    //Create cd object

    CD cd = new CD(“Coldplay”,”Viva La Vida”);

    //Add tracks

    cd.addTrackListing(“Life In Technicolor”);

    cd.addTrackListing(“Cemeteries of London”);

    //Display info

    cd.display();

    }

    }

  • Leave a Comment