Код: Выделить всё
import java.util.*;
public class QueueHowell
{
//no code here
}
class Song
{
//instance ariables
private String song;
private String singer;
//add two instnace variables
//constructor, modify to include the new instance variables
public Song(String song, String singer)
{
this.song = song;
this.singer = singer;
}
public String getSong()
{
return song;
}
public String getSinger()
{
return singer;
}
//toString
public String toString()// must be modified
{
return song + " " + singer+ "\n**************\n";
}
public boolean equals(Song other)//must be modified
{
return this.song.equalsIgnoreCase(other.song ) && this.song.equalsIgnoreCase(other.song);
}
/*compares the two songs based on the singer,if same singer then compares the two songs based on the name of the songs name*/
public int compareTo(Song other)
{
if (this.singer.compareTo(other.singer) == 0 )
return this.song.compareTo(other.song);
else
return this.singer.compareTo(other.singer);
}
/*
Must add a new compareTo method that is based on the instance ariables you added
*/
}
/*
Queue class using an Array. this class will create a queue of songs
Using ArrayList or LinkList will get zero points*/
class Queue
{
//instance variable
private Song[] list;
private int count = 0;
//constructor
public Queue( )
{
list = new Song[20];
}
/*adding a song to the end of the queue. Adding an element at the end of the queue. The variable count is the index of the first
avaiable spot at the end of the array
make sure to increment the count after you place a song at the end of the list*/
public void enqueue(Song s)
{
boolean done = false;
while(list[count] == null && !done && count < list.length - 1){
list[count] = s;
count++;
done = true;
}
}
/*removing a song from the front of the queue. removing the element at the index 0
Song s = list[0]
list[0] = null
after setting the list[0] to null, all the elemnets must be shifted to the left. shift method must be called
after ethe shift method is called decrement the count by 1
*/
public Song dequeue()
{
Song s = list[0];
list[0] = null;
shift();
count--;
return s;
}
/*
this method shifts the elements of the array to the left. This method is called in the dequeue method.
a for loop is needed to shift the elements. We have to make sure that all the null values are at the end of the queue
for example if the array is :
a b c d null null null
null b c d null null null//making the first element null
b c d null null null null //shifting all the elements to the left
*/
public void shift()
{
for (int i = 0; i < list.length; i++) {
if (list[i] != null && list[i-1] == null && i != 0){
list[i-1] = list[i];
list[i] = null;
}
}
}
/*
This method plays the song in the queue. playing the song in the front of the queue, removing the song at index 0
then moving to the next song util no more songs left in the queue. No for loop or a counter for the index can be sued
*/
public void play()
{
//declare a stack called s
Stack s = new Stack();
//declare a Scanner object
Scanner input = new Scanner(System.in);
//declare a boolean variable called done and set it to false
boolean done = false;
//while done is false
while (!done)
{
try
{
//dequeue a song , store it in a variable called front of type Song, dequeue method must be called
Song front = dequeue();
//if dequeued song is null throw an exception
if (front == null){
throw new NullPointerException();
}
//push the variable front to the stack that you declared above
s.push(front);
//display the content of the front
System.out.println("Press any key to continue");
//flush the buffer
}
catch(Exception e)
{
//set done to true
done = true;
}
}
//call the restor method and pass the s to it
restore(s);
}
/*
this method gets the name of a singer and finds all the song with the given singer. return the list of the songs
Since the list is a queue, you can only see the song at the index 0. This method must be implemented using the queue concept
and not looping through the list.
This method gets the song at the index 0, then pushes it to an extra storage of stack so that we can restore the queue using the stack
Must use LinkList for the method below otherwise zero points will be given for the whole code
*/
public LinkedList getSingerSongs(String singer)
{
//declare an LinkList of Song with the name songs, to hold all the songs with the given siger
LinkedList songs = new LinkedList();
//declare a stack named s
Stack s = new Stack();
//declare a boolen variable called done and set it to false
boolean done = false;
//while done is false
while (!done)
{
try
{
//dequeue a song and store it in a variable of type Song called front(method dequeue must be called
Song front = dequeue();
// if the song is null throw an exception
if (front == null){
throw new NullPointerException();
}
//push the front variable to the stack
s.push(front);
//if the singer's name in the variable front is the same as the singer paramter
// add front to the LinkedList of songs that you declared
songs.add(front);
}
catch(Exception e)
{
//set done to true
done = true;
}
}
//call the restore method and pass the stack s to it
restore(s);
return songs;// Change this to return the arraylist songs
}
/*
this method goes through the queue, removing the element at index 0, push it to the stack, and also concatinate it
to the String s that will hold the list of all the songs.
Must use try/catch. the code is similar to the method above.
also don't forget to call the restore method at the very end, before the return statement.
Make sure to use try/catch
Keep dequeuing from the array until a null is dequeued at that point an Exception must be thrown
*/
public String toString()
{
boolean done = false;
String s = "";
Stack songs = new Stack();
while (!done){
try {
Song front = dequeue();
if (front == null){
throw new NullPointerException();
}
songs.push(front);
s = s + front;
}
catch(Exception e){
done = true;
}
}
restore(songs);
return s;
}
/*
This method gets a stack of songs and restor the original queue in the original order.
*/
public void restore(Stack s)
{
count = 0;
boolean b = false;
while(!b)
{
try
{
if (s.isEmpty())
throw new Exception();
Song ss = (Song)s.pop();
list[count] = ss;
count++;
}
catch(Exception c)
{
b = true;
}
}
}
/*
this method reverses the order of the songs in the queue. if the songs are a b c d then method reverse will change the queue to
d c b a
*/
public void reverseOrder()
{
//declare a boolean called done = false
boolean done = false;
//decalre a stack called s
Stack s = new Stack();
//while done is false
while (done == false)
{
try
{
//dequeue the song and store it in a variable of type song called e
Song e = dequeue();
//if song is null throw an exception
if(e == null){
throw new NullPointerException();
}
//push e to the stack s
s.push(e);
}
catch(Exception e)
{
//set done to true
done = true;
shift();
}
}
//set done to false
done = false;
//while done is false(this loop is building the queue in the reverse order using the stack s
while(!done)
{
try
{
//pop an element from the stack s , store it in a variable of type Object called o
Object o = s.pop();
//if o is null
if (o == null){
//throw an exception
throw new NullPointerException();
}
// enqueue the object o by calling the enqueue method
enqueue((Song)o);
}
catch(Exception e)
{
//set done to true
done = true;
}
}
}
/*This method gets the name of a singer and returns the percentage of the songs in the queue from the given singer*/
public String getPercentage(String singer)
{
//declare a variable of type int to hold the number of the songs by the given singer
double sum = 0;
//declare a variable called count to hold the total number of the songs in the queue
int count = 0; //holds the number of the songs in the list
//declare a boolean called done set it to false
boolean done = false;
//declare a stack called s
Stack s = new Stack();
//while done is false
while(!done)
{
try
{
//dequeue a song by calling the dequeue method and store it in a variable of type song called song
Song song = dequeue();
//if song is null
if(song == null)
{
//throw an exception
throw new NullPointerException();
}
//incremnet count
count++;
//push song to the stack
s.push(song);
//if song.getSinger is the same as singer
if (song.getSinger() == singer){
//incremnet sum
sum++;
}
}
catch(Exception e) // goes here when there is no song left in the queueue
{
//set done to true
done = true;
}
}
//call the restore method and pass s to it
restore(s);
return sum + " out of " + count + " is by the singer " + singer;
}
/*
rebuild the queueue from the give stack which contains all the songs
*/
public void preserve(Queue q)
{
boolean b = false;
while(!b)
{
try
{
this.enqueue(q.dequeue());
}
catch (Exception e)
{
b = true;
}
}
}
}
/*Do not remove this driver
You must run your code with the given Driver.This is how I test your code.
*/
class Driver
{
public static void main(String[] args)
{
Queue m = new Queue();
m.enqueue(new Song ("Riders in the Sky", "Monroe"));
m.enqueue(new Song("Catch My Breath","Clarkson"));
m.enqueue(new Song("All American Girl", "Underwood"));
m.enqueue(new Song("Anyway","McBride"));
m.enqueue(new Song("Before He Cheats", "Underwood"));
m.enqueue(new Song("Born Free", "Monroe"));
m.enqueue(new Song("people Like Us","Clarkson"));
m.enqueue(new Song("Give Her That", "Underwood"));
m.enqueue(new Song("So Small", "Underwood"));
m.enqueue(new Song("Stronger","Clarkson"));
m.enqueue(new Song("Walk Away", "Monroe"));
m.enqueue(new Song("Independence Day","McBride"));
System.out.println("here is the list of your songs\n__________________________");
System.out.println(m);
System.out.println("The queue is : " + m);
m.reverseOrder();
System.out.println("The queue in the reverse order is: \n"+ m);
m.reverseOrder();
System.out.println("\n\nSongs by Underwood: \n");
System.out.println("\n\n" + m.getPercentage("Underwood"));
System.out.println("\n\n" + m.getPercentage("Clarkson")+"\n\n");
System.out.println("Now playing your songs\n");
m.play();
}
}Подробнее здесь: https://stackoverflow.com/questions/796 ... method-its