Question
For this assignment you need to do a JAVA program to implement queue data structure using linked list for this assignment. Summary Your job is
For this assignment you need to do a JAVA program to implement queue data structure using linked list for this assignment.
Summary
Your job is to implement a FriendshipQueue, using linked nodes. A friendship queue is like a normal queue, except that when a new object joins a queue, it looks to see if it has any friends in the queue. If it has friends in the queue, it jumps in the queue with its friends, instead of joining the tail of the queue. There is a limit though, a person in line can only have two friends join them. When a set of friends gets to the front of the queue, they are processed one at a time, in the order they joined the line.
Instructions
Part 1: Create a TernaryNode
Test your TernaryNode
Part 2: Create a FriendshipQueue
Note that while you must implement the methods above, you are welcome to add as many private helper methods to the FriendshipQueue class as you need. While this looks similar to a regular queue, a few things are different:
In the enqueue() method, you must search the queue to see if the element has any friends (the compareTo method will return 0 if the items are friends). If a friend element is found in the queue, you must check to make sure it hasnt already got two other friends in line with it. If not, you add this element so that it stands in queue with its friend. If the friend already has two friends in line with it, continue searching until youve got to the front of the queue. If you search the queue and cant find any friends to join, enqueue as usual.
In the dequeue() method, if the element you are dequeing has friends standing in line with it, you must move the friends up towards the queue. Watch how you maintain your state variables here.
The size() method counts the length of the queue, ignoring friends. The totalSize() method returns the length of the queue, including the friends.
Here are diagrams that show how the friendship queue should operate:
after dequeue() operation (size is 5, totalSize is 7):
after a queue() operation adding a dark blue element:
after enqueuing another dark blue element:
Requirements
You must fulfill the following:
- Implementation: you need to implement the TernaryNode class and the FriendshipQueue class. These two classes will be graded by web-cat.
- Write Junit tests for your TernaryNode and your FriendshipQueue classes. These will be graded by the TA.
Files needed
ClubLineUp:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ds_assignment2;
import java.util.logging.Level; import java.util.logging.Logger;
/** * Club lineup simulation for ITIS 2214 Assignment 2 * Version 1 * @date Feb 20, 2017 * @author clatulip */ public class ClubLineup { private FriendshipQueue
public ClubLineup() { setupNames(); }
public void runSimulation() { lineup = new FriendshipQueue
private void setupNames() { names = new String[MAX_NAMES]; names[0] = "Abdul"; names[1] = "Jen"; names[2] = "Chris"; names[3] = "Myron"; names[4] = "Rutuja"; names[5] = "Harrison"; names[6] = "Carlos"; names[7] = "Elizabeth"; names[8] = "Jean-Paul"; names[9] = "Richard"; names[10] = "Sofia"; names[11] = "Kyle"; names[12] = "Tyler"; names[13] = "Mohammed"; names[14] = "Avery"; names[15] = "Miriam"; names[16] = "Mariah"; names[17] = "Tristan"; names[18] = "Jacob"; names[19] = "Brandon"; // setup dorms dorms = new String[MAX_DORMS]; dorms[0] = "Levine Hall"; dorms[1] = "Belk Hall"; dorms[2] = "Laurel Hall"; dorms[3] = "Pine Hall"; dorms[4] = "Oak Hall"; }
private Friend createNewFriend() { int dNum = (int)(Math.random()*(MAX_DORMS-1)); int nNum = (int)(Math.random()*(MAX_NAMES-1)); Friend f = new Friend(names[nNum], dorms[dNum], id); id++; return f; } }
DS_Assignment2:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ds_assignment2;
/** * * @author clatulip */ public class DS_Assignment2 {
/** * @param args the command line arguments */ public static void main(String[] args) { ClubLineup cl = new ClubLineup(); cl.runSimulation(); } }
EmptyCollectionException:
package ds_assignment2;
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/** * An Empty Collection Exception class * Prints out what type of collection is empty * For use in ITCS 2214 Data Structures & Algorithms * UNC Charlotte, 2016 * @author clatulip */ public class EmptyCollectionException extends Exception {
public EmptyCollectionException(String collection) { super("The " + collection + " is empty."); } }
Friend:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package ds_assignment2;
/** * Friend class for use in Data Structures Assignment 2 * @author clatulip */ public class Friend implements Comparable { private String name; private String dormName; private int id;
public Friend(String name, String dormName, int id) { this.name = name; this.dormName = dormName; this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDormName() { return dormName; }
public void setDormName(String dormName) { this.dormName = dormName; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
@Override public int compareTo(Object o) { // string comparison based on dormName field Friend f = (Friend)o; return f.getDormName().compareTo(this.getDormName()); }
@Override public String toString() { return "Friend{" + "name=" + name + ", dormName=" + dormName + ", id=" + id + '}'; }
}
FriendshipQueueADT:
package ds_assignment2;
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/** * An interface for a friendship queue * For use in ITCS 2214 Data Structures & Algorithms * UNC Charlotte, 2016 * @author clatulip */ public interface FriendshipQueueADT
NonComparableElementException:
package ds_assignment2;
/** * Exception class for when trying to compare collection elements that * haven't implemented Comparable (and don't have a compareTo(..) method) * @author clatulip */ public class NonComparableElementException extends Exception {
public NonComparableElementException(String collection) { super("The element passed in to " + collection + " does not implement Comparable."); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started