CPS 151/Spring 2022 Homework Assignment 3 (Party Planner 2.0) Purpose: In this assignment you are asked to re-implement

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

CPS 151/Spring 2022 Homework Assignment 3 (Party Planner 2.0) Purpose: In this assignment you are asked to re-implement

Post by answerhappygod »

Cps 151 Spring 2022 Homework Assignment 3 Party Planner 2 0 Purpose In This Assignment You Are Asked To Re Implement 1
Cps 151 Spring 2022 Homework Assignment 3 Party Planner 2 0 Purpose In This Assignment You Are Asked To Re Implement 1 (287.43 KiB) Viewed 17 times
import java.util.NoSuchElementException;
import java.util.Scanner;
/**@author Josh Neils*/
/**Apr 25, 2022 1:54:20 PM*/
public class AsGn3 {
private static SLND<String> guests,
victuals;
static int cmdCount = 0; // command
counter
private static final Scanner cin = new
Scanner(System.in);
public static void main(String[] args)
{
System.out.println("CPS 151
Assignment 3 by Josh Neils");
tellPurpose();
guests = new
SLND_LinkedList<>("Guest List");
victuals = new
SLND_LinkedList<>("Food and Beverage List");
mainMenu();
System.out.println("\nFinal
lists:\n");

System.out.println(guests);

System.out.println(victuals);
System.out.println("\nCPS 151
Assignment 3 complete");
} // end main
private static void mainMenu() {
char choice =
mainMenuGetChoice();
while (choice!= 'Q') {
if
(choice== 'F'){

maintain(victuals);
}
else
if(choice== 'G') {

maintain(guests);
}
else
{

System.out.println("Please renter a valid
input");
}
//
TODO: call "maintain" with appropriate argument
// or say
that the choice is invalid
choice
= mainMenuGetChoice();
} // end loop
} // end mainMenu
private static char mainMenuGetChoice()
{
cmdCount++;

System.out.println("\nMaintain: F)ood and beverage list, or
G)uest list, " +

"or Q)uit the program");
System.out.print(cmdCount +
". Your choice (F/G/Q): ");
return
cin.nextLine().toUpperCase().charAt(0);
} // end method
private static void
maintain(SLND<String> theList) {
System.out.println("\nWorking
with " + theList.name);
char choice =
subMenuGetChoice();
while (choice != 'Q')
{
try
{

if (choice=='A') {

add(theList);

}

else if (choice=='F') {

find(theList);

}

else if (choice=='P') {


theList.toString();

}

else if(choice=='R') {

remove(theList);

}

else {


System.out.println("Invalid command");

}
}

catch(Exception e){
}
choice
= subMenuGetChoice();
} // end loop
System.out.println("Ended
working with " + theList.name);
}
private static char subMenuGetChoice() {
cmdCount++;

System.out.println("\nChoices: A)dd, F)ind, P)rint, R)emove,
Q)uit");
System.out.print(cmdCount +
". Your choice (A/F/P/R/Q): ");
return
cin.nextLine().toUpperCase().charAt(0);
} // end method
private static void add(SLND<String>
theList) {
System.out.print("Add to " +
theList.name + "? ");
String item =
cin.nextLine();
theList.add(item); // the add
method may throw exception, handled within "maintain"
System.out.println("Add
succeeded");
} // end method
private static void find(SLND<String>
theList) {
System.out.println("Please
enter name:");
String
find=cin.nextLine();
if(theList.find(find))
{

System.out.println(find+" found");
}
else {

System.out.println(find+" was not found");
}
} // end method
private static void remove(SLND<String>
theList) {
System.out.println("Remove
from"+theList.name+"?");
String
remove=cin.nextLine();//variable declaration
if(theList.find(remove))
{

theList.remove(remove); //removes the name from the list when
true

System.out.println(remove+" Has been removed from the
list");//clarifies String is in Arraylist
}
else {

System.out.println("Error:"+remove+" does not exist in
"+theList.name);
}
} // end method
private static void tellPurpose() {
System.out.println("Party
Planner: This program lets you maintain" +

"\n\ta guest list and a food/beverage
list");
} // end method
} // end class
// ----------------- abstract generic class SLND (Sorted List No
Duplicates)
// No changes needed here
abstract class SLND<E extends Comparable<E>> {
public final String name;
public SLND(String name) {
this.name = name;
}
public abstract void add(E item);
public abstract void remove(E item);
public abstract boolean find(E item);
public abstract String toString();
} // end class
// ----------------- generic class Node (used to create singly
linked list)
// No changes needed here
// The fields (instance variables) are public
class Node<T> {
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
// ----------------- generic class SLND_LinkedList
class SLND_LinkedList<E extends Comparable<E>> extends
SLND<E> {
// fields of Node are public, but making head
private does data hiding
private Node<E> head;
public SLND_LinkedList(String name) {
super(name);
head = null;
} // Constructor
public void add(E item) throws
IllegalStateException {
// make the new node to
insert into list
Node<E> newNode = new
Node<>(item);
// first see if the
list is empty
if (head == null)
{
head =
newNode;
} else if
(item.compareTo(head.data) < 0) {

newNode.next = head;
head =
newNode;
} else
if(find(item)==true) {
throw new
IllegalStateException("The"+item+" already exists in the
Bag");

}

} // end methodod
public void remove(E item) throws
NoSuchElementException {
if (item == null)
{
throw
new NullPointerException();
}
if (head == null)
{
throw
new NoSuchElementException();
}
Node<E>
temp = head, prev = null;
if (temp != null
&& item.equals(temp.data)) {
head =
temp.next;
}
while (temp != null
&& (!item.equals(temp.data))) {
prev =
temp;
temp =
temp.next;
}
if (temp == null)
return;
prev.next =
temp.next;
} // end method
public boolean find(E item) {//This method is
okay
Node<E> ptr =
head;
while (ptr != null) {
if
(item.equals(ptr.data))

return true;
ptr =
ptr.next;
}
return false;
} // end method
public String toString() {//This Is
okay
// TODO: complete this
method, may need local variables
Node<E> cur = head,
prev = null;

System.out.println(name+"\n------\n");
while (cur != null) {

System.out.println(cur.data);
cur =
cur.next;
}
if (cur == null) {
return
"";
}
return "";
} // end method
} // end class
Please follow to these Guidlines listed above
CPS 151/Spring 2022 Homework Assignment 3 (Party Planner 2.0) Purpose: In this assignment you are asked to re-implement the Party Planner application with the following changes: The sorted list abstract class is to be implemented using a linked list for storing the list items (instead of using the ArrayList class from Java library). The add and remove methods indicate failure using the Exception mechanism (instead of returning false on failure), so these SLND methods are now declared as void methods; the find method remaining Boolean. The overall functionality of the application remains the same. Objectives (In addition to version 1.0 objectives): a) try-catch mechanism, b) Linked List mechanism for storing items in a sorted list (no duplicates). The client application modification You can retain most of the version 1 code. The two user errors that are possible are: a) attempt to remove an item not in the list, and b) attempt to add an item already in the list. In version 1, the SLND class methods add(item) and remove(item) return false to indicate failure. In version 2, the class methods would throw an exception. The thrown exception needs to be caught in the client application, which can recover from the error by simply letting the user make another choice from the list operations menu. Do that by adding a try block around the decision mechanism within the loop in the maintain(theList) method. Follow the try block by a catch block with a parameter of type Exception, so this one catch clause can catch both types of exception that may be thrown. The catch clause just prints the message from the Exception object to let the user know what happened. The maintain method's loop keeps the user within the menu system so the user can just make another choice. See the comments in the startup code. The client side methods add(theList) and remove theList) methods need some modification, as the SLND methods they call are not Boolean. They should just call the class's instance methods assuming the method calls will be successful; and with that expectation, print a success message after calling the SLND instance method. If the called SLND method throws an exception then control will revert to the catch block in maintain, and the success message will not be printed (but the error message will be in the catch block). The generic abstract class SLND<E> (Sorted List with No Duplicates) This class has one instance variable (intended for the client class to create a name for the list object public final). This allows the client code to inform the user which list is being worked on and also print the list's name when the list content is displayed. The class has 4 methods and all are declared abstract. See the design in the startup code file. Note the changes in the return types of the add and remove methods as already discussed. Completing the Client Code Similar to version 1. Implementing the abstract class In this version, the list items are stored in Nodes and each Node contains a reference to the next Node in the list. The list items are in strictly increasing order and no duplicate items are allowed. There is a single instance variable declared in the implementation: a reference to the first Node (the head Node) which is null when the list is empty. In the methods you will need to use local variables for Node references. • To add a new item you must first find out the correct Node before which the new Node (containing the new item) should be added. Starting with the first node skip all nodes containing smaller list items or if you reach the end of list, (in this process you can find out if the value already exists, in which case the method should throw an Page 1 of 2 2022-04-17
IllegalStateException). But if the new item can be added, then create a Node containing that item and link it correctly into the existing list. • To find a value you can just use the technique demonstrated in the sample class IntLinkedList posted on Isidore. • Removing a value can also be done similar to the technique shown in that sample class. If the value is not present a NoSuchElementException should be thrown. The toString method can be implemented by a creating a string containing the name of the list concatenated with the list items. Add the end-of-line ('\n') character after each string, so printing the string would show the list name followed by the item names, one per line of output. Add a line of hyphens (of the right length) after the name of the list (to set it off from the item names in the list), as in version 1. Beware! Since the SLND abstract class and its implementation are generic, the standard comparison operators cannot be used. Instead you must use the .compareTo () method that is declared to exist for the generic type parameter. What to submit Submit a Word document with the complete code and screen shots showing what your program can do. You may need to submit several screen shots to show that everything works the way they are supposed to. Sample Output Sample output will be posted later. Do not submit your assignment until this is posted and you have had a chance to compare your results with the sample.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply