Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem1 is writing methods and Junits for those methods and Problem 2 is implementing those methods under the given code. 1. (30 points) Write JUnit

Problem1 is writing methods and Junits for those methods and Problem 2 is implementing those methods under the given code. image text in transcribed
image text in transcribed
image text in transcribed
1. (30 points) Write JUnit test cases for the following list methods: (a) A method most common which assumes the list is organized so that duplicate values are together. (For example, it could be sorted.) The method returns the value that appears the most times. When more than one value appears the most number of times, the first occurring such value is returned. If the list is empty, the method throws a NoSuchElementException. (b) A method doubleIt that takes no arguments and adds a duplicate of each list element after its original occurrence. (It's actually creating duplicate references. For example, calling this method on the list ("a", "b", "c", "d") changes it to ("a", "a", "b", "b", "c", "c", "d", "d"). (c) A method moveToTail that takes an index. It should move the value at that index to the "tail" of the list, i.e. so that it's the last element. For example, calling the method with argument 2 on the list ("a", "b", "c", "d") changes the list to ("a", "b", "d", "c"). If the index is invalid, the method should throw an IndexOutOf BoundsException. You can build off the given code, other code we/you have written this term, and code from the text. I suggest that you use the code from HW 3, either the original given code or your HW submission. Be sure to use these test cases on the implementations you create for the problems below. 2. (30 points) Implement each of the methods above for an array-based list. 3. (30 points) Implement each of the methods from problem 1 for a linked-memory list implementation. public class MyArrayList implements MyList= num)) throw new IndexOutOfBoundsException(); return vals[index]; public class MyLinkedListT> implements MyList private Node head; public class Node public T value; public Node next; public Node(t value, Node next) { this.value-value; this.next next; public MyLinkedlist() { head - null; public void addFront (T newitem) { Node newlode - new Node(newitem, head); head - nowode: public boolean contains(TS) //returns whether the list contains Node curr - head; while(curr ! - null) { if(curr.value.equals(s)) return true; curr - curr.next; return false; public int size() { Node curr - head; int count - @; while(curr - null) { count : curr - curr.next; return count: public boolean remove(t val) { //removes first occurrence of val from the list //returns whether it was there Node curr - head; //current Node we're looking at Node prev - null; // Node before that one while(curr ! - null) { if(curr.value.equals(val)) { if(curr -- head) head - curr.next; //special case for removing head else prev.next - curr.next; /remove curr's node return true; prev - curr: curr - curr.next; return false; //didn't find val @Override public void add(t todd) if(head = null) addFront(toAdd): return; Node curr - head; while(curr.next !- null) //advance curr to point at first node curr - curr.next; curr.next - new Mode(todd, null); adverride public T Rat(int index) // TODO Auto-generated method stub return null; 1. (30 points) Write JUnit test cases for the following list methods: (a) A method most common which assumes the list is organized so that duplicate values are together. (For example, it could be sorted.) The method returns the value that appears the most times. When more than one value appears the most number of times, the first occurring such value is returned. If the list is empty, the method throws a NoSuchElementException. (b) A method doubleIt that takes no arguments and adds a duplicate of each list element after its original occurrence. (It's actually creating duplicate references. For example, calling this method on the list ("a", "b", "c", "d") changes it to ("a", "a", "b", "b", "c", "c", "d", "d"). (c) A method moveToTail that takes an index. It should move the value at that index to the "tail" of the list, i.e. so that it's the last element. For example, calling the method with argument 2 on the list ("a", "b", "c", "d") changes the list to ("a", "b", "d", "c"). If the index is invalid, the method should throw an IndexOutOf BoundsException. You can build off the given code, other code we/you have written this term, and code from the text. I suggest that you use the code from HW 3, either the original given code or your HW submission. Be sure to use these test cases on the implementations you create for the problems below. 2. (30 points) Implement each of the methods above for an array-based list. 3. (30 points) Implement each of the methods from problem 1 for a linked-memory list implementation. public class MyArrayList implements MyList= num)) throw new IndexOutOfBoundsException(); return vals[index]; public class MyLinkedListT> implements MyList private Node head; public class Node public T value; public Node next; public Node(t value, Node next) { this.value-value; this.next next; public MyLinkedlist() { head - null; public void addFront (T newitem) { Node newlode - new Node(newitem, head); head - nowode: public boolean contains(TS) //returns whether the list contains Node curr - head; while(curr ! - null) { if(curr.value.equals(s)) return true; curr - curr.next; return false; public int size() { Node curr - head; int count - @; while(curr - null) { count : curr - curr.next; return count: public boolean remove(t val) { //removes first occurrence of val from the list //returns whether it was there Node curr - head; //current Node we're looking at Node prev - null; // Node before that one while(curr ! - null) { if(curr.value.equals(val)) { if(curr -- head) head - curr.next; //special case for removing head else prev.next - curr.next; /remove curr's node return true; prev - curr: curr - curr.next; return false; //didn't find val @Override public void add(t todd) if(head = null) addFront(toAdd): return; Node curr - head; while(curr.next !- null) //advance curr to point at first node curr - curr.next; curr.next - new Mode(todd, null); adverride public T Rat(int index) // TODO Auto-generated method stub return null

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions