Create a program named queue.java that implements a queue of monster class eleme
ID: 665404 • Letter: C
Question
Create a program named queue.java that implements a queue of monster class elements. The class is below:
class MonsterInfo
{
private String Name;
private String Species;
private int HitPoints;
// add two more of your own attributes
// add a constructor with parameters for the attributes
// add get and set methods to access all attributes
}
Main program methods should include Enqueue, Dequeue, Peek, and IsEmpty.
The main program should begin by adding at least 4 monsters to the queue, then have the following menu options:
Welcome to the Monster Queue!
1 = Dequeue the monster at the front of the queue
2 = Enqueue a new monster
3 = List all the Monsters in the Queue
4 = Run for the Exit!
Your Choice:
1
You just dequeued an Orc named Dork with 100 hit points.
Explanation / Answer
Java code:
MonsterInfo.java
class MonsterInfo
{
//MonsterInfo class
private String Name;
private String Species;
private int HitPoints;
//constructor with parameters for the attributes
MonsterInfo(String name,String species,int points)
{
Name=name;
Species=species;
HitPoints=points;
}
void setName(String name)
{
Name= name;
}
void setSpecies(String species)
{
Species=species;
}
void setHitPoints(int points)
{
HitPoints = points;
}
String getName()
{
return Name;
}
String getSpecies()
{
return Species;
}
int getHitPoints()
{
return HitPoints;
}
}
Queue.java:
import java.util.LinkedList;
import java.util.Scanner;
class Queue {
//class with main method
private static LinkedList<MonsterInfo> Queue = new LinkedList<MonsterInfo>();
public static void main(String[] args)
{
String name,species;
int hitPoints;
MonsterInfo monster;
Enqueue(new MonsterInfo("Dork","Orc",100));
Enqueue(new MonsterInfo("Hork","Orc",80));
Enqueue(new MonsterInfo("Sork","Orc",80));
Enqueue(new MonsterInfo("Pork","Orc",80));
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Monster Queue! "+
"1 = Dequeue the monster at the front of the queue "+
"2 = Enqueue a new monster "+
"3 = List all the Monsters in the Queue "+
"4 = Run for the Exit!");
while(true)
{
System.out.println("Your Choice");
int choice=sc.nextInt();
switch(choice)
{
case 1:Dequeue();
break;
case 2: System.out.println("Enter the name of the monster:");
name=sc.next();
System.out.println("Enter the species name of the monster:");
species=sc.next();
System.out.println("Enter the species name of the monster:");
hitPoints=sc.nextInt();
monster =new MonsterInfo(name,species,hitPoints);
Queue.addLast(monster);
Enqueue(monster);
break;
case 3: ListAll();
break;
case 4:System.exit(0);
}
}
}
public static void Enqueue(MonsterInfo monster) {
Queue.addLast(monster);
}
public static void Dequeue() {
MonsterInfo temp=Queue.get(0);
Queue.poll();
System.out.println("You just dequeued an "+temp.getSpecies()+" named "
+temp.getName()+" with "+temp.getHitPoints()+" hit points.");
}
public static void ListAll() {
for(int i=0;i<Queue.size();i++)
{
MonsterInfo monster=Queue.get(i);
System.out.println("Species :"+monster.getSpecies()+"; Name :"
+monster.getName()+"; HitPoints :"+monster.getHitPoints()+"");
}
}
public static boolean IsEmpty() {
return Queue.isEmpty();
}
public static MonsterInfo Peek() {
return Queue.get(0);
}
}
Output:
Welcome to the Monster Queue!
1 = Dequeue the monster at the front of the queue
2 = Enqueue a new monster
3 = List all the Monsters in the Queue
4 = Run for the Exit!
Your Choice
3
Species :Orc; Name :Dork; HitPoints :100
Species :Orc; Name :Hork; HitPoints :80
Species :Orc; Name :Sork; HitPoints :80
Species :Orc; Name :Pork; HitPoints :80
Your Choice
1
You just dequeued an Orc named Dork with 100 hit points.
Your Choice
3
Species :Orc; Name :Hork; HitPoints :80
Species :Orc; Name :Sork; HitPoints :80
Species :Orc; Name :Pork; HitPoints :80
Your Choice
2
Enter the name of the monster:
Temp
Enter the species name of the monster:
TempSpecies
Enter the species name of the monster:
110
Your Choice
3
Species :Orc; Name :Hork; HitPoints :80
Species :Orc; Name :Sork; HitPoints :80
Species :Orc; Name :Pork; HitPoints :80
Species :TempSpecies; Name :Temp; HitPoints :110
Species :TempSpecies; Name :Temp; HitPoints :110
Your Choice
4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.