So I got my program to do exactly what I want it to do, but it does not match th
ID: 3890451 • Letter: S
Question
So I got my program to do exactly what I want it to do, but it does not match the format below. Can someone correct my format?
import java.util.*;
public class StackList{
private static final String Stack = null;
static Scanner scan = new Scanner(System.in);
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}
static void showpop(Stack st) {
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack: " + st);
try {
showpop(st);
}catch (EmptyStackException e) {
System.out.println("empty stack");
int num = 0;
int s=0;
// loop to for the print shape
while (Stack == null) {
Random generator = new Random();
int ran_dom;
ran_dom = generator.nextInt(20);
System.out.println("Menu: ");
System.out.println("1: add(x) " + " " + "2: del(x)");
System.out.println("3: exist(x)" + " " + "4: Goodbye ");
System.out.print("Please select an options: " );
s = scan.nextInt();
// Allow me to select one of the 3 shapes
switch (s)
{
case 1:
System.out.print("Enter a number: ");
num = scan.nextInt();
System.out.println("You Enter: " + num);
showpush(st, num);
break;
case 2:
System.out.print("Enter a number: ");
num = scan.nextInt();
if (st.contains(num))
{
st.remove(Integer.valueOf(num));
System.out.println(st);
}
else
{
System.out.println("Error:");
}
break;
case 3:
System.out.print("Enter a number: ");
num = scan.nextInt();
if (st.contains(num)) {
System.out.println("Number Exists");
} else {
System.out.println("Number do not Exists");
}
break;
// Allow me to end the program
default:
System.out.print("Goodbye");
// System.exit(0);
}
}}}}
For this assignment, you are to write a program which implements a Set data structure using a singly linked list implementation and a driver program that will test this implementation. The Set ADT is a linear collection of data that will support the following operations: add(x)-adds the integer x to the collection. The resulting collection should not contain any duplicate values 1. 2. delete(x)- deletes the integer x from the set. 3. exists(x) returns true if the integer x exists in the set and false otherwise. 4. toString() - returns a string representing the set as a space separated list. The time complexity of all operations should be O(N), where N is the size of the collection. Program Design Requirements 1. Your program should consist of 3 classes: LinkedNode, Set, and Test. 2. The Test class is the driver class for your program (should contain only the main method). Inside the main method, you should create a new Set object, then enter an infinite loop in which the user is prompted to enter one of 3 commands: add x del x exists x stands for an actual number (some integer). The add and delete operations should perform the necessary operation (inserting or deleting the given integer) and then display the contents of the set using the toString() method. The exists command should output true or false depending on whether x exists in the set. 3. The Set class should implement the Set ADT by adding the appropriate variables and methods. There should be 1 method for each of the required operations. 4. The LinkedNode class should contain an integer value and a pointer to the next LinkedNode. 5. Make sure to validate all inputs. This means re-prompting the user if he or she entered something invalid.Explanation / Answer
The provided code in question does not create 3 classes as mentined in the question - LinkedNode, Set and Test.
Given below is the needed code for the question. Hope it helps. If it did, please don't forget to rate the answer. Thank you.
LinkedNode
public class LinkedNode
{
private int data;
private LinkedNode next;
public LinkedNode(int data, LinkedNode nxt)
{
this.data = data;
this.next = nxt;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public LinkedNode getNext() {
return next;
}
public void setNext(LinkedNode next) {
this.next = next;
}
}
Set
public class Set {
private LinkedNode head;
public Set()
{
head = null;
}
public boolean exists(int n)
{
LinkedNode curr = head;
while(curr != null)
{
if(curr.getData() == n) //found matching value
return true;
curr = curr.getNext();
}
return false;//did not find in loop above
}
public boolean add(int n)
{
if(!exists(n)) //since this is a set, check if the value is already not in list
{
head = new LinkedNode(n, head); //always add new value to front of list
return true;
}
else
return false;
}
public boolean delete(int n)
{
LinkedNode prev = null;
LinkedNode curr = head;
while(curr != null)
{
if(curr.getData() == n)
break;
prev = curr;
curr = curr.getNext();
}
if(curr == null) //did not find matching value
return false;
else
{
if(curr == head) //the data to be delete is the head node
head = head.getNext();
else
prev.setNext(curr.getNext());
return true;
}
}
public String toString()
{
LinkedNode curr = head;
String str = "" ;
while(curr != null)
{
str += curr.getData() + " ";
curr = curr.getNext();
}
return str;
}
}
Test
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String command;
String value;
int num;
System.out.println("Please enter commands add, del, exists or quit at the prompt");
Scanner keybd = new Scanner(System.in);
Set myset = new Set();
while(true)
{
System.out.print("> ");
command = keybd.next(); //get the command name
if (command.equals("quit"))
break;
value = keybd.nextLine().trim();//get the value
try
{
num = Integer.parseInt(value);
}catch(NumberFormatException e)
{
System.out.println("Error in command");
continue; //continue back into loop
}
if(command.equals("add"))
{
if(myset.add(num))
{
System.out.println(num + " added to set");
System.out.println(myset.toString());
}
else
System.out.println("duplicate " + num);
}
else if(command.equals("del"))
{
if(myset.delete(num))
{
System.out.println(num + " deleted from set");
System.out.println(myset.toString());
}
else
System.out.println( num + " not in set");
}
else if(command.equals("exists"))
{
if(myset.exists(num))
System.out.println(num + " exists ");
else
System.out.println(num + " does not exist");
}
else
{
System.out.println("Invalid command");
}
}
}
}
output
Please enter commands add, del, exists or quit at the prompt
> add
Error in command
> add 4
4 added to set
4
> add 5
5 added to set
5 4
> add 6
6 added to set
6 5 4
> add 4
duplicate 4
> add 8
8 added to set
8 6 5 4
> add 5
duplicate 5
> del 9
9 not in set
> del 5
5 deleted from set
8 6 4
> exists 9
9 does not exist
> exists 4
4 exists
> quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.