Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Java Song class

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Exclamation Java Song class

    Instructions:
    Now it's time to code. You will be creating a program that implements a stack of Songs.
    Step 1: Create a class called Song that has 3 attributes-title, artist, and price. It should contain 2 constructor methods (a no-arg and full parameter), all the necessary set and get methods, and a "toString" method that prints the values of the 3 attributes. A starter file has been provided below- download it and finish the code for it.
    Step 2: Create a class called "SimpleSongStack" which will be used for creating/manipulating a stack of Song objects (use the file "SimpleStack" as a guide).
    Step 3: Create a class called "UseSimpleSongStack" which will utilize the "SimpleSongStack" class to create a stack of songs. This program should initialize an "ITunesGiftCard" variable to $15.00. Then it should set up a while loop to request a Song object's data from the user (title, artist, and price), create a Song object with the data, and push the newly created Song object onto the stack. The loop should track the money spent on each song ordered (assume song prices are either 0.99, 1.29, or 1.49) by subtracting it from the gift card total. When no more songs can be purchased (ITunesGiftCard < 0.99), the loop should terminate. After the loop finishes, the program should report the "Songs Recently Purchased:". To do this, pop each song object from the song stack and call it's "toString" method.

    File 1: Song.java
    public class Song
    {
    private String title;
    private String artist;
    private double price;

    public Song(String Title, String Artist, double Price)
    {
    this.title = Title;
    this.artist = Artist;
    this.price = Price;
    }

    public Song()
    {
    title = null;
    artist = null;
    price = 0;
    }

    public String getTitle()
    {
    return this.title;
    }

    public void setTitle(String newTitle)
    {
    this.title = newTitle;
    }

    public String getArtist()
    {
    return this.artist;
    }

    public void setArtist(String newArtist)
    {
    this.artist = newArtist;
    }

    public Double getPrice()
    {
    return this.price;
    }

    public void setPrice(Double newPrice)
    {
    this.price = newPrice;
    }

    public String toString()
    {
    return title + " by " + artist + "\n Price: " + price + "\n";
    }
    }

    File 2: SimpleSongStack.java
    public class SimpleSongStack
    {

    private Song[] stack;
    private int top;

    public SimpleSongStack(int capacity)
    {
    stack = new Song[capacity];
    top = 0;
    }

    public boolean isEmpty()
    {
    return (top == 0);
    }

    public boolean isFull()
    {
    return (top == stack.length);
    }

    public void push(Song item) throws Exception
    {
    if (isFull())
    {
    throw new Exception("stack overflow");
    }
    else
    {
    stack[top++] = item;
    }
    }

    public Song pop() throws Exception
    {
    if (isEmpty())
    {
    throw new Exception("stack underflow");
    }
    else
    {
    return (stack[--top]);
    }
    }
    }

    File 3: UsesSimpleSongStack.java
    import java.util.Scanner;

    public class UseSimpleSongStack
    {
    public static void main (String args[]) throws Exception
    {
    SimpleStack myStack = new SimpleStack(16);
    int numberOfSongItems;
    Song item;
    String itemName;
    String itemArtist;
    double itemPrice;
    double itunesGiftCard = 15.00;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the number of song items you would like to purchase: ");
    numberOfSongItems = keyboard.nextInt();
    keyboard.nextLine();


    for (int i=0; i< numberOfSongItems; i++)
    {
    System.out.println("\nEnter the name of the song: ");
    itemName = keyboard.nextLine();
    System.out.println("\nEnter the artist of the song:");
    itemArtist = keyboard.nextLine();
    System.out.println("Enter the price for this item: ");
    itemPrice = keyboard.nextInt();
    keyboard.nextLine();

    item = new Song(itemName, itemArtist, itemPrice);
    System.out.println("...pushing the " + item.getDescription() + " song on the stack...");
    myStack.push(item) ;
    }

    System.out.println("\nPopping Song Items off the stack:");

    for (int i=0; i< numberOfSongItems; i++)
    {
    item = myStack.pop();
    System.out.println("...the item " + item.getDescription() + " was popped off the stack...");
    }
    }
    }

    HELP:
    I am getting an error message in the File 3 UseSimpleSongStack.java and I dont know why or how. I have bolded the troubled parts.
    PLEASE HELP. IM DESPERATE


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Song class

    I am getting an error message
    Copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    brandalicious78 (April 22nd, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Song class

    4 errors found:
    File: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java [line: 32]
    Error: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java:32: cannot find symbol
    symbol : method getDescription()
    location: class Song
    File: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java [line: 34]
    Error: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java:34: push(InventoryItem) in SimpleStack cannot be applied to (Song)
    File: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java [line: 41]
    Error: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java:41: incompatible types
    found : InventoryItem
    required: Song
    File: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java [line: 42]
    Error: /Users/brandifelzmann/Downloads/stacks/UseSimpleSongStack.java:42: cannot find symbol
    symbol : method getDescription()
    location: class Song

    Please edit your post and wrap your code with code tags:
    <File 1: Song.java
    public class Song
    {
    private String title;
    private String artist;
    private double price;
     
    public Song(String Title, String Artist, double Price)
    {
    this.title = Title;
    this.artist = Artist;
    this.price = Price;
    }
     
    public Song()
    {
    title = null;
    artist = null;
    price = 0;
    }
     
    public String getTitle()
    {
    return this.title;
    }
     
    public void setTitle(String newTitle)
    {
    this.title = newTitle;
    }
     
    public String getArtist()
    {
    return this.artist;
    }
     
    public void setArtist(String newArtist)
    {
    this.artist = newArtist;
    }
     
    public Double getPrice()
    {
    return this.price;
    }
     
    public void setPrice(Double newPrice)
    {
    this.price = newPrice;
    }
     
    public String toString()
    {
    return title + " by " + artist + "\n Price: " + price + "\n";
    }
    }
     
    File 2: SimpleSongStack.java
    public class SimpleSongStack
    {
     
    private Song[] stack; 
    private int top; 
     
    public SimpleSongStack(int capacity) 
    {
    stack = new Song[capacity];
    top = 0; 
    }
     
    public boolean isEmpty() 
    {
    return (top == 0);
    }
     
    public boolean isFull() 
    {
    return (top == stack.length);
    }
     
    public void push(Song item) throws Exception 
    {
    if (isFull()) 
    {
    throw new Exception("stack overflow");
    } 
    else 
    {
    stack[top++] = item;
    }
    }
     
    public Song pop() throws Exception 
    {
    if (isEmpty()) 
    {
    throw new Exception("stack underflow");
    }
    else 
    {
    return (stack[--top]);
    }
    }
    }
     
    File 3: UsesSimpleSongStack.java
    import java.util.Scanner;
     
    public class UseSimpleSongStack
    {
    public static void main (String args[]) throws Exception
    {
    SimpleStack myStack = new SimpleStack(16);
    int numberOfSongItems;
    Song item;
    String itemName;
    String itemArtist;
    double itemPrice;
    double itunesGiftCard = 15.00;
    Scanner keyboard = new Scanner(System.in);
     
    System.out.println("Enter the number of song items you would like to purchase: ");
    numberOfSongItems = keyboard.nextInt();
    keyboard.nextLine();
     
     
    for (int i=0; i< numberOfSongItems; i++)
    {
    System.out.println("\nEnter the name of the song: ");
    itemName = keyboard.nextLine();
    System.out.println("\nEnter the artist of the song:");
    itemArtist = keyboard.nextLine();
    System.out.println("Enter the price for this item: ");
    itemPrice = keyboard.nextInt();
    keyboard.nextLine();
     
    item = new Song(itemName, itemArtist, itemPrice);
    System.out.println("...pushing the " + item.getDescription() + " song on the stack...");
    myStack.push(item) ;
    }
     
    System.out.println("\nPopping Song Items off the stack:");
     
    for (int i=0; i< numberOfSongItems; i++)
    {
    item = myStack.pop();
    System.out.println("...the item " + item.getDescription() + " was popped off the stack...");
    }
    }
    }
    to get highlighting and preserve formatting.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Song class

    cannot find symbol
    symbol : method getDescription()
    The compiler can not find a definition for the method named in the error message. Is it defined in the class that item references?

    incompatible types
    found : InventoryItem
    required: Song
    The code requires an object of type Song but the code has an object of type InventoryItem. Change the code so it uses an object of the correct type: Song.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    brandalicious78 (April 22nd, 2013)

  7. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Song class

    The code was originally written with the InventoryItem class and I changed it to Song because that is what is required. The code doesnt have anything left that says InventoryItem in it so I am confused still.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Song class

    Are you using an IDE that can have class files hidden away someplace?
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    brandalicious78 (April 22nd, 2013)

  10. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Song class

    I am not positive. I don't understand what IDE is. I am coding in Dr.Java if that helps?

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Song class

    Does DRJAVA hide any files? Can you easily see all the files used by a project? No parts are hidden.
    If you don't understand my answer, don't ignore it, ask a question.

  12. The Following User Says Thank You to Norm For This Useful Post:

    brandalicious78 (April 22nd, 2013)

  13. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Song class

    nothing is hidden. all the files need to be open for it to run i believe

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Song class

    Are ALL the files used by DRJAVA for a project in one folder where they can easily be seen?
    How many folders does DRJAVA use to hold the files for a project?
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    brandalicious78 (April 22nd, 2013)

  16. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java Song class

    I am not sure. I appreciate the help but I think I'm just gonna call it a day. Thank you though.

  17. #12
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    It looks like you might still be using the old SimpleStack class in your test file (file 3). Instead use the new SimpleSongStack you created.

    Rolf

Similar Threads

  1. Java and Flickr - Help with Inheritance between Main Class and sub-class
    By thientanchuong in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 14th, 2012, 07:29 PM
  2. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  3. create a test class (main method) to start(run) the class in Java
    By curious725 in forum Java Theory & Questions
    Replies: 5
    Last Post: August 1st, 2012, 03:21 AM
  4. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  5. How to take input dynamically and display?
    By abielm_007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2009, 05:51 PM