Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class ICA06 { final static Scanner cin = new Scanner(System.in); final static Bag friends = new LinkedBag(); /** * @param args the command line
public class ICA06 { final static Scanner cin = new Scanner(System.in); final static BagCreate a standard Java application using NetBeans. Name it ICA06 Let NetBeans create the main class for you. Delete the code created by NetBeans and copy-paste code from ICA06_Start.txt. This includes the main (client) class, generic Bag interface, generic Node class, and a skeleton generic class that implements the methods specified in the Bag interface (as stubs). Generic typing considerations: Specify as little about the type as is necessary to support your code. In this case constraining the type parameter is not necessary. Remember that all non- primitive types in Java support the .equals() method, and tha isall we need to implement the Bag interface methods. Do not forget to add your name in the output identification (sign-on) println. Add header block comments at top of the program textfriends = new LinkedBag(); /** * @param args the command line arguments */ public static void main(String[] args) { char cmd; out.println("CPS 151 ICA 06 by ________________"); out.print(" A/C/F/R/P/S/Q? "); cmd = Character.toUpperCase(cin.next().trim().charAt(0)); while(cmd != 'Q') { if (cmd == 'A') add(); else if (cmd == 'C') count(); else if (cmd == 'F') find(); else if (cmd == 'R') remove(); else if (cmd == 'S') out.println("The bag has " + friends.size() + " items"); else if (cmd == 'P') out.println(friends); else out.println("Invalid command"); out.print(" A/C/F/R/P/S/Q? "); cmd = Character.toUpperCase(cin.next().trim().charAt(0)); } // end loop } // end main private static void add() { String name = getWord("Enter friend's first name: "); friends.add(name); out.println("Name added"); } private static void find() { out.print("Enter friend's first name: "); String name = getWord("Enter friend's first name: "); if (friends.find(name)) out.println("The name " + name + " is in bag"); else out.println("The name " + name + " is not in bag"); } private static void remove() { String name = getWord("Enter friend's first name: "); if (friends.remove(name)) out.println("The name " + name + " was removed"); else out.println("The name " + name + " is not in bag, cannot remove"); } public static void print() { out.println(friends); } private static void count() { String name = getWord("Enter friend's first name: "); out.println("The name occurs " + friends.count(name) + " times in the bag"); } static String getWord(String prompt) { out.print(prompt); return cin.next(); } } // end class //-------------------------- interface Bag interface Bag { void add(T item); boolean remove(T item); boolean find(T item); int count(T item); int size(); @Override String toString(); } // end interface //-------------------------- class LinkedBag class LinkedBag implements Bag { private Node head; public LinkedBag() { head = null; } @Override public void add(T item) { } @Override public boolean remove(T item) { return false; } @Override public boolean find(T item) { return false; } @Override public int count(T item) { return 0; } @Override public int size() { return 0; } public String toString() { return ""; } } // end class LinkedBag //-------------------------- class Node class Node { public T data; public Node next; // Constructor 0 public Node() { this(null); } // Constructor 1 public Node(T data) { this(data, null); // use Constructor 2 specifying both instance variables } // Constructor 2 public Node(T data, Node next) { this.data = data; this.next = next; } } // end class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started