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

I am using eclipse to write in Java: //A snapshot of the output would be greatly

ID: 668205 • Letter: I

Question

I am using eclipse to write in Java:

//A snapshot of the output would be greatly appreciated. Thanks!


/////////////////////////linkedlist
/******************************
* Week 2 lab - exercise 1: *
* a simple LinkedList class *
*******************************/

/**
* Class implementing a linked list.
*/
public class LinkedList
{

private Node first; //dummy header node

/**
* Initializes the list to empty creating a dummy header node.
*/
public LinkedList()
{
first = new Node();
}

/**
* Determines whether the list is empty
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
return (first.getNext() == null);
}

/**
* Prints the list elements.
*/
public void display()
{
Node current = first.getNext();

while (current != null)
{
System.out.print(current.getInfo() + " ");
current = current.getNext();
}

System.out.println();
}

/**
* Adds the element x to the beginning of the list.
*
* @param x element to be added to the list
*/
public void add(int x)
{
Node p = new Node();

p.setInfo(x);
p.setNext(first.getNext());

first.setNext(p);
}

/**
* Deletes an item from the list. Only the first occurrence of the item in
* the list will be removed.
*
* @param x element to be removed.
*/
public void remove(int x)
{
Node old = first.getNext(),
p = first;

//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
{
found = true;
} else
{
p = old;
old = p.getNext();
}
}

//if x is in the list, remove it.
if (found)
{
p.setNext(old.getNext());
}

}
}


/////////////////////////////////////////////////////////////////////// node class

/******************************
* Week 2 lab - exercise 1: *
* a simple LinkedList class *
*******************************/

/**
* Linked list node.
*/
public class Node
{

private int info; //element stored in this node
private Node next; //link to next node

/**
* Initializes this node setting info to 0 and next to null
*/
public Node()
{
info = 0;
next = null;
}

/**
* Sets the value for this node
*
* @param i the desired value for this node
*/
public void setInfo(int i)
{
info = i;
}

/**
* Sets the link to the next node
*
* @param l node reference
*/
public void setNext(Node l)
{
next = l;
}

/**
* Returns the value in this node
*
* @return the value in this node
*/
public int getInfo()
{
return info;
}

/**
* Returns the link to the next node
*
* @return link to the next node
*/
public Node getNext()
{
return next;
}
}


///////////////////////////////////main

/******************************
* Week 2 lab - exercise 1: *
* a simple LinkedList class *
*******************************/

public class Main
{
   public static void main(String args[])
   {
       LinkedList intList = new LinkedList();

       System.out.print("List of numbers before list creation: ");
       for (int i =0; i < 10; i++)
       {
           int info = (int)(Math.random()*10);
           System.out.print(info + " ");

           intList.add(info);
       }

       System.out.print(" List of numbers after list creation: ");

       intList.display();
   }
}

Create a class bag that uses a linked list 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 isEmpty0: determines whether the bag is empty c. void print(): prints the bag elements d. int getength: returns the number of items in the bag e. void clear: removes all of the items from the bag f. void add(String item): adds an item to the bag g. void removeOne(String item): removes an item from the bag, only one occurrence of the item should be removed. int count(String item): counts the number of occurrences of an item in the bag. (Note that you can reuse the code in Exercise 1 for the LinkedList class to create your bag class. It will help you to save development time.)

Explanation / Answer

Please find below the code as demanded in the question. Their are three classes in the code :- Node, LinkedList, Bag. And the driver program `main`. You can modify the code inside `main` according to your need/test cases.

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Main
{
public class LinkedList
{
private Node first; //dummy header node

public LinkedList()
{
first = new Node();
}

public boolean isEmpty()
{
return (first.getNext() == null);
}

public void display()
{
Node current = first.getNext();
while (current != null)
{
System.out.print(current.getInfo() + " ");
current = current.getNext();
}
System.out.println();
}

public void getLen()
{
   int count=0;
Node current = first.getNext();
while (current != null)
{
count++;
current = current.getNext();
}
System.out.println(count);
}

public void add(String x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}
  

public void remove(String x)
{
Node old = first.getNext(),
p = first;
//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
{
found = true;
} else
{
p = old;
old = p.getNext();
}
}
//if x is in the list, remove it.
if (found)
{
p.setNext(old.getNext());
}
}

public void clearList()
{
Node current = first.getNext();
while (current != null)
{
remove(current.getInfo());
current = current.getNext();
}
System.out.println();
}

public int countElem(String item)
{
   int cnt=0;
Node current = first.getNext();
while (current != null)
{
if( current.getInfo().equals(item) ) cnt++;
current = current.getNext();
}
   return cnt;
}

}

public class Node
{
private String info; //element stored in this node
private Node next; //link to next node

public Node()
{
info = null;
next = null;
}

public void setInfo(String i)
{
info = i;
}

public void setNext(Node l)
{
next = l;
}

public String getInfo()
{
return info;
}

public Node getNext()
{
return next;
}
}


public class bag
{
   public LinkedList bagItemList;
  

public void Bag()
{
bagItemList = new LinkedList();
}

public boolean isEmpty()
{
   return bagItemList.isEmpty();
}

public void print()
{
   bagItemList.display();
}

public void getLength()
{
   bagItemList.getLen();
}

public void clear()
{
bagItemList.clearList();
}

public void add(String item)
{
   bagItemList.add(item);
}

public void removeOne(String item)
{
   bagItemList.remove(item);
}

public int count(String item)
{
   return bagItemList.countElem(item);
}

}

public void main (String[] args) throws java.lang.Exception
{
   bag Bag = new bag();
   String keyword[] = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue"};
   int len = keyword.length;

   while(len > 0)
   {
   bag.add(keyword[len]);
   --len;
   }
System.out.print("Content of bag : ");
Bag.print();
}
}

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