Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 5 Please use JAVA In this lab assignment, we will compare our sorted doubly-linked list class, from lab 04, and develop an array-based alternative.

Lab 5 Please use JAVA

In this lab assignment, we will compare our sorted doubly-linked list class, from lab 04, and develop an array-based alternative. These two implementations will then be compared for performance. Instructions In the previous lab assignment, you were tasked with writing a sorted doubly-linked list class to store a number of Warrior instances, keeping them sorted by the speed stat (highest to lowest). Implement the same behaviour in a class, called SortedArray, and add operating counting to both classes. Operation counting should focus only on assignment operations, and in both cases merely updating a public instance variable will be sufficient. The driver code that follows can be used to compare your two implementations: import java.util.Random; public class ComparisonDriver { public static void main(String[] args) { SortedDoublyLinkedList list = new SortedDoublyLinkedList(); SortedArray array = new SortedArray(); // insert 100 elements into both lists (randomly generated) for (int i = 0; i < 100; i++) { Warrior newWarrior = new Warrior("Generic", genStat(50), genStat(50), genStat(200)); list.insert(newWarrior); array.insert(newWarrior); } // compare the number of assignment operations: System.out.println("Linked List assignmentCount: " + list.assignmentCount); System.out.println("Sorted Array assignmentCount: " + array.assignmentCount); } public static int genStat(int max) { // randomly choose an integer between 1 and max Random rand = new Random(); return rand.nextInt(max) + 1; } } Run the driver code multiple times, and summarize the results. Try to come up with an explanation for why the test produced the values that it did.

Attached below are the code from lab 4. Bolded are different classes used to run sorted doubly linked list class

//sorted doubly linked list

public class SortedDoublyLinkedList implements LinkedList {

// Inner class

private class Node {

private Warrior warrior;

private Node prev;

private Node next;

//warrior code

public Node(Warrior warrior) {

this.warrior = warrior;}

// set warrior

public void setWarrior(Warrior warrior) {

this.warrior = warrior;

}

//get warrior

public Warrior getWarrior() {

return warrior; }

// returns prev

public Node getPrev() {

return prev; }

//next code

public Node getNext() {

return next;

}

//set previus

public void setPrev(Node prev) {

this.prev = prev;

}

//sets node

public void setNext(Node next) {

this.next = next;

}

//returns string

public String toString() {

return warrior.toString();

}

}

// Instance variables for SortedDoublyLinkedList

private Node head;

private int inc;

//default constructor

public SortedDoublyLinkedList() {

this.head = null;

this.inc = 0;

}

//add new warrior to list

public void insert(Warrior warrior) {

// Create new node

Node newNode = new Node(warrior);

// Last node

Node lastNode = null;

// Check if list is null

if (this.inc == 0) {

// Set newNode as the head

this.head = newNode;

}

else {

// If list is not null

boolean inputed = false;

// Get the head node

Node node = this.head;

while (!inputed && (node != null)) {

// Compare warrior's speed with the warrior at node

int res = warrior.getSpeed() - node.getWarrior().getSpeed();

if (res >= 0) { // If warrior's speed is greater than or equal to the warrior's speed at node

// Check if node is the head node

if (node.prev == null) {

// Set new node as head

this.head = newNode;

} else {

// Set newNode.prev as node.prev

newNode.prev = node.prev;

// Set node.prev.next as newNode

node.prev.next = newNode;

}

// Set node.prev as newNode

node.prev = newNode;

// Set newNode.next as node

newNode.next = node;

inputed = true;

}

// Set last node

if (node.next == null)

lastNode = node;

// Go to next node

node = node.next;

}

// Check if node was not added

if (!inputed) {

// Set lastNode.next as newNode

lastNode.next = newNode;

// Set newNode.prev as lastNode

newNode.prev = lastNode;

}

}

// Increment count

this.inc += 1;

}

//Returns the warriors contained in this doubly linked list sepearated by space.

public String toString() {

StringBuffer space = new StringBuffer("[ ");

// Check if list is not null

if (this.inc > 0) {

// Get the head node

Node node = this.head;

while (node != null) {

space.append(node + " ");

// Go to next node

node = node.getNext();

}

}

// Append closing bracket

space.append("]");

return space.toString();

}

}

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);

//interface public interface LinkedList { void insert(Warrior warrior); String toString(); }

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 + ")"; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

8. Explain the contact hypothesis.

Answered: 1 week ago