COSC 1P03 Assignment 3 Linked Structures and Generics Refer to Sakai for due date. Objective: To implement a simple ADT that supports dynamic storage of arbitrary types, and to write a program that...

1 answer below »
2ws


COSC 1P03 Assignment 3 Linked Structures and Generics Refer to Sakai for due date. Objective: To implement a simple ADT that supports dynamic storage of arbitrary types, and to write a program that demonstrates it through an interactive text interface. Background: Let's define a Pouch ADT: • It could hold pretty much anything ◦ It doesn't hold mixed types; it can simply be declared/instantiated to hold anything • It can hold (nearly) any number of those things ◦ I'd go so far as to say, “it grows and shrinks with the amount of data it holds” • You can add an item to the pouch ◦ (If you want to add multiple items, do it multiple times) • You can retrieve a single item from the pouch ◦ There is no expectation of which item you will receive ◦ If you try to retrieve from an empty pouch, that's an underflow • You can check if the bag isEmpty • You can shake the bag, to rearrange its contents somehow ◦ There's no specific expectation of how to do this, but it should be reasonably expected to be at least somewhat different in its internal sequence after this • You can iterate over the pouch ◦ Through conventional means A suitable client program would have the ability to test all of these features, as much as desired, until he or she wished to exit. Requirements: The ADT and the client must be in different packages. • You must design your solution, in Dr. Java, to include both packages ◦ If you try writing it elsewhere and then dropping it in, don't be surprised if you get a zero • The ADT is in the bagofholding package, and the client is in the client package ◦ Make sure your project is configured to have client.Client as the 'main class' ▪ (Don't forget you can ask me about these things?) You need a Pouch interface, and a separate LnkPouch implementation. • I trust you know why it's called LnkPouch • Also in the same package will be an UnderflowException (you can just borrow from the class example) • Clearly you'll also need to write your own iterator for this The shaking really doesn't have to be any single specific implementation; it just needs to be obvious to the marker that its sequence is changing each time (unfortunately, simply reversing the list wouldn't accomplish this). You are advised to check out the sample execution at the end. That's what the marker will be using as a 'reasonable example' of a client program. You need to include a (brief) writeup, explaining how you 'shook' your pouch. Include a diagram. Tips: I know I always say, “don't cheat”, but just to be clear: in spite of the name, this 'pouch' is not the same thing as the more common 'bag'. So if you try googling how to write a bag, please don't complain when you receive a zero for doing the entirely wrong assignment. K? It should be obvious from the description above that this needs to be generic. If it isn't, again, don't be surprised by a zero. Also, the 'shaking' method is probably the hardest part of the whole assignment, but since it's just reorganizing you could (hypothetically) save that one for last. There'll probably be quite a few completely different implementations for this. • It would not be reasonable to expect the markers to fix your indentation for you • It would not be reasonable to expect the markers to pull apart indecipherable code • You will make it easy to tell what you're doing, or someone will be sad If there is a single System.out.println anywhere in your bagofholding package, or a single mention of String, you're doing it wrong. Lastly, my interface is about 7 lines of code, my implementation around 50, and my client is ~60. The mechanisms themselves are not the difficult part here. Your goals are: • To get experience making linked structures that don't fit a specific existing model • To carefully follow instructions • To document your work Submission: Submission is electronic only. Bundle up your solution, which must include: • All of your source files (obviously) • Your Dr. Java project • A sample execution demonstrating how your program works • Your writeup, in .pdf format • All files you used to create your writeup Put all of the above (and whatever else the marker needs to easily grade your work) into a .zip file, and submit it through Sakai. Important: you're writing this, from start to end, in Dr. Java. Sample execution: What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit show What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit add Type in each term. Enter an empty line when finished. Hello class! This is neat? What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit show neat? is This class! Hello What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit retrieve Removing: neat? What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit add Type in each term. Enter an empty line when finished. I had fun with this What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit show this with fun had I is This class! Hello What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit shake (shake shake) What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit show Hello had with I This fun class! is this What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit shake (shake shake) What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit show this is class! fun This I with had Hello What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit empty Removing: this Removing: is Removing: class! Removing: fun Removing: This Removing: I Removing: with Removing: had Removing: Hello What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit show What would you like to do? [add]: Add to pouch [retrieve]: Retrieve a single entry [show]: Display pouch [shake]: Shake pouch [empty]: Empty pouch [exit]: Exit exit Bye!
Answered 2 days AfterJun 21, 2021

Answer To: COSC 1P03 Assignment 3 Linked Structures and Generics Refer to Sakai for due date. Objective: To...

Kashyap answered on Jun 23 2021
143 Votes
86851/86851.docx
Introduction to assignment:
In this assignment, I was supposed to implement an ADT ‘Pouch’ in java
Requirements Specifications of the assignment:
The ADT and the client must be in different packages.
· You must design your solution, in Dr. Java, to include both packages
· If you try writing it elsewhere and then dropping it in, don't be surprised if you get a zero
· The AD
T is in the bagofholding package, and the client is in the client package
· Make sure your project is configured to have client.Client as the 'main class'
You need a Pouch interface, and a separate LnkPouch implementation.
· Also in the same package will be an UnderflowException (you can just borrow from the class example)
· It should be a generic class
· Clearly you'll also need to write your own iterator for this
Application and Implementation:
· For implementing the pouch, I have selected LinkedList of Java as it satisfied all the requirements.
· It should be able to hold any data type
· Should expand or shrink as per the need
· As mentioned in the comments, the Shake function randomizes the list.
· For n^2 times, where n is the size of the list, I’m selecting a random number and removing element at that index, and adding it again at the back of the list.
86851/bagofholding/LnkPouch.java
86851/bagofholding/LnkPouch.java
/**
 * Auto Generated Java Class.
 */
package bagofholding;
import java.util.*;
public class LnkPouch implements Pouch {

  private LinkedList list;

  public LnkPouch(){
    list = new LinkedList<>();
  }

  public void add(T t){
    list.addFirst(t);
  }

  public T retrieve() throws UnderflowException{
    if(list.size()==0){
      throw new UnderflowException("Pouch is Empty!!");
    }
    return list.poll();
  }

  public LinkedList show(){
    return list;
  }
  //It will n^2 times remove a random element and add it to back.
  public void shake(){
    int size = list.size();
    Random random = new Random();
    for(int i=0;i      int rNum = random.nextInt(size);
      T e1 = list.get(rNum);
      list.remove(rNum);
      list.add(e1);
    }
  }

  public LinkedList empty(){
    LinkedList temp = list;
    list = new LinkedList();
    return temp;
  }

}
86851/bagofholding/LnkPouch.java~
/**
* Auto Generated Java Class.
*/
package bagofholding;
import java.util.*;
public class LnkPouch implements Pouch {

/* ADD YOUR CODE HERE */
private LinkedList list;

public LnkPouch(){
list = new LinkedList<>();
}

public void add(T t){
list.addFirst(t);
}

public T retrieve() throws UnderflowException{
// System.out.println(list.size());
if(list.size()==0){
throw new UnderflowException("Pouch is Empty!!");
}
return list.poll();
}

public LinkedList show(){
return list;
}
//It will n^2 times remove a random element and add it to back.
public void shake(){
int size = list.size();
Random random = new Random();
for(int i=0;i int rNum = random.nextInt(size);
T e1 = list.get(rNum);
list.remove(rNum);
list.add(e1);
}
}

public LinkedList empty(){
LinkedList temp = list;
list = new LinkedList();
return temp;
}

}
86851/bagofholding/Pouch.java
86851/bagofholding/Pouch.java
/**
 * Auto Generated Java Class.
 */
package bagofholding;
import java.util.*;
public interface Pouch {

  public void add(T t);
  public T retrieve() throws UnderflowException;
  public LinkedList show();
  public void shake();
  public LinkedList empty();

}
86851/bagofholding/Pouch.java~
/**
* Auto Generated Java Class.
*/
package bagofholding;
import java.util.*;
public interface Pouch {

public void add(T t);
public T retrieve();
public LinkedList show();
public void shake();
public LinkedList...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here