Question
I do need help solving this problem Thanks /* This class encapsulates a list of user-defined items that should be done- a WORK list. *
I do need help solving this problem Thanks
/* This class encapsulates a list of user-defined items that should be done- a "WORK" list.
* Each item on the list is represented by a WORKitem object.
* The list is implemented by an array. The array is initialized in the constructor to an
* initial length. The initial array contains only NULL values.
* The methods in this class impement the functionality the program provides to a user
* for maintaining their WORK list.
*/
public class WORKList {
// private instance variable declarations: do not modify these.
private final int INITIAL_LEN = 5;// the initial length of the array.
private WORKitem[] WorkList;
//declare any other private instance variables here.
/* Constructor that initializes the WorkList array with the initial length.
* Any other instance variables are initialized as well.
*/
public WORKList(){
//WORK 1: Implement this method
}
/* Add the item passed in to the end of the list- the next available index in the array.
* Calls expandList if the current array is full.
* Suggestion: use instance variable to keep track of the index of the next available cell.
*/
public void addItem(WORKitem item){
//WORK 6: Implement this method
}
/* Remove the item at "position". Position is always one more than the index.
* Check if the array is empty and throw an Exception "Trying to remove from an empty list."
* Check if the index is in the range of the data on the array. If it is not, throw
* an Exception "Invalid position [position]", where [position] is the value passed in.
* If the position is valid, overwrite the value at that index with NULL and call reOrganizeList
* to maintain the contiguous order of the data in the array.
*/
public void removeItemAt(int position)throws Exception{
//WORK 9: Implement this method
}
/* Sets the value of the parameter "hours" into the WORKitem object located at the "position" passed in.
* Position is always one more than the index.
* Check if the array is empty and throw an Exception "Trying to edit an item from an empty list."
* Check if the index is in the range of the data on the array. If it is not, throw
* an Exception "Invalid position [position]", where [position] is the value passed in.
* If the position is valid, set the new hours into the object at the index.
*/
public void setNewHours(int position, double hours)throws Exception{
//WORK 7: Implement this method
}
/*
* Returns the list. Do not modify this method.
*/
public WORKitem[] getWorkList(){
return WorkList;
}
/* Returns the array and any other instance variables to their intial state.
*/
public void clearWorkList(){
//WORK 10: Implement this method
}
/* Returns a String representation of the current item list according to
* these specifications:
* Each item is on one line, position number first followed by one blank,
* followed by the item's toString method call, followed by a line break " ".
* For example:
*1 WORK item: fun, allotted time(hrs): 1.3, get opera tickets
*2 WORK item: study, allotted time(hrs): 2.5, work on the lab for ch9
*
* If no items are on the list the String returned is: "no items".
*/
public String getWorkListAsString(){
//WORK 4: Implement this method
return null;
}
/****** Private, "helper" method section ******/
/* Returns true if the item list contains no items, false otherwise.
*/
private boolean isEmpty(){
//WORK 2: Implement this method
return false;
}
/* A full item list is an array where all cells contain an item. That
* means there is no cell that contains NULL.
* This method returns true if all cells in the array contain a WORKitem
* object, false otherwise.
*/
private boolean isFull(){
//WORK 3: Implement this method
return false;
}
/* Creates a new array that is double the size of the current array, copies the data
* from that array to the new array, and assigns the new array to the WorkList instance variable.
* Note that the new array will contain the items from the previous array followed by NULL values.
*/
private void expandList(){
//WORK 5: Implement this method
}
/* Removes any "gaps" in the WorkList array by shifting all cell values to the left.
* A gap is a NULL value between WORKitem objects. Gaps can be created when an item
* is removed from the array. This method is called by removeItem.
*/
private void reOrganizeList(){
//WORK 8: Implement this method
}
}
-------------------------------------------------------------------------------
import java.util.Scanner;
/* This class runs a console interface between a user and
* an instance of an EventSchedule.
*/
public class WORKListMain {
public static void main(String[] args){
System.out.println("My WORK List");
Scanner scan = new Scanner(System.in);
WORKList WorkList = new WORKList();
// can be used to populate the list for testing purposes.
// getTestList(WorkList);
boolean keepGoing = true;
String userStr = "";
while(keepGoing) {
System.out.println("Main Menu:");
System.out.println("Enter A to add an item.");
System.out.println("Enter R to remove an item.");
System.out.println("Enter H to change the hours for an item.");
System.out.println("Enter P to print all item.");
System.out.println("Enter C to clear all items.");
System.out.println("Enter X to quit.");
System.out.println("");
userStr = scan.nextLine();
if (userStr.equalsIgnoreCase("A")){
System.out.println("Enter the category: ");
String cat = scan.nextLine();
System.out.println("Enter the description: ");
String desc = scan.nextLine();
System.out.println("Enter the hours allotted: ");
double hours = Double.parseDouble(scan.nextLine());
WorkList.addItem(new WORKitem(cat, desc, hours));
}
else if (userStr.equalsIgnoreCase("R")){
System.out.println("Enter the position of the item to be removed:");
int position = Integer.parseInt(scan.nextLine());
try{
WorkList.removeItemAt(position);
System.out.println("Item replaced.");
}
catch(Exception ex){
System.out.println("Item not replaced. "+ex.getMessage());
}
}
else if (userStr.equalsIgnoreCase("H")){
System.out.println("Enter the position of the item:");
int position = Integer.parseInt(scan.nextLine());
System.out.println("Enter the new hours allotted: ");
double hours = Double.parseDouble(scan.nextLine());
try{
WorkList.setNewHours(position, hours);
System.out.println("Hours were updated.");
}
catch(Exception ex){
System.out.println("Hours not not updated. "+ex.getMessage());
}
}
else if (userStr.equalsIgnoreCase("P")){
System.out.println("Your items: ");
System.out.println(WorkList.getWorkListAsString());
}
else if (userStr.equalsIgnoreCase("C")){
WorkList.clearWorkList();
System.out.println("Items cleared.");
}
else if(userStr.equalsIgnoreCase("X"))
keepGoing = false;
else
System.out.println("Unrecognized input.");
}
System.out.println("Bye for now.");
scan.close();
}
/*
*This method may be used for testing purposes to populate the WORK list.
*/
public static void getTestList(WORKList WorkList){
WorkList.addItem(new WORKitem("fun", "get opera tickets", 1.3));
WorkList.addItem(new WORKitem("study", "work on the lab for ch9", 2.5));
WorkList.addItem(new WORKitem("work", "late shift panera", 4.0));
WorkList.addItem(new WORKitem("chore", "feed cat", 0.1));
WorkList.addItem(new WORKitem("chore", "walk the cat?", 0.5));
}
}
------------------------------------------------------------------------------
/**
* This class encapsulates the data to represent a WORK item.
**/
public class WORKitem {
private String category;
private String description;
private double hours;
public WORKitem(String category, String description, double hours){
this.category = category;
this.description = description;
this.hours = hours;
}
public void setNewHours(double newHours){
hours = newHours;
}
public String toString(){
return "WORK item: "+category+", allotted time(hrs): "+hours+", "+description;
}
}
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