Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description : In this homework you will get some practice creating a JUnit 5 test suite for the classes in project #1. You will also

Description: In this homework you will get some practice creating a JUnit 5 test suite for the classes in project #1. You will also add functionality for the enhanced for-each loop seen in class.

Implementation Details:

Your task is to fully test the GenericStack and GenericQueue classes as well as the iterator. Keep in mind, there could be errors in the code provided. You will need to create two additional Junit5 test classes in their own files in the same directory as ListTest.java. Call them QueueTest.java and IteratorIterableTest.java. You will continue to test the GenericStack in ListTest.java. You are required to use a minimum of 3 annotations and 4 assertions in your unit tests. You must test the classes and iterator in a logical, step by step, fashion. Additional to the test cases provided, A minimum of 10 test cases per class and 10 for the iterator. You will probably need more to fully test this program. Each class and the iterator should be tested in their own file.

Enhanced For-each loop:

Your list library is almost complete except it does not allow the user to do: list.forEach(e -> System.out.println(e)); Your going to add the functionality for an enhanced for-each loop now. You will implement the Iterable interface in the GenericList class. Remember, you can implement as many interfaces as you want in a class. Just add them as a comma separated list after the implements key word. You will need to implement one method from the Iterable interface (check documentation to see what it is); the definition will look a lot like the method you already implemented for the CreateIterator interface. You will write test cases for an enhanced for-each loop in the IteratorIterableTest.java file. Yes, theses can count towards your minimum of 10.

GenericList:

image text in transcribedimage text in transcribed

GenericQueue:

image text in transcribed

CreateIterator:

import java.util.Iterator; public interface CreateIterator { public Iterator createIterator(); } 

GLIterator:

image text in transcribed

GLProject:

image text in transcribed

ListJava:

image text in transcribed

import java.util.ArrayList; import java.util.Iterator; abstract class Genericlist implements CreateIterator{ //A linked list, could change to another data structure private Node head; private int length; public abstract void add(I data); public void setLength(boolean how) { if (how) { ++length; else { --length; public int getLength() { return length; } public Node getHead() { return head; } public void setHead (Node head) { this.head = head; } public I delete() { if (head == null) { return null; I obj = head.data; head = head.next; return obj; public void print() { if (head == null) { System.out.println("Empty List!"); } else { Node temp = head; while (temp != null) { System.out.println(temp.data); temp = temp.next; public ArrayList dumpList() { ArrayList list = new ArrayListO; if(head == null) { System.out.println("EmptyList: returning empty list"); return list; else { while(head != null) { list.add(head.data); head = head.next; return list; public Iterator createIterator({ return new GLIterator(head); class Node { I data; Node next; import java.util. Iterator; public class GenericQueue extends GenericList { AntMaven GenericQueuell data){ add(data); this.setLength(true); public void enqueue(data) { add(data); this.setLength(true); public I daqueue() { if(this.getHead() != null) { this.setlength(false); return delete(); @Override public void add date) [ // TODO Auto-generated wethod stub Node iter = new NodeO; if(this.getHead() -- null) [ Node tomp - new Node0; temp.data = data; ten.next = this.getHead(): this.setllead(temp); hd - ww e n; iter = getHead(); while(iter.next != null) { iter - iter-next; Node newlode = new Node(); rellode.date - date; newlode.next - null; iter.next = newlode; import java.util.Iterator; AntMaven public class GLIterator implements Iterator{ private GenericList.Node theList; GLIterator (GenericList.Node list) { this.thelist = list; } @Override public boolean hasNext() { // TODO Auto-generated method stub if(thelist == null) { return false; else { return true; @Override public I next() { // TODO Auto-generated method stub I val = theList.data; theList = theList.next; return val; Ant mport java.util.ArrayList; mport java.util.Iterator; ublic class GLProject { E Maven public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("hello generic lists"); GenericStack i = new GenericStack( data: 20); i.push(data: 30); i.push( data: 40); i.push( data: 50); i.print(); Iterator it = i.createIterator; while(it.hasNext() { System.out.println(it.next()); ArrayList theList = i.dumpList(); for(Integer ival : theList) { System.out.println("from AL: " + ival); i.print(); ArrayList theList2 = i.dumpList(); for(Integer ival : theList2) { System.out.println("from AL2: " + ival); import static org.junit.jupiter.api.Assertions. *; AntMaven import org.junit.jupiter.api. BeforeEach; import org.junit.jupiter.api. Test; import org.junit.jupiter.params. ParameterizedTest, import org.junit.jupiter.params.provider.ValueSource; class ListTest { GenericStack 5; @BeforeEach void init() { s = new Genericstack( data: 300); } @Test void testInitys { assertEquals( expected: "Genericstack", s.getClass().getName(), message: "init failed on GS"); } @Test void testInitNode { assertEquals( expected: "Genericlist$Node", s.getHead().getClass().getName(), message: "node not init"); } @Test void testNodeval() { assertEquals( expected: 300, s.pop(), message: "value wrong in Node: not 200"); } @Test void testEmptyList() { s.pop(); assertNull(s.getHead()); @ParameterizedTest @valueSource(chars = {'a', 'b', 'a', 'b']), void isLetterA( char input) { assertEquals( expected: 'a', input, message: "not letter 'a'); } 1 Event Log UTF-8 Tab* 1 1:8 LF import java.util.ArrayList; import java.util.Iterator; abstract class Genericlist implements CreateIterator{ //A linked list, could change to another data structure private Node head; private int length; public abstract void add(I data); public void setLength(boolean how) { if (how) { ++length; else { --length; public int getLength() { return length; } public Node getHead() { return head; } public void setHead (Node head) { this.head = head; } public I delete() { if (head == null) { return null; I obj = head.data; head = head.next; return obj; public void print() { if (head == null) { System.out.println("Empty List!"); } else { Node temp = head; while (temp != null) { System.out.println(temp.data); temp = temp.next; public ArrayList dumpList() { ArrayList list = new ArrayListO; if(head == null) { System.out.println("EmptyList: returning empty list"); return list; else { while(head != null) { list.add(head.data); head = head.next; return list; public Iterator createIterator({ return new GLIterator(head); class Node { I data; Node next; import java.util. Iterator; public class GenericQueue extends GenericList { AntMaven GenericQueuell data){ add(data); this.setLength(true); public void enqueue(data) { add(data); this.setLength(true); public I daqueue() { if(this.getHead() != null) { this.setlength(false); return delete(); @Override public void add date) [ // TODO Auto-generated wethod stub Node iter = new NodeO; if(this.getHead() -- null) [ Node tomp - new Node0; temp.data = data; ten.next = this.getHead(): this.setllead(temp); hd - ww e n; iter = getHead(); while(iter.next != null) { iter - iter-next; Node newlode = new Node(); rellode.date - date; newlode.next - null; iter.next = newlode; import java.util.Iterator; AntMaven public class GLIterator implements Iterator{ private GenericList.Node theList; GLIterator (GenericList.Node list) { this.thelist = list; } @Override public boolean hasNext() { // TODO Auto-generated method stub if(thelist == null) { return false; else { return true; @Override public I next() { // TODO Auto-generated method stub I val = theList.data; theList = theList.next; return val; Ant mport java.util.ArrayList; mport java.util.Iterator; ublic class GLProject { E Maven public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("hello generic lists"); GenericStack i = new GenericStack( data: 20); i.push(data: 30); i.push( data: 40); i.push( data: 50); i.print(); Iterator it = i.createIterator; while(it.hasNext() { System.out.println(it.next()); ArrayList theList = i.dumpList(); for(Integer ival : theList) { System.out.println("from AL: " + ival); i.print(); ArrayList theList2 = i.dumpList(); for(Integer ival : theList2) { System.out.println("from AL2: " + ival); import static org.junit.jupiter.api.Assertions. *; AntMaven import org.junit.jupiter.api. BeforeEach; import org.junit.jupiter.api. Test; import org.junit.jupiter.params. ParameterizedTest, import org.junit.jupiter.params.provider.ValueSource; class ListTest { GenericStack 5; @BeforeEach void init() { s = new Genericstack( data: 300); } @Test void testInitys { assertEquals( expected: "Genericstack", s.getClass().getName(), message: "init failed on GS"); } @Test void testInitNode { assertEquals( expected: "Genericlist$Node", s.getHead().getClass().getName(), message: "node not init"); } @Test void testNodeval() { assertEquals( expected: 300, s.pop(), message: "value wrong in Node: not 200"); } @Test void testEmptyList() { s.pop(); assertNull(s.getHead()); @ParameterizedTest @valueSource(chars = {'a', 'b', 'a', 'b']), void isLetterA( char input) { assertEquals( expected: 'a', input, message: "not letter 'a'); } 1 Event Log UTF-8 Tab* 1 1:8 LF

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_2

Step: 3

blur-text-image_3

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

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions