Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java - Lists and Bags (From the Client Perspective) Part 1: myBag is an object o

ID: 3599156 • Letter: J

Question

Java - Lists and Bags (From the Client Perspective)

Part 1:

myBag is an object of type BagInterface. myBag currently contains the Strings:

david

mark

victor

jason

What will be printed after the following is executed?

System.out.println(myBag.remove());

Part 2:

myList is an object of type ListInterface. myList currently contains the Strings in the order listed:

delaware

texas

california

iowa

What will be printed after the following is executed?

System.out.println(myList.getEntry(4));

Part 3:

list is an object of type ListInterface that contains the Strings in the order listed:

hamster
elephant
iguana
frog

What statement will result in list containing:

hamster
elephant
frog

Part 4:

myList is an object of type ListInterface. myList currently contains the Strings in the order listed:

desk

pencil

egg

menu

What will the list contain after the following is executed?

myList.add(2, "radio");

Write the contents of the list with each element separated by a single space.

-------------------------------

BagInterface.java

public interface BagInterface

{

   /** Gets the current number of entries in this bag.

       @return The integer number of entries currently in the bag. */

   public int getCurrentSize();

  

   /** Sees whether this bag is empty.

       @return True if the bag is empty, or false if not. */

   public boolean isEmpty();

  

   /** Adds a new entry to this bag.

      @param newEntry The object to be added as a new entry.

      @return True if the addition is successful, or false if not. */

   public boolean add(T newEntry);

   /** Removes one unspecified entry from this bag, if possible.

   @return Either the removed entry, if the removal.

was successful, or null. */

   public T remove();

   /** Removes one occurrence of a given entry from this bag.

   @param anEntry The entry to be removed.

   @return True if the removal was successful, or false if not. */

   public boolean remove(T anEntry);

  

   /** Removes all entries from this bag. */

   public void clear();

  

   /** Counts the number of times a given entry appears in this bag.

       @param anEntry The entry to be counted.

       @return The number of times anEntry appears in the bag. */

   public int getFrequencyOf(T anEntry);

  

   /** Tests whether this bag contains a given entry.

       @param anEntry The entry to locate.

       @return True if the bag contains anEntry, or false if not. */

   public boolean contains(T anEntry);

   /** Retrieves all entries that are in this bag.

       @return A newly allocated array of all the entries in the bag.

Note: If the bag is empty, the returned array is empty. */

   public T[] toArray();

} // end BagInterface

----------------------------

ListInterface.java

public interface ListInterface

{

   /** Adds a new entry to the end of this list.

   Entries currently in the list are unaffected.

   The list's size is increased by 1.

   @param newEntry The object to be added as a new entry. */

   public void add(T newEntry);

   /** Adds a new entry at a specified position within this list.

   Entries originally at and above the specified position

   are at the next higher position within the list.

   The list's size is increased by 1.

   @param newPosition An integer that specifies the desired

   position of the new entry.

   @param newEntry The object to be added as a new entry.

   @throws IndexOutOfBoundsException if either

newPosition < 1 or newPosition > getLength() + 1. */

   public void add(int newPosition, T newEntry);

   /** Removes the entry at a given position from this list.

   Entries originally at positions higher than the given

   position are at the next lower position within the list,

   and the list's size is decreased by 1.

   @param givenPosition An integer that indicates the position of

   the entry to be removed.

   @return A reference to the removed entry.

   @throws IndexOutOfBoundsException if either

givenPosition < 1 or givenPosition > getLength(). */

   public T remove(int givenPosition);

   /** Removes all entries from this list. */

   public void clear();

   /** Replaces the entry at a given position in this list.

   @param givenPosition An integer that indicates the position of

   the entry to be replaced.

   @param newEntry The object that will replace the entry at the

position givenPosition.

   @return The original entry that was replaced.

   @throws IndexOutOfBoundsException if either

givenPosition < 1 or givenPosition > getLength(). */

   public T replace(int givenPosition, T newEntry);

   /** Retrieves the entry at a given position in this list.

   @param givenPosition An integer that indicates the position of

   the desired entry.

   @return A reference to the indicated entry.

   @throws IndexOutOfBoundsException if either

givenPosition < 1 or givenPosition > getLength(). */

   public T getEntry(int givenPosition);

   /** Retrieves all entries that are in this list in the order in which

   they occur in the list.

   @return A newly allocated array of all the entries in the list.

If the list is empty, the returned array is empty. */

   public T[] toArray();

   /** Sees whether this list contains a given entry.

   @param anEntry The object that is the desired entry.

   @return True if the list contains anEntry, or false if not. */

   public boolean contains(T anEntry);

   /** Gets the length of this list.

   @return The integer number of entries currently in the list. */

   public int getLength();

   /** Sees whether this list is empty.

   @return True if the list is empty, or false if not. */

   public boolean isEmpty();

} // end ListInterface

Explanation / Answer

PART 1

Statement :System.out.println(myBag.remove());

Output : Last element from bag is removed and its removed value is printed on screen

PART 2

Statement : System.out.println(myList.getEntry(4));

Output : IndexOutOfBoundsException because myList has element only from 0 to 3 .hence 4th element is illegal option.

PART 3

Statement : list.remove(2);

for(String s:list){ System.out.println(s); }

Output :

hamster
elephant
frog

PART 4

Statement : for(String s:myList){ System.out.print(s +", "); }

Output :   desk, pencil, radio, egg, menu,

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote