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 Bag friends = 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