public class Hero { //data members private String name; private int nemesis; pri
ID: 3690346 • Letter: P
Question
public class Hero {
//data members
private String name;
private int nemesis;
private double travelTime;
private Hero next;
private Hero prev;
/**
* Default constructor
*/
public Hero() {
name = null;
nemesis = -1;
travelTime = 0.0;
next = null;
prev = null;
}
/**
* Parameterized constructor
*
* @param name
* @param nemesis
* @param travelTime
* @param next
* @param prev
*/
public Hero(String name, int nemesis, double travelTime, Hero next, Hero prev) {
this.name = name;
this.nemesis = nemesis;
this.travelTime = travelTime;
this.next = next;
this.prev = prev;
}
//member functions
/**
* returns name
*
* @return
*/
public String getName() {
return name;
}
/**
* sets name
*
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* gets nemesis
*
* @return
*/
public int getNemesis() {
return nemesis;
}
/**
* set nemesis
*
* @param nemesis
*/
public void setNemesis(int nemesis) {
this.nemesis = nemesis;
}
/**
* returns travel time
*
* @return
*/
public double getTravelTime() {
return travelTime;
}
/**
* sets travel time
*
* @param travelTime
*/
public void setTravelTime(double travelTime) {
this.travelTime = travelTime;
}
/**
* returns next hero in list
*
* @return
*/
public Hero getNext() {
return next;
}
/**
* sets next hero in list
*
* @param next
*/
public void setNext(Hero next) {
this.next = next;
}
/**
* returns previous hero
*
* @return
*/
public Hero getPrev() {
return prev;
}
/**
* sets previous hero in the list
*
* @param prev
*/
public void setPrev(Hero prev) {
this.prev = prev;
}
/**
* prints containing hero's name, nemesis status, and travel time
*/
void info() {
System.out.println("Name:" + name);
System.out.println("Nemesis status:" + nemesis);
System.out.println("Travel time:" + travelTime);
}
}
===========================================================
public class List {
private Hero firstHero; //first hero in the list
private Hero lastHero; //last hero in the list
private int length; //length of list
/**
* Default constructor
*/
public List() {
firstHero = null;
lastHero = null;
length = 0;
}
/**
* returns if list is empty.
*
* @return
*/
public boolean isEmpty() {
return (firstHero == null);
}
/**
* returns length of list
*
* @return
*/
public int getLength() {
return length;
}
/**
* Adds new hero to the end of list.
*
* @param newHero
*/
void enqueue(Hero newHero) {
if (isEmpty()) {
firstHero = newHero;
lastHero = newHero;
} else {
newHero.setPrev(lastHero);
lastHero.setNext(newHero);
lastHero = newHero;
}
length++;
}
/**
* removes and returns a hero from the front of list.
*
* @return
*/
Hero dequeue() {
Hero hero = null;
if (!isEmpty()) {
hero = firstHero;
firstHero.getNext().setPrev(firstHero.getPrev());
firstHero = firstHero.getNext();
length--;
} else {
System.err.println("Underflow!");
}
return hero;
}
/**
* removes heros from list till it is empty.
*/
void dequeueAll() {
while (!isEmpty()) {
Hero hero = dequeue();
System.out.println("Removed Hero's info:");
hero.info();
}
System.out.println("All heros removed. List empty now.");
}
/**
* Adds new hero to the top (front) of list.
*
* @param newHero
*/
void push(Hero newHero) {
if (isEmpty()) {
firstHero = newHero;
lastHero = newHero;
} else {
newHero.setNext(firstHero);
firstHero.setPrev(newHero);
firstHero = newHero;
}
length++;
}
/**
* removes and returns a hero from top (front) of list.
*
* @return
*/
Hero pop() {
Hero hero = null;
if (!isEmpty()) {
hero = firstHero;
firstHero.getNext().setPrev(firstHero.getPrev());
firstHero = firstHero.getNext();
length--;
} else {
System.err.println("Underflow!");
}
return hero;
}
/**
* pops heros from list till it is empty.
*/
void popAll() {
while (!isEmpty()) {
Hero hero = pop();
System.out.println("Popped Hero's info:");
hero.info();
}
System.out.println("All heros popped. List empty now.");
}
/**
* traverses the list and prints info about all heros in the list.
*/
void printList() {
Hero hero = firstHero;
while (hero != null) {
System.out.println("->");
hero.info();
hero = hero.getNext();
}
}
}
===========================================================
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Hero hero1 = new Hero();
Hero hero2 = new Hero();
hero1.setName("SuperMan");
hero1.setNemesis(2);
hero1.setTravelTime(40);
hero2.setName("BatMan");
hero2.setNemesis(3);
hero2.setTravelTime(24);
List heroList = new List();
heroList.enqueue(hero1);
heroList.enqueue(hero2);
heroList.printList();
}
SAMPLE OUTPUT:
Explanation / Answer
public class ListQueue { public static void main(String[] args){ Queue myQueue; Scanner sc = new Scanner(System.in); String input; int choice = 99; do{ System.out.println("================"); System.out.println("Queue Operations Menu"); System.out.println("================"); System.out.println("1,Enquene"); System.out.println("2,Dequeue"); System.out.println("3,Empty?"); System.out.println("4,Count?"); System.out.println("5,View Queue"); System.out.println("0, Quit "); System.out.println("Enter Choice:"); try{ choice = sc.nextInt(); switch(choice){ case 1: System.out.println("Please enter name: "); input = sc.next(); myQueue.enqueue(input); System.out.println(input + "is successful queued"); break; case 2: if(myQueue.isEmpty()){ } break; case 3: if(myQueue.isEmpty()){ System.out.println("Queue is empty"); }else{ System.out.println("Queue is not empty"); } break; case 4: System.out.println("Number of people is " + "the queue" + myQueue.size()); break; case 5: if(!myQueue.isEmpty()) myQueue.viewQueue(); else System.out.println("Queue is empty"); break; case 0: System.out.println("Good-bye"); break; default: System.out.println("Invalid choice"); } } catch(InputMismatchException e){ System.out.println("Please enter 1-5, 0 to quit"); sc.nextLine(); } }while(choice != 0); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.