Answer To: Ask the person who worked on my order 50013 if they are available to do this order. If not then any...
Abhishek answered on Mar 04 2021
51551 - Linked Bag/Screenshots/O1.png
51551 - Linked Bag/Problem/assignment_3/Assignment-03-Feb22.docx
Objective
In this assignment, you are asked to implement a bag collection using a linked list and the collection is to store only one type of Thing of your choice.
Important: you can NOT use the LinkedList class from the Java library, instead, you have to implement your own linked list as explained below.
Use assignment_2 and lecture_code_6 from the zip folder to finish assignment_3 with the classes already started
Requirements
In order to complete this project, you need to implement the following classes. The details of each class is explained below.
Thing: you must use the same Thing that you used for the Assignment 2. But make sure to look for the requirements below and change your previous implementation as necessary.
· ThingNode: a linked list node where the data part is of type Thing.
· ThingLinkedBag: a collection of Things in which the elements are stored in a linked list.
· ThinsLinkedBagIterator: an iterator class that is used to iterate over a linked list. Note that this is NOT an inner class.
· ThingLinkedBagDriver: a Java class that includes only a main method to test the functionality of your collection.
class Thing
Use the same Thing class that you used in Assignment 2 but make sure all the following requirements are met. Basically, your Thing class must have the following:
· has exactly 2 instance variables such that:
· one instance variable of type String and this variable will be used as a search key.
· one instance variable of type integer (i.e, int) and this variable will be used to find aggregate information about Things that are stored in a collection class as will be explained later.
· An implementation of toString method that returns a one-line representation of the
Thing.
· Implement equals methods to test the equality of two things.
· Implement Comparable interface and include compareTo method.
class ThingNode
This class represents a node in the linked list. The node class should include the following:
· two private instance variables:
· data of type Thing. It is required that the data part to be of a specific Thing type. Do not use a generic node.
· link of type ThingNode.
· a constructor that takes two input parameters and use them to initialize the two instance variables.
· The following instance methods which are the same as in the IntNode class that we discussed in class and also is discussed in the text book (Chapter 4, Section 4.2). You have to change the methods as appropriate to reflect your new type of data.
· getData, getLink, setData, setLink, addNodeAfter, removeNodeAfter.
· The following static methods that are the same as IntNode class. You have to change the methods as appropriate. o display, listPosition, listLength,and listSearch.
class ThingLinkedBag
The requirements of the ThingLinkedBag class are as follows:
· two instance variables:
· head of type ThingNode: this represent the head of the linked list that stores your collection.
· manyItems of type int: this represent the size of the linked list.
· Implement the following instance methods in the collection class. Note that all the method headers below are written in terms of Thing, however, your implementation should replace each Thing with your specific data type.
1. a constructor to create an empty linked list.
2. int size(): returns the number of nodes in the list.
3. void display(): displays the contents of the collection such that each element is displayed on one line. Note that this method displays the list on the screen and does NOT return a String representation of the list.
4. void add(Thing element): a method to add a Thing to the collection.
5. void add(int position, Thing element): a method to add an element at a specific position in the collection assuming the head node is at position 1. If the position is greater than the collection length, then the element is added as the last element in the collection. The method does not do anything if position is negative.
6. void addLast(Thing element): adding an element to be the last element in the linked list
7. boolean remove(Thing target): this method removes one occurrence from the target from the list if any. The method returns true if an item is removed and false otherwise.
8. boolean remove(int position): this method removes the element that is located at position position in the linked list where the head node is at position 1. The method returns true if an item is removed and false if no element is removed because position is negative or beyond the list length.
9. void removeLast(): this method removes the last node in the linked list.
10. int countRange(Thing start,Thing end): this method counts and returns how many element in the collection falls in the range between start and end inclusive. Note that things are compared using the compareTo method.
11. Thing grab(int position): returns the element of the node located at position position in the list or null if position is past the end of the list. Note that this method does not remove the element from the list.
12. int positionOf(Thing target): returns the position of the node that contains target assuming that the head node is at position 1. If the target is not found, the method return -1 and if there are more than one occurrence, then the method returns the position of the first occurrence.
13. void set(int position, Thing element): changes the element at position position with the input element. If position is negative or beyond the length of the list, then the method does not do anything.
14. int totalValue(): this method returns the sum of all the integer values of all things in the list (remember that it was required that each thing has an integer attribute).
15. ThingNode lessThan(Thing element): this method takes one Thing as input and returns an output a linked list that includes all elements that are less than or equal to the input element. Note that things are ordered based on the compareTo method.
16. Thing getMax(): this method returns the maximum Thing in the linked list where things are ordered based on the compareTo method.
class ThingLinkedBagIterator
This class and it is used to iterate over your linked collection. Note that this class should be an independent class and NOT an inner class. An implementation of the linked list iterator is discussed in class Lecture7's code that is posted on D2L. Basically, the header of your class should be as follows:
class ThingLinkedBagIterator implements Iterator
You have to change of the rest of iterator class as necessary to reflect your Thing data type.
Change your ThingLinkedBag class to implement Iterable and add the iterator method that returns an iterator of type ThingLinkedBagIterator.
class ThingLinkedBagDriver
Write a driver class to test ALL the methods that you implemented in the ThingNode class and the
ThingLinkedBag class.
51551 - Linked Bag/Problem/assignment_3/.~lock.Assignment-03-Feb22.docx#
,abhishek,abhishek-HP-Pavilion-15-Notebook-PC,04.03.2020 17:56,file:///home/abhishek/.config/libreoffice/4;
51551 - Linked Bag/Solution/Simple Code Files/assignment3/ThingNode.java
51551 - Linked Bag/Solution/Simple Code Files/assignment3/ThingNode.java
package assignment3;
public class ThingNode {
private Ring data;
private ThingNode link ;
public ThingNode(Ring data, ThingNode link) {
super();
this.data = data;
this.link = link;
}
public Ring getData() {
return data;
}
public void setData(Ring data) {
this.data = data;
}
public ThingNode getLink() {
return link;
}
public void setLink(ThingNode link) {
this.link = link;
}
public void addNodeAfter(Ring element){
this.link = new ThingNode(element,this.link);
}
public void removeNodeAfter(){
this.link = this.link.link;
}
public static int listLength(ThingNode head){
ThingNode cursor = head;
int answer = 0;
while (cursor != null){
answer++;
cursor = cursor.link;
}
return answer;
}
public static ThingNode listSearch(ThingNode head, Ring target){
ThingNode cursor = head;
while (cursor != null){
if (cursor.getData().equals(target))
return cursor;
cursor = cursor.getLink();
}
return null;
}
public static ThingNode listPosition(ThingNode head, int position){
ThingNode cursor = head;
int index = 1;
while (cursor != null && index < position){
index++;
cursor = cursor.getLink();
}
return cursor;
}
public static void display(ThingNode list){
ThingNode cursor = list;
while (cursor != null){
System.out.println(cursor.getData());
cursor = cursor.getLink();
}
}
}
51551 - Linked Bag/Solution/Simple Code Files/assignment3/ThingLinkedBagDriver.java
51551 - Linked Bag/Solution/Simple Code Files/assignment3/ThingLinkedBagDriver.java
package assignment3;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class ThingLinkedBagDriver {
public static void main(String[] args) {
testSize();
testDisplay();
testAdd();
testRemove();
testCountRange();
testGrab();
testPositionOf();
testSet();
testTotalValues();
testLessThan();
testGetMax();
}
private static void testGetMax() {
ThingLinkedBag bag = getBag2();
boolean passed = true;
if(!bag.getMax().equals(new Ring("Unique",2)))
passed = false;
bag = getBag1();
if(!bag.getMax().equals(new Ring("Vintage",6)))
passed = false;
if(!passed)
System.out.println("GetMax test Failed...!!!");
else
System.out.println("GetMax test passed Successfully...!!!");
}
private static void testLessThan() {
ThingLinkedBag bag = getBag2();
boolean passed = true;
ThingNode node = bag.lessThan(new Ring("Unique",2));
if(!node.getData().equals(new Ring("Riviera",3)) || node.getLink() != null)
passed = false;
bag = getBag1();
node = bag.lessThan(new Ring("Unique",2));
if(!node.getData().equals(new Ring("Garland",4)) || node.getLink() == null)
passed = false;
node = node.getLink();
if(!node.getData().equals(new Ring("Halo",5)) || node.getLink() == null)
passed = false;
node = node.getLink();
if(!node.getData().equals(new Ring("Pave",7)) || node.getLink() == null)
passed = false;
node = node.getLink();
if(!node.getData().equals(new Ring("Riviera",3)) || node.getLink() != null)
passed = false;
if(!passed)
System.out.println("LessThan test Failed...!!!");
else
System.out.println("LessThan test passed Successfully...!!!");
}
private static void testTotalValues() {
ThingLinkedBag bag = getBag2();
boolean passed = true;
if(bag.totalValue() != 12)
passed = false;
bag = getBag1();
if(bag.totalValue() != 36)
passed = false;
if(!passed)
System.out.println("TotalValue test Failed...!!!");
else
System.out.println("TotalValue test passed Successfully...!!!");
}
private static void testSet() {
ThingLinkedBag bag = getBag2();
boolean passed = true;
bag.set(0, new Ring("Pave1", 1));
bag.set(1, new Ring("Pave2", 1));
bag.set(2, new Ring("Pave3", 1));
bag.set(3, new Ring("Pave4", 1));
bag.set(4, new Ring("Pave5", 1));
Ring ring = bag.grab(3);
if(!ring.equals(new Ring("Pave4", 1)))
passed = false;
ring = bag.grab(2);
if(!ring.equals(new Ring("Pave3", 1)))
passed = false;
ring = bag.grab(1);
if(!ring.equals(new Ring("Pave2", 1)))
passed = false;
if(bag.size() != 3)
passed = false;
if(!passed)
System.out.println("Set test Failed...!!!");
else
System.out.println("Set test passed Successfully...!!!");
}
private static void testPositionOf() {
ThingLinkedBag bag = getBag2();
boolean passed = true;
if(bag.positionOf(new Ring("Pave", 7)) != 3)
passed = false;
if(bag.positionOf(new Ring("Unique", 2)) != 2)
passed = false;
if(bag.positionOf(new Ring("Riviera", 3)) != 1)
passed = false;
if(bag.positionOf(new Ring("aaa", 3)) != -1)
passed = false;
if(!passed)
System.out.println("PositionOf test Failed...!!!");
else
System.out.println("PositionOf test passed Successfully...!!!");
}
private static void testGrab() {
ThingLinkedBag bag = getBag2();
boolean passed = true;
Ring ring = bag.grab(0);
if(ring !=null)
passed = false;
ring = bag.grab(3);
if(!ring.equals(new Ring("Pave", 7)))
passed = false;
ring = bag.grab(2);
if(!ring.equals(new Ring("Unique", 2)))
passed = false;
ring = bag.grab(1);
if(!ring.equals(new Ring("Riviera", 3)))
passed = false;
ring = bag.grab(4);
if(ring !=null)
passed = false;
if(!passed)
System.out.println("Grab test Failed...!!!");
else
System.out.println("Grab test passed Successfully...!!!");
}
private static void testCountRange() {
ThingLinkedBag bag = getBag1();
int range = bag.countRange(new Ring("Unique", 2), new Ring("Vintage", 6));
if(range != 3) {
System.out.println("CountRange test Failed...!!!");
return;
}
range = bag.countRange(new Ring("A", 2), new Ring("Z", 6));
if(range != 8) {
System.out.println("CountRange test Failed...!!!");
return;
}
range = bag.countRange(new Ring("Vintage", 1), new Ring("Z", 10));
if(range != 1) {
System.out.println("CountRange test Failed...!!!");
return;
}
System.out.println("CountRange test passed Successfully...!!!");
}
private static void testRemove() {
ThingLinkedBag bag = getBag1();
boolean val = bag.remove(new Ring("Unique", 2));
if( !val || bag.grab(2).equals(new Ring("Unique", 2))) {
System.out.println("Remove test Failed...!!!");
return;
}
val = bag.remove(new Ring("Unique", 2));
if( !val && bag.grab(5).equals(new Ring("Unique", 2))) {
System.out.println("Remove test Failed...!!!");
return;
}
val = bag.remove(new Ring("Unique", 2));
val = bag.remove(0);
val = bag.remove(7);
if( val || bag.size() != 6) {
System.out.println("Remove test Failed...!!!");
return;
}
val = bag.remove(1);
if( !val || bag.size() != 5 || bag.grab(1).equals(new Ring("Pave", 7))) {
System.out.println("Remove test Failed...!!!");
return;
}
val = bag.remove(5);
if( !val || bag.size() != 4 || bag.grab(1).equals(new Ring("Braided", 7))) {
System.out.println("Remove test Failed...!!!");
return;
}
bag.removeLast();
if(bag.size() != 3 || bag.grab(1).equals(new Ring("Halo", 5))) {
System.out.println("Remove test Failed...!!!");
return;
}
val = bag.remove(2);
if( !val || bag.size() != 2 || bag.grab(2).equals(new Ring("Vintage", 6))) {
System.out.println("Remove test Failed...!!!");
return;
}
bag.removeLast();
bag.removeLast();
if( bag.size() != 0 ) {
System.out.println("Remove test Failed...!!!");
return;
}
System.out.println("Add test passed Successfully...!!!");
}
private static void testAdd() {
ThingLinkedBag bag = getBag1();
bag.add(new Ring("Unique", 2));
if(!bag.grab(1).equals(new Ring("Unique", 2))) {
System.out.println("Add test Failed...!!!");
return;
}
bag.add(1, new Ring("Pave4", 2));
if(!bag.grab(1).equals(new Ring("Pave4", 2))) {
System.out.println("Add test Failed...!!!");
return;
}
bag.add(4, new Ring("Pave5", 2));
if(!bag.grab(4).equals(new Ring("Pave5", 2))) {
System.out.println("Add test Failed...!!!");
return;
}
bag.add(8, new Ring("Pave8", 8));
if(!bag.grab(8).equals(new Ring("Pave8", 8))) {
System.out.println("Add test Failed...!!!");
return;
}
bag.add(25, new Ring("Pave9", 8));
if(!bag.grab(13).equals(new Ring("Pave9", 8))) {
System.out.println("Add test Failed...!!!");
return;
}
bag.addLast( new Ring("Pave10", 8));
if(!bag.grab(14).equals(new Ring("Pave10", 8))) {
System.out.println("Add test Failed...!!!");
return;
}
System.out.println("Add test passed Successfully...!!!");
}
private static void testDisplay() {
ThingLinkedBag bag1 = getBag1();
ThingLinkedBag bag2 = getBag2();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
PrintStream old = System.out;
System.setOut(ps);
bag1.display();
bag2.display();
System.out.flush();
System.setOut(old);
if(baos.toString().equals("Ring type-[Riviera: 3]\n" +
"Ring type-[Unique: 2]\n" +
"Ring type-[Unique: 2]\n" +
"Ring type-[Pave: 7]\n" +
"Ring type-[Halo: 5]\n" +
"Ring type-[Garland: 4]\n" +
"Ring type-[Vintage: 6]\n" +
"Ring type-[Braided: 7]\n" +
"Ring type-[Riviera: 3]\n" +
"Ring type-[Unique: 2]\n" +
"Ring type-[Pave: 7]\n"))
System.out.println("Display test passed Successfully...!!!");
else
System.out.println("Display test Failed...!!!");
}
private static void testSize() {
ThingLinkedBag bag1 = getBag1();
ThingLinkedBag bag2 = getBag2();
if(bag1.size() == 8 && bag2.size() == 3)
System.out.println("Size test passed Successfully...!!!");
else
System.out.println("Size test Failed...!!!");
}
private static ThingLinkedBag getBag1(){
ThingLinkedBag bag1 = new ThingLinkedBag();
bag1.add(new Ring("Braided", 7));
bag1.add(new Ring("Vintage", 6));
bag1.add(new Ring("Unique", 2));
bag1.add(new Ring("Garland", 4));
bag1.add(new Ring("Halo", 5));
bag1.add(new Ring("Pave", 7));
bag1.add(new Ring("Unique", 2));
bag1.add(new Ring("Riviera", 3));
return bag1;
}
private static ThingLinkedBag getBag2(){
ThingLinkedBag bag2 = new ThingLinkedBag();
bag2.add(new Ring("Pave", 7));
bag2.add(new Ring("Unique", 2));
bag2.add(new Ring("Riviera", 3));
return bag2;
}
}
51551 - Linked Bag/Solution/Simple Code Files/assignment3/ThingLinkedBag.java
51551 - Linked Bag/Solution/Simple Code...