4. Java Netbeans-TheBag ADT is an abstraction of a multiset, i.e. a set where an itemcan be...
Question:
4. Java Netbeans-TheBag ADT is an abstraction of a multiset, i.e. a set where an itemcan be present in the bag more than once. Locations of itemsare not relevant in a bag, so operations such as removeItemAt(intlocation) or retrieveItemAt(int location) are not allowed.Implement an array-based Bag as described in the UML class diagrambelo
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
new Main();
}
/**
* Tester method.
*/
public Main()
{
ArrayBag bag = new ArrayBag();
File file = new File("assignment 1 testset.txt");
try
{
Scanner in = newScanner(file);
String operation = "",item = "";
int entryNumber =0;
while(in.hasNextLine())
{
entryNumber++;
operation =in.next();
if(!operation.equals("IS_EMPTY"))
{
item = in.next();
System.out.println("" + operation + " " + item);
}
elseSystem.out.println("" + operation);
switch(operation)
{
case "ADD": bag.add(item);
System.out.println(bag);
break;
case "COUNT": System.out.println("Number of "" + item +"": " + bag.count(item));
break;
case "IS_EMPTY": System.out.println(bag.isEmpty());
break;
case "REMOVE": bag.remove(item);
System.out.println(bag);
break;
default: System.out.println("Operation "" + operation + ""unknown at line " + entryNumber);
System.exit(1);
}
}
}
catch(FileNotFoundException e)
{
System.out.println("Filenot found!");
System.exit(1);
}
}
}
public class ArrayBag {
private final int SIZE;
String[] bag;
int length;
/**
* Default constructor. Initial bag capacity is1.
*
*/
public ArrayBag() {
int size = 0;
SIZE = size;
length = 0;
bag = new String[SIZE];
}
/**
* Adds new element to the bag. If the arrayused to store the bag items is
* full, it will be expanded by doubling itssize.
*
* @param s new element to be added to thebag.
*/
public void add(String s) {
if (length < SIZE && s !=null) {
bag[length++] = s;
}
}
/**
* Determines the number of occurrences in thebag of a given element.
*
* @param s given element
* @return number of times the given elementoccurs in the bag.
*/
public int count(String s) {
int count = 0;
for (int i = 0; i < length; i++){
if (bag[i].equals(s)){
count +=1;
}
}
return count;
}
/**
* Determines if bag is empty.
*
* @return true if bag contain no elements,false otherwise.
*/
public boolean isEmpty() {
return length == 0;
}
void remove(String item) {
throw newUnsupportedOperationException("Not supported yet."); //To changebody of generated methods, choose Tools | Templates.
}
}
/**
* Removes all occurrences of a given element.
*
* @param s element to be removed.
*/
public void remove(String s)
{
for (int i = 0; i < length; i++) {
if (bag[i].equals(s)){
for (int j= i; j < length; j++) {
bag[j] = bag[j + 1];
}
length--;
}
}
}
/**
* Constructs a String description of thebag.
*
* @return String containing the bagelements.
*/
public String toString()
{
String str = "";
for (int i=0; i {
str += bag[i] + ",";
}
if (length != 0) return "Bag: {" +str + bag[length-1] + "}";
else return "Bag: {" + str + "}";
}
private String[] bag; //array that storesthe bag elements
private int length; //number ofelements in the bag
private int SIZE; //bagcapacity
}
Data Modeling and Database Design
ISBN: 978-1285085258
2nd edition
Authors: Narayan S. Umanath, Richard W. Scammel