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

WRITE THE JAVA PROGRAM AND PLEASE ATTACH THE SCREENSHOT OF THE OUTPUT CONCOLE Cr

ID: 3883746 • Letter: W

Question

WRITE THE JAVA PROGRAM AND PLEASE ATTACH THE SCREENSHOT OF THE OUTPUT CONCOLE

Create a class bag (multiset) that uses an expandable array to store the bag items. The item type must be a Java String type; that is, the bag will store strings of characters. The class should have the methods listed below. Create a main class to test your bag class. This main class should fill a bag with the keywords of the Java language.

a.Bag(): default constructor

b. boolean isEmpty(): determines whether the bag is empty (still need it)

c. void print(): prints the bag elements (still need it)

d. int getLength(): returns the number of items in the bag (still need it for expansion)

e. void add(String item): adds an item to the bag

f. void removeOne(String item): removes item from the bag; only one occurrence of item should be removed.

Explanation / Answer

Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.

________________

Bag.java

import java.util.ArrayList;

import java.util.Iterator;

public class Bag {

//Declaring instance variable

private String item;

//Creating ArrayList reference

ArrayList<String> arl;

//Zero argumented constructor

Bag()

{

arl=new ArrayList<String>();

}

//check whether the Bag is Empty or not

public boolean isEmpty()

{

if(arl.size()==0)

return true;

else

return false;

}

//Method which display the elements in the Bag

public void print()

{

Iterator itr=arl.iterator();

while(itr.hasNext())

{

System.out.println(itr.next());

}

}

  

//Method which returns the no of Items in the Bag

public int getLength()

{

return arl.size();

}

  

//Method which add the Items to the Bag

public void add(String item)

{

arl.add(item);

}

  

public void removeOne(String item)

{

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

{

if(arl.get(i).equals(item))

arl.remove(i);

}

}

}

___________________

Main.java

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

//Declaring variables

String item;

char ch;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Creating an Bag class Object

Bag b=new Bag();

/* This while loop continues to execute

* until the user enters a 'N' or 'n'

*/

while (true) {

System.out.print("Enter Item :");

item = sc.next();

//Calling the method which will add the items to the Bag

b.add(item);

// Getting the character from the user 'Y' or 'y' or 'N' or 'n'

System.out.print("Do you want to add more Items (Y/N) ::");

ch = sc.next(".").charAt(0);

if (ch == 'Y' || ch == 'y')

continue;

else {

break;

}

}

//Displaying the items in the Bag

System.out.println(" Displaying the Items in the bag :");

b.print();

System.out.println("No of items in the bag :"+b.getLength());

System.out.print("Enter the element you want to remove :");

item=sc.next();

b.removeOne(item);

System.out.println(" Displaying the Items in the bag after removing :"+item);

b.print();

System.out.println(" Is bag is Empty : "+b.isEmpty());

}

}

__________________

Output:

Enter Item :finally
Do you want to add more Items (Y/N) ::Y
Enter Item :catch
Do you want to add more Items (Y/N) ::Y
Enter Item :try
Do you want to add more Items (Y/N) ::Y
Enter Item :boolean
Do you want to add more Items (Y/N) ::Y
Enter Item :char
Do you want to add more Items (Y/N) ::Y
Enter Item :switch
Do you want to add more Items (Y/N) ::Y
Enter Item :abstract
Do you want to add more Items (Y/N) ::Y
Enter Item :byte
Do you want to add more Items (Y/N) ::N

Displaying the Items in the bag :
finally
catch
try
boolean
char
switch
abstract
byte
No of items in the bag :8
Enter the element you want to remove :switch

Displaying the Items in the bag after removing :switch
finally
catch
try
boolean
char
abstract
byte
Is bag is Empty : false

_____________Thank You