Question: SOLUTION NEEDED IN JAVA PLEASE n this lab assignment, we will create a sorted doubly-linked list class, and use that class to implement the outline

SOLUTION NEEDED IN JAVA PLEASE

n this lab assignment, we will create a sorted doubly-linked list class, and use that class to implement the outline of a speed-priority battle system.

Instructions

Our battle game needs to support warriors of different speeds properly. A priority queue seems like an appropriate data structure for the task, but you haven't covered that yet in your computer science courses. Let's try to implement a basic doubly-linked list that implements a priority system by sorting the elements that are inserted.

The driver code that follows can be used to test your linked list:

public class LinkedListDriver {

public static void main(String[] args) {

LinkedList list = new SortedDoublyLinkedList();

System.out.println(list);

Warrior krogg = new Warrior("Krogg", 30, 50, 200);

list.insert(krogg); System.out.println(list);

Warrior gurkh = new Warrior("Gurkh", 40, 45, 180);

list.insert(gurkh);

System.out.println(list);

Warrior brynn = new Warrior("Brynn", 45, 40, 190);

list.insert(brynn); System.out.println(list);

Warrior dolf = new Warrior("Dolf", 20, 65, 210);

list.insert(dolf); System.out.println(list);

Warrior zuni = new Warrior("Zuni", 50, 35, 170);

list.insert(zuni);

System.out.println(list);

}

}

The Warrior class has also been included, to make your job easier:

public class Warrior {

private String name; private int speed; private int strength; private int hp;

public Warrior(String name, int speed, int str, int hp) {

this.name = name; this.speed = speed; this.strength = str; this.hp = hp;

}

public String getName() {

return this.name;

}

public int getSpeed() {

return this.speed;

}

public int getStrength() {

return this.strength;

}

public int getHp() {

return this.hp;

}

public String toString() {

return this.name + "(" + this.speed + ")"; }

}

The class you need to implement will implement the following interface:

interface LinkedList {

void insert(Warrior warrior); String toString();

}

Implement the SortedDoublyLinkedList class such that the driver class' code compiles and runs with the following output:

[ Zuni(50) Brynn(45) Gurkh(40) Krogg(30) Dolf(20) ]

Your class will have an inner class, called Node, which will store the individual elements in your list. Note: The warriors are separated by spaces in the toString() output to make your job easier. Of course, your program should work for any driver code.

Extra Challenge

Note: This part of the lab assignment is optional. A bonus of 0.5 marks will be given to anyone who completes this correctly. However, it is recommended that you complete the base assignment, first! Nonfunctional code will not qualify for the bonus marks, nor will they earn you a perfect lab mark. Implement the SortedDoublyLinkedList class using generics so that any object can be inserted into the list. You will need to modify the driver class and the interface slightly to make this work.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!