Question
need help with my homework. Please read the rubric before coding. The questions 8 and 12 that explain which method needs to be made. Each
need help with my homework. Please read the rubric before coding. The questions 8 and 12 that explain which method needs to be made. Each method of those exercises must be included in the LinkedIntList.java file that I pasted bellow. It also needs a test client program which will run and test the code. I need help with completing those methods and putting them in the LinkedIntList.java file. I also need help with the test client code. Thank you so much for your help! PLEASE Attach the client test code and put comments to help me better understand.
Please do exercises 8 and 12 on page 1023 (eBook). Please note that the use of "list" in the exercise specification is to be understood to refer to a LinkedIntList, unless it specifically says otherwise. Also, be sure to read the exercise 12 specification very carefully and fully; from its beginning to its end is the specification. Use the files LinkedIntList.java, Intlist.java, ListNode.java, and ListClient.java files in in Module Chapter 16 as your starting point. Include a test client that exercises the methods you have added to the LinkedIntList class in your submission; ListClient.java is the beginnings of one; change it as required to exercise your methods.
8.Add method called switchPairs that switches the order of values in the list in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. If the list contains an odd number of values, the final element is not moved. For example, if the list initially stores [10, 25, 31, 47, 52, 68, 77], your method should switch the first pair (10 and 25), the second pair (31 and 47), and the third pair (52 and 68) to yield [25, 10, 47, 31, 68, 52, 77].
12.Add a method called split that rearranges the elements of a list so that all of the negative values appear before all of the nonnegatives. For example, suppose a variable list stores the values [8, 7, -4, 19, 0, 43, -8, -7, 2]. The call of list.split(); should rearrange the list to put the negatives first: [-4, -8, -7, 8, 7, 19, 0, 43, 2]. It doesn't matter what order the numbers are in, only that the negatives appear before the nonnegatives, so this is only one possible solution. You must solve the problem by rearranging the links of the list, not by swapping data values or creating new nodes. You also may not use auxiliary structures like arrays or ArrayLists to solve this problem.
This the LinkedIntList.java
// Class LinkedIntList can be used to store a list of integers.
public class LinkedIntList implements IntList {
private ListNode front; // first value in the list
// post: constructs an empty list
public LinkedIntList() {
front = null;
}
// post: returns the current number of elements in the list
public int size() {
int count = 0;
ListNode current = front;
while (current != null) {
current = current.next;
count++;
}
return count;
}
// pre : 0 <= index < size()
// post: returns the integer at the given index in the list
public int get(int index) {
return nodeAt(index).data;
}
// post: creates a comma-separated, bracketed version of the list
public String toString() {
if (front == null) {
return "[]";
} else {
String result = "[" + front.data;
ListNode current = front.next;
while (current != null) {
result += ", " + current.data;
current = current.next;
}
result += "]";
return result;
}
}
// post : returns the position of the first occurrence of the given
// value (-1 if not found)
public int indexOf(int value) {
int index = 0;
ListNode current = front;
while (current != null) {
if (current.data == value) {
return index;
}
index++;
current = current.next;
}
return -1;
}
// post: appends the given value to the end of the list
public void add(int value) {
if (front == null) {
front = new ListNode(value);
} else {
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
}
// pre: 0 <= index <= size()
// post: inserts the given value at the given index
public void add(int index, int value) {
if (index == 0) {
front = new ListNode(value, front);
} else {
ListNode current = nodeAt(index - 1);
current.next = new ListNode(value, current.next);
}
}
// pre : 0 <= index < size()
// post: removes value at the given index
public void remove(int index) {
if (index == 0) {
front = front.next;
} else {
ListNode current = nodeAt(index - 1);
current.next = current.next.next;
}
}
// pre : 0 <= i < size()
// post: returns a reference to the node at the given index
private ListNode nodeAt(int index) {
ListNode current = front;
for (int i = 0; i < index; i++) {
current = current.next;
}
return current;
}
}
ListNode.java
// ListNode is a class for storing a single node of a linked
// list. This node class is for a list of integer values.
public class ListNode {
public int data; // data stored in this node
public ListNode next; // link to next node in the list
// post: constructs a node with given data and given link
public ListNode(int data, ListNode next) {
this.data = data;
this.next = next;
}
// post: constructs a node with given data and null link
public ListNode(int data) {
this(data, null);
}
// post: constructs a node with data 0 and null link
public ListNode() {
this(0, null);
}
}
IntList.java
/*
This is the interface definition for the IntList data type
*/
public interface IntList {
public int size();
public int get(int index);
public String toString();
public int indexOf(int value);
public void add(int value);
public void add(int index, int value);
public void remove(int index);
}
ListClient.java
public class ListClient {
public static void main(String[] args) {
LinkedIntList list1 = new LinkedIntList();
processList(list1);
LinkedIntList list2 = new LinkedIntList();
processList(list2);
}
public static void processList(IntList list) {
list.add(18);
list.add(27);
list.add(93);
System.out.println(list);
list.remove(1);
System.out.println(list);
}
}
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