These programs use the Java \"Stack\" class (API) to create stacks. You will be
ID: 3534165 • Letter: T
Question
These programs use the Java "Stack" class (API) to create stacks. 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 (which will be entered later as either 0.99 or 1.49). It should contain 2 constructor methods, all the necessary set and get methods, and a "toString" method that prints the values of the 3 attributes.
Step 2: Create a class called "SimpleSongStack" which will be used for creating a stack of Song objects.
Step 3: Create a class called "UseSimpleSongStack" which will utilize the "SimpleSongStack" class to create a stack of songs. This program will initialize an "ITunesGiftCard" variable to $15.00. Then it will set up a while loop to request a Song object's data, create a Song object from the data, and push the Song object onto the stack. The loop should track the money spent on each song ordered (either 0.99 or 1.49) and subtract it from the gift card total. When the money has been spent (no more songs can be purchased), 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.
======================================================================================
I will post what I have done so far but it is not working correctly.
========================================================================================
public class Song
{
private String title;
private String artist;
private double price;
public Song(String title, String artist, double price) {
super();
this.title = title;
this.artist = artist;
this.price = price;
}
public Song() {
this.title = "";
this.artist = "";
this.price = 0;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "Song [title=" + title + ", artist=" + artist + ", price="
+ price + "]";
}
}
import java.util.Stack;
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]);
}
}
}
=======================================================================================
import java.util.Scanner;
public class UseSimpleSongStack
{
private double ITunesGiftCard;
private SimpleSongStack simpleSongStack;
public UseSimpleSongStack()
{
ITunesGiftCard = 15;
simpleSongStack = new SimpleSongStack(5);
Scanner keyboard = new Scanner(System.in);
while(ITunesGiftCard >= 0.9)
{
System.out.println("Enter Title");
String title = keyboard.next();
System.out.println("Enter Artist");
String artist = keyboard.next();
System.out.println("Enter price");
double price = keyboard.nextDouble();
if (ITunesGiftCard >= price)
{
ITunesGiftCard -= price;
simpleSongStack.getSongsStack().push(new Song(title, artist, price));
}
else {
System.out.println("Not enough balance enter a different song cost lesser than the " + price);
}
}
System.out.println("Songs recently purchased");
while( !simpleSongStack.getSongsStack().empty())
{
System.out.println(simpleSongsStack.getSongStack().pop());
}
}
public static void main(String[] args) {
new UseSimpleSongStack();
}
}
=======================================================================================
This is unfinished and would appreciate if someone could edit the above code so it may work correctly as per the 3 questions.
Explanation / Answer
========================================================================================
public class Song
{
private String title;
private String artist;
private double price;
public Song(String title, String artist, double price) {
super();
this.title = title;
this.artist = artist;
this.price = price;
}
public Song() {
this.title = "";
this.artist = "";
this.price = 0;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "Song [title=" + title + ", artist=" + artist + ", price="
+ price + "]";
}
}
import java.util.Stack;
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]);
}
}
}
=======================================================================================
import java.util.Scanner;
public class UseSimpleSongStack
{
private double ITunesGiftCard;
private SimpleSongStack simpleSongStack;
public UseSimpleSongStack() throws Exception
{
ITunesGiftCard = 15;
simpleSongStack = new SimpleSongStack(5);
Scanner keyboard = new Scanner(System.in);
while(ITunesGiftCard >= 0.9)
{
System.out.println("Enter Title");
String title = keyboard.next();
System.out.println("Enter Artist");
String artist = keyboard.next();
System.out.println("Enter price");
double price = keyboard.nextDouble();
if (ITunesGiftCard >= price)
{
ITunesGiftCard -= price;
simpleSongStack.push(new Song(title, artist, price));
}
else {
System.out.println("Not enough balance enter a different song cost lesser than the " + price);
}
}
System.out.println("Songs recently purchased");
while( !simpleSongStack.isEmpty())
{
System.out.println(simpleSongStack.pop());
}
}
public static void main(String[] args) throws Exception {
new UseSimpleSongStack();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.