Question

I NEED HELP with this. please create a UML diagram. I need a simple code to...

I NEED HELP with this. please create a UML diagram. I need a simple code to solve the problem.  

The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document.

ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should be included in an interface.)
Specify operations to

 create an empty bag that can hold up to 100 items,
 put an item at the end of the list of the bag (insert(item)),
 remove the last item in the bag ( removeLast()),
 remove a random item from the bag (removeRandom()),
 get a reference to the first occurrence of an item from the bag if it is existed (get(item)), get a reference to an item at position index( get(index)),
 check how many items are in the bag (size()),
 check to see if the bag is full (isFull()),
 check to see if the bag is empty( isEmpty()),
 and completely empty the bag (makeEmpty()).

ADT Bag Design:

Complete a UML diagram to include all classes that are needed to meet the specifications. An interface class is usually defined to include all operations. A class implementing this interface provides implementation details.

Exceptions should to be considered when operations are designed.
Java has two types of exceptions: checked exceptions and runtime exceptions.
Checked exceptions are instances of classes that are sub classes of java.lang.Exception class. They must be handled

locally or explicitly thrown from the method. They are typically used when the method encounters a serious problem. In some cases, the error may be considered serious enough that the program should be terminated.

Runtime exceptions occur when the error is not considered as serious. These types of exceptions can often be prevented by fail-safe programming. For example, it is fairly easy to avoid allowing an array index to go out of range, a situation that causes the runtime exception ArrayIndexOutOfBoundsException to be thrown. Runtime exceptions are instances of classes that are subclasses of the java.lang.RuntimeException class. RuntimeException is a subclass ofjava.lang.Exception that relaxes the requirement forcing the exception to be either handled or explicitly thrown by the method.

In general, some operations of an ADT list can be provided with an index value. If the index value is out of range, an exception should be thrown. Therefore, a subclass of IndexOutOfBoundException needs to be defined.

Also, an exception is needed when the list storing the items becomes full. A subclass of java.lang.RuntimeExceptionshould be defined for this erroneous situation. A full ADT bag should throw an exception when a new item is inserted.

ADT Bag Implementation:

Data structure array must be used to store all items in an ADT Bag list. The element type of the array should beObject type. Implement all classes included in the design. Javadoc comments need be written during this activity.

5

Class comments must be included right above the corresponding class header. Method comments must be included right above the corresponding a method header. All comments must be written in Javadoc format. There will be no credits if comments are not complete, and/or not written in Javadoc format.

ADT Bag Test/Debug:

Note: It is required to store all testing data in a file. No credit will be given if not.
It is required to use decomposition design technique. No credit will be given if not.

To test ADT bag design, all operations in the design must be tested. In general, an empty ADT Bag is created, and then, fill the bag list with items read from a text file, invoke any operations via the list, and always display the list after it is changed. It is not efficient to let main to do all. Method main should be very small and should be the only method in the class. It should invoke a method (for example, start) that is decomposed into more methods (for example, fillList,displayList) in a separate class. Every method should be designed as a single-minded method. For example, Class ADTBagTest contains method main; class ADTBagUtility is a helper class. Both classes are used for testing purposes.

public class ADTBagTest{

public static void main(String[] args){ADTBagUtility.start();

}

public class ADTBagUtility {

/**
* Creates a bag of items, and change items in the bag, and displays items. */

public static void start(){
ADTBagArrayBased list = new ADTBagArrayBased();

}

}

//Fill the bag.
//A Scanner object must be used to read item information from a file. //Items need to be created before they are put into the bag.fillList(list);

//Display items in the bag.displayList(list);

/**
 * Stores items into a bag.
 * @param list A reference to a bag
 */

public static void fillList(ADTBagArrayBased list){Scanner input = ...

//add items into the bag and/or remove items from the bag. //All operations in ADT Bag design must be used/tested here.

}

/**
 * Displays items in the bag.
 * @param list A reference to a bag
 */

public static void displayList(ADTBagArrayBased list){...

0 0
Add a comment Improve this question Transcribed image text
Answer #1

fillList() method fills the bag from file.
It was commented that the All operations in ADT Bag design must be used/tested here.
So I have tested them there.. you can comment them if you just need fillList() functionality.

Please find the java files and outputs below:
******************************************************************************************

public class ADTBagTest{

public static void main(String[] args)

{

ADTBagUtility.start();

}

}

******************************************************************************************

import java.io.File;

import java.util.Scanner;

public class ADTBagUtility {

/**

* Creates a bag of items, and change items in the bag, and displays items.

*/

public static void start() {

ADTBagArrayBased list = new ADTBagArrayBased<Integer>();

// Fill the bag.

// A Scanner object must be used to read item information from a file.

// Items need to be created before they are put into the bag.

fillList(list);

// Display items in the bag.

displayList(list);

}

/**

* Stores items into a bag.

* @param list A reference to a bag

*/

public static void fillList(ADTBagArrayBased list){

try (Scanner fileScan = new Scanner(new File("data.txt"))) {

String line = "";

System.out.println("Loading data from file..\nPlease wait..");

while (fileScan.hasNext()) {

line = fileScan.nextLine(); // one line of data

Integer data = Integer.parseInt(line);

list.insert(data);

}

}

catch(Exception e) {

e.printStackTrace();

}

//add items into the bag and/or remove items from the bag.

//All operations in ADT Bag design must be used/tested here.

//THE CODE BELOW IS FOR TESTING ONLY//

System.out.println("After loading data from file bag:");

displayList(list);

System.out.println("\n\nAdding 4 more items");

list.insert(130);

list.insert(140);

list.insert(150);

list.insert(160);

System.out.println("After adding more items to bag:");

displayList(list);

System.out.println("Removing last item..");

list.removeLast();

System.out.println("After removing last item from bag:");

displayList(list);

System.out.println("Removing random item..");

list.removeRandom();

System.out.println("After removing random item from bag:");

displayList(list);

System.out.println("Getting item at index 1 :" + list.get(1));

System.out.println("Getting item 50(if available else null) :" + list.get((Integer)50));

System.out.println("Getting item 150(if available else null) :" + list.get((Integer)150));//We get null since 150 was a removed from bag [removeLast()]

}

/**

* Displays items in the bag.

* @param list A reference to a bag

*/

public static void displayList(ADTBagArrayBased list){

System.out.println("\n\nBAG CONTENTS:");

for(int i = 0;i<list.size();i++) {

System.out.println("Item " + (i+1) + ": " + list.get(i));

}

}

}

******************************************************************************************

import java.util.Random;

public class ADTBagArrayBased<T> {

private T[] bag;

private final int CAPACITY = 15;

int position;

@SuppressWarnings("unchecked")

public ADTBagArrayBased() {

position = -1;

bag = (T[]) new Object[CAPACITY];

}

public void insert(T item) {

if (isFull()) {

System.out.println("Cannot add " + item + ". Bag is full");

} else {

position++;

bag[position] = item;

System.out.println(item + " added to bag");

}

}

public void removeLast() {

if (isEmpty()) {

System.out.println("Bag is empty");

} else {

bag[position] = null;

position--;

System.out.println("Item removed from bag");

}

}

public void removeRandom() {

Random r = new Random();

int max = position;

int min = 0;

int index = r.nextInt((max - min) + 1) + min;

if (index == position)

removeLast();

else {

for (int i = index; i <= position; i++) {

bag[i] = bag[i + 1];

}

bag[position] = null;

position--;

}

System.out.println("Removed item at index " + index);

}

public T get(T item) {

for (int i = 0; i <= position; i++) {

if (bag[i].equals(item)) {

return item;

}

}

return null;

}

public T get(int index) {

if (index >= 0 && index <= position) {

return bag[index];

} else {

return null;

}

}

public int size() {

return position + 1;

}

public boolean isFull() {

return position == bag.length - 1;

}

public boolean isEmpty() {

return position == -1;

}

}

******************************************************************************************

Outputs:

Loading data from file..
Please wait..
10 added to bag
20 added to bag
30 added to bag
40 added to bag
50 added to bag
60 added to bag
70 added to bag
80 added to bag
90 added to bag
100 added to bag
110 added to bag
120 added to bag
After loading data from file bag:


BAG CONTENTS:
Item 1: 10
Item 2: 20
Item 3: 30
Item 4: 40
Item 5: 50
Item 6: 60
Item 7: 70
Item 8: 80
Item 9: 90
Item 10: 100
Item 11: 110
Item 12: 120


Adding 4 more items
130 added to bag
140 added to bag
150 added to bag
Cannot add 160. Bag is full
After adding more items to bag:


BAG CONTENTS:
Item 1: 10
Item 2: 20
Item 3: 30
Item 4: 40
Item 5: 50
Item 6: 60
Item 7: 70
Item 8: 80
Item 9: 90
Item 10: 100
Item 11: 110
Item 12: 120
Item 13: 130
Item 14: 140
Item 15: 150
Removing last item..
Item removed from bag
After removing last item from bag:


BAG CONTENTS:
Item 1: 10
Item 2: 20
Item 3: 30
Item 4: 40
Item 5: 50
Item 6: 60
Item 7: 70
Item 8: 80
Item 9: 90
Item 10: 100
Item 11: 110
Item 12: 120
Item 13: 130
Item 14: 140
Removing random item..
Removed item at index 4
After removing random item from bag:


BAG CONTENTS:
Item 1: 10
Item 2: 20
Item 3: 30
Item 4: 40
Item 5: 60
Item 6: 70
Item 7: 80
Item 8: 90
Item 9: 100
Item 10: 110
Item 11: 120
Item 12: 130
Item 13: 140
Getting item at index 1 :20
Getting item 50(if available else null) :null
Getting item 150(if available else null) :null


BAG CONTENTS:
Item 1: 10
Item 2: 20
Item 3: 30
Item 4: 40
Item 5: 60
Item 6: 70
Item 7: 80
Item 8: 90
Item 9: 100
Item 10: 110
Item 11: 120
Item 12: 130
Item 13: 140

******************************************************************************************

Input: data.txt
10
20
30
40
50
60
70
80
90
100
110
120

Add a comment
Know the answer?
Add Answer to:
I NEED HELP with this. please create a UML diagram. I need a simple code to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

  • I was wondering if I could get some help with a Java Program that I am...

    I was wondering if I could get some help with a Java Program that I am currently working on for homework. When I run the program in Eclipse nothing shows up in the console can you help me out and tell me if I am missing something in my code or what's going on? My Code: public class Payroll { public static void main(String[] args) { } // TODO Auto-generated method stub private int[] employeeId = { 5658845, 4520125, 7895122,...

  • please this is Java and i need help Question 5 public class Food { public void...

    please this is Java and i need help Question 5 public class Food { public void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { } public static void Foodmethod_3(int i) { public class Bankudade extends Food { public static void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { public void Foodmethod_3 (int i) { > 7 a) Determine which method in the subclass overrides a method in the super class? (6 marks) EV b) Determine which...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • Need help with this Java. I need help with the "to do" sections. Theres two parts...

    Need help with this Java. I need help with the "to do" sections. Theres two parts to this and I added the photos with the entire question Lab14 Part 1: 1) change the XXX to a number in the list, and YYY to a number // not in the list 2.code a call to linearSearch with the item number (XXX) // that is in the list; store the return in the variable result 3. change both XXX numbers to the...

  • I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create...

    I need help with this coding. A. Create ArrayIntStack.java (one that's REALLY O(constant), totally!!!) B. Create the Javadoc web page (separate Assignment #15b). A. Create your own Stack Class, call it ArrayIntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT