Question
need help finishing this assignment. can anyone help me with the bold part of the following list? I have everything else done I just need
need help finishing this assignment. can anyone help me with the bold part of the following list? I have everything else done I just need to complete the A3CircleLL class part. thanks in advance!
this list shows what needs to be done. Thanks in advance!
1. Complete A3DoubleLL.swap(int index) method - 3pts
2. Complete A3CircleLL.playGame(int players, int passes) method - 1.5pts
3. Complete A3CircleLL.addPlayers(int players) method - 1pts
4. Complete A3CircleLL.passPotato(int passes) method - 1.5pts
5. Complete A3Queue.enqueue(E val) method - 1pts
6. Complete A3Queue.dequeue() method - 1.5pts
7. Complete A3Queue.peek() method - 1.5pts
A3Driver.java
public class A3Driver {
public static void main(String[] args) {
A3DoubleLL
for(int i = 1; i < 10; i++) {
list.add(i);
}
String forwardBefore = "1,2,3,4,5,6,7,8,9,";
String reverseBefore = "9,8,7,6,5,4,3,2,1,";
String forwardAfter = "1,2,3,4,6,5,7,8,9,";
String reverseAfter = "9,8,7,5,6,4,3,2,1,";
System.out.println("Before Swap");
System.out.println(list.printList().equals(forwardBefore));
System.out.println(list.printListRev().equals(reverseBefore));
list.swap(4);
System.out.println("After Swap");
System.out.println(list.printList().equals(forwardAfter));
System.out.println(list.printListRev().equals(reverseAfter));
System.out.println();
System.out.println("Hot Potatoe");
String correctRemovalOrder = "4,3,5,2,1";
A3CircleLL hotPotato = new A3CircleLL();
System.out.println(hotPotato.playGame(5, 3).equals(correctRemovalOrder));
System.out.println();
System.out.println("Queue");
A3Queue
Integer var1 = 5;
Integer var2 = 20;
Integer var3 = 15;
queue.enqueue(5);
queue.enqueue(20);
queue.enqueue(15);
System.out.println(var1.equals(queue.peek()));
System.out.println(var1.equals(queue.dequeue()));
queue.enqueue(25);
System.out.println(var2.equals(queue.dequeue()));
System.out.println(var3.equals(queue.dequeue()));
}
}
A3Queue.java
/*
* Complete the enqueue(E val) method
* Complete the dequeue() method
* Complete the peek() method
* No other methods/variables should be added/modified
*/
public class A3Queue
/*
* Grading:
* Correctly adds an item to the queue - 1pt
*/
public void enqueue(E val) {
/*
* Add a node to the list
*/
}
/*
* Grading:
* Correctly removes an item from the queue - 1pt
* Handles special cases - 0.5pt
*/
public E dequeue() {
/*
* Remove a node from the list and return it
*/
return null;
}
/*
* Grading:
* Correctly shows an item from the queue - 1pt
* Handles special cases - 0.5pt
*/
public E peek() {
/*
* Show a node from the list
*/
return null;
}
private Node front, end;
private int length;
public A3Queue() {
front = end = null;
length = 0;
}
private class Node {
E value;
Node next, prev;
public Node(E v) {
value = v;
next = prev = null;
}
}
}
A3DoubleLL.java
/*
* Complete the swap(int index) method
* No other methods/variables should be added/modified
*/
public class A3DoubleLL
/*
* Grading:
* Swapped nodes without modifying values - 2pt
* Works for all special cases - 1pt
*/
public void swap(int index) {
//swap the nodes at index and index+1
//change the next/prev connections, do not modify the values
//do not use delete/remove/insert/add to help with this process
//make sure to account for all special cases
}
private Node start, end;
private int count;
public A3DoubleLL() {
start = end = null;
count = 0;
}
public String printList() {
String output = "";
Node current = start;
while(current != null) {
output += current.value + ",";
current = current.next;
}
return output;
}
public String printListRev() {
String output = "";
Node current = end;
while(current != null) {
output += current.value + ",";
current = current.prev;
}
return output;
}
public void add(E val) {
Node newItem = new Node(val);
if(start == null) {
start = newItem;
end = start;
count = 1;
} else {
end.next = newItem;
newItem.prev = end;
end = newItem;
count++;
}
}
public void insert(E val, int index) {
if(index < 0) {//fix invalid index
index = 0;
}
if(index >= count) {//goes in last position
this.add(val);
} else {
Node newItem = new Node(val);
if(index == 0) {//goes in first position
newItem.next = start;
start.prev = newItem;
start = newItem;
} else {//goes in middle
Node current = start;
for(int i = 1; i < index; i++) {
current = current.next;
}
newItem.next = current.next;
newItem.prev = current;
current.next.prev = newItem;
current.next = newItem;
}
count++;
}
}
public void delete(int index) {
if(index >= 0 && index < count) {//valid index
if(index == 0) {//remove first
start = start.next;
if(start != null) {//as long as there was an item next in list
start.prev = null;
} else {//if only item was removed
end = null;
}
} else if(index == count-1) {//remove last item
end = end.prev;
end.next = null;
} else {//remove middle item
Node current = start;
for(int i = 1; i < index; i++) {
current = current.next;
}
current.next = current.next.next;
current.next.prev = current;
}
count--;
}
}
public E get(int index) {
if(index >= 0 && index < count) {//valid index
Node current = start;
for(int i = 0; i < index; i++) {
current = current.next;
}
return current.value;
}
return null;
}
public String toString() {
return this.printList();
}
private class Node {
E value;
Node next, prev;
public Node(E v) {
value = v;
next = prev = null;
}
}
}
A3CircleLL.java
/*
* Complete the playGame(int players, int passes) method
* Complete the addPlayers(int players) method
* Complete the passPotatoe(int passes) method
* No other methods/variables should be added/modified
*/
public class A3CircleLL {
/*
* Grading:
* Correctly uses helpers to play game - 1pt
* Prints correct winner when game is complete - 0.5pt
*/
public String playGame(int players, int passes) {
/*
* Use the helper methods addPlayers and passPotatoe to play the game
* Continue passing the potato until only 1 player remains
* Print the winning players number
*
* For players = 5 and passes = 3, the winner should be 1. Players should be removed in this order:
* - 4, 3, 5, 2
*/
String removalOrder = "";
return removalOrder;
}
/*
* Grading:
* Correctly creates circular linked list of size amount - 1pt
*/
private void addPlayers(int amount) {
/*
* Set up this method to create a Node for each player
* The value of each Node, should be the player number, starting at 1
* For example, if the amount is 5, there should be Nodes 1-5
* Node 1 should always be set as the start
* Make list circular by connecting the last player Node to the first
*/
}
/*
* Grading:
* Correctly removes the player the number of passes away from the start - 1pt
* Correctly changes the start to the player after the one being removed - 0.5pt
*/
private int passPotato(int passes) {
/*
* Set up this method to play a single round of the game
* Move through the list the number of passes from the start
* Remove the player/Node at this position
* Set the start equal to the player/Node after this position
* Do not play a round if there is one 1 player remaining
* Print the player number that was removed and the player with the potato
*/
int removedPlayerNumber = -1;
return removedPlayerNumber;
}
private Node start;
private int count;
public A3CircleLL() {
start = null;
count = 0;
}
public String printList() {
String output = "";
if(start != null) {
Node current = start;
do {
output += current.value + ",";
current = current.next;
}while(current != start);
}
return output;
}
public String toString() {
return this.printList();
}
private class Node {
Integer value;
Node next;
public Node(Integer v) {
value = v;
next = null;
}
}
}
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