Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I Need help writing the TODOList class. /* This class encapsulates a list of user-defined items that should be done- a TODO list. * Each

I Need help writing the TODOList class.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

/* This class encapsulates a list of user-defined items that should be done- a "TODO" list. * Each item on the list is represented by a String object. * The list is implemented by a String array. The array is initialized in the constructor to an * initial length that is passed to the constructor. The initial array contains only NULL values. * A user adds an item (a String) to the list by calling the addItem method, passing in a String * that represents the to-do item. * Thus, the array may have fewer items (Strings) than its length. * * For example, assume the list has an initial length of 5. It looks like this: * NULL, NULL, NULL, NULL, NULL * * Then, a user adds three items. It looks like this: * "eat lunch", "walk dog", "study Java", NULL, NULL * * The length of the list is 5, the number of items is 3. The NULL values are unoccupied cells. * The capacity of the array is its length. The size of the data stored in the array is less than * or equal to its capacity. * * If a user wants to add more items to a list that has no more NULL values, i.e. no more room, * the expandArray method is called to double the length of the toDoList. The original Strings * are in the same positions, but the new array has double the capacity- more room for adding items. */ public class TODOList { /* YOUR Instance variable declarations here. */

/* Constructor that initializes the initialLength variable to the value passed in. * It also initializes the toDoList with the initial length. * Any other instance variables may be initialized as well. */ public TODOList(int initialLen){ } /* Add the item passed in to the end of the list. * For example, if the toDoList list contained: "eat lunch", "walk dog", * the next item added, "study Java", would result in this list: * "eat lunch", "walk dog", "study Java" * Suggestion: use instance variable to keep track of the index of the next available cell. */ public void addItem(String itemStr){ } /* Overwrite the item at "position" to be the parameter "itemStr". * Note: position is a positive integer > 0 that has to be a valid position * in the toDoList. A valid position corresponds to an item stored in the * toDoList. For example, if this was the list: 1 walk the cat 2 order doughnuts 3 go to the gym 4 wash dishes * valid positions would be 1, 2, 3, 4. All other integers are invalid. * This method returns true if a valid position was passed in, false otherwise. */ public boolean replaceItemAt(String itemStr, int position){ return false; } /* Remove the last item in the toDoList. * For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java", * removing the last item would result in this list: * "eat lunch", "walk dog". * This method returns true if there is at least one item in the list, * false otherwise. */ public boolean removeLastItem(){ return false; } /* * Return a list that contains only the items that have been added. * This list does not contain any NULL values. * For example, if the toDoList list contained: "eat lunch", "walk dog", "study Java", NULL, NULL * This method would return this array: "eat lunch", "walk dog", "study Java" * If the toDoList does not contain any items, this method returns a String array with zero length. */ public String[] getToDoList(){ return null; } /* Remove all items from the list, resulting in an empty list. * The capacity of the list returns to the initial length. */ public void clearToDoList(){ } /* 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 String. * For example: 1 walk the cat 2 order doughnuts 3 go to the gym 4 wash dishes * If no items are on the list the String returned is: "no items". */ public String getToDoListAsString(){ return null; } /* Returns the number of items stored in the item list. */ public int getNumberOfItems(){ return 5000; } /* Returns true if the item list contains no items, false otherwise. */ public boolean isEmpty(){ return false; } /****** Private, "helper" method section ******/ /* Creates a new array that is double the size of the array passed in, copies the data * from that array to the new array, and returns the new array. * Note that the new array will contain the items from the previous array followed by NULL values. */ private String[] expandList(String[] inputList){ return null; } /* 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 String * object, false otherwise. */ private boolean isFull(){ return true; } }

-----------------------------------------------------------------------------------------

import java.util.Scanner; /* This class runs a console interface between a user and * an instance of an EventSchedule. */ public class TODOListMain { public static void main(String[] args){ System.out.println("TODO List"); Scanner scan = new Scanner(System.in); int initialSize = 5; TODOList toDoList = new TODOList(initialSize); boolean keepGoing = true; String userStr = ""; int position; while(keepGoing) { System.out.println("Main Menu:"); System.out.println("Enter A to add an item."); System.out.println("Enter R to replace an item."); System.out.println("Enter L to remove the last 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 item: "); userStr = scan.nextLine(); toDoList.addItem(userStr); } else if (userStr.equalsIgnoreCase("R")){ System.out.println("Enter the number of the item to be replaced:"); position = Integer.parseInt(scan.nextLine()); System.out.println("Enter the replacement item:"); userStr = scan.nextLine(); if(toDoList.replaceItemAt(userStr, position)) System.out.println("Item replaced."); else System.out.println("Item not replaced- invalid number."); } else if (userStr.equalsIgnoreCase("L")){ if(toDoList.removeLastItem()) System.out.println("Last item removed."); else System.out.println("Item not removed- list empty."); } else if (userStr.equalsIgnoreCase("P")){ System.out.println("Your items: "); System.out.println(toDoList.getToDoListAsString()); } else if (userStr.equalsIgnoreCase("C")){ toDoList.clearToDoList(); 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(); } }

---------------------------------------------------------------------------------

import org.junit.Assert;

import static org.junit.Assert.*;

import org.junit.Before;

import org.junit.Test;

import java.lang.reflect.*;

public class TODOListTest {

private TODOList toDoList;

/** Fixture initialization (common initialization

* for all tests). **/

@Before public void setUp() {

toDoList = new TODOList(4);

}

/** Test for required instance variable declarations. **/

@Test public void initialLengthNameTest() {

Field[] fields = TODOList.class.getDeclaredFields();

boolean wasDeclared = false;

for(int i=0;i

if(fields[i].getName().equals("initialLength")){

wasDeclared = true;

}

}

assertEquals("Test 1: The instance variable initialLength has not been declared.", true, wasDeclared);

}

@Test public void toDoListNameTest() {

Field[] fields = TODOList.class.getDeclaredFields();

boolean wasDeclared = false;

for(int i=0;i

if(fields[i].getName().equals("toDoList")){

wasDeclared = true;

}

}

assertEquals("Test 2: The instance variable toDoList has not been declared.", true, wasDeclared);

}

@Test public void numItemsNameTest() {

Field[] fields = TODOList.class.getDeclaredFields();

boolean wasDeclared = false;

for(int i=0;i

if(fields[i].getName().equals("numItems")){

wasDeclared = true;

}

}

assertEquals("Test 3: The instance variable numItems has not been declared.", true, wasDeclared);

}

/** Test for required instance variable initializations in constructor. **/

@Test public void initialLengthTypeTest() {

Field[] fields = TODOList.class.getDeclaredFields();

String varType = null;

for(int i=0;i

if(fields[i].getName().equals("initialLength")){

varType = fields[i].getType().getName();

}

}

assertEquals("Test 4: The instance variable initialLength has not been declared as an int.", "int", varType);

}

@Test public void toDoListTypeTest() {

Field[] fields = TODOList.class.getDeclaredFields();

String varType = null;

for(int i=0;i

if(fields[i].getName().equals("toDoList")){

varType = fields[i].getType().getName();

}

}

assertEquals("Test 5: The instance variable toDoList has not been declared as an int.", "[Ljava.lang.String;", varType);

}

/** Test that the list has been created. **/

@Test public void initListTest() {

String[] items = toDoList.getToDoList();

assertNotEquals("Test 6: The array is null- list may not have been initialized.", null, items);

}

/** Test the addItem method. **/

@Test public void addOneItemTest() {

toDoList.addItem("walk the cat");

String[] items = toDoList.getToDoList();

assertEquals("Test 7: The item was not added.", "walk the cat", items[0]);

}

/** Test the addItem method-length of array. **/

@Test public void addOneItemLenOneTest() {

toDoList.addItem("walk the cat");

String[] items = toDoList.getToDoList();

assertEquals("Test 8: Call to addItem- the list should contain one item.", 1, items.length);

}

/** Test the getNumberOfItems method on an empty list. **/

@Test public void initListSizeTest() {

int len = toDoList.getNumberOfItems();

assertEquals("Test 9: The getNumberOfItems method should return 0 on an empty list.", 0, len);

}

/** Test the isEmpty method on an empty list. **/

@Test public void initListIsEmptyTest() {

boolean empty = toDoList.isEmpty();

assertEquals("Test 10: The isEmpty method should return true on an empty list.", true, empty);

}

/** Test the getNumberOfItems method on a list with one item. **/

@Test public void oneItemListSizeTest() {

toDoList.addItem("walk the cat");

int len = toDoList.getNumberOfItems();

assertEquals("Test 11: The getNumberOfItems method should return 1.", 1, len);

}

/** Test the replaceItemAt method. **/

@Test public void replaceItemAtTest() {

toDoList.addItem("walk the cat");

toDoList.replaceItemAt("walk the dog", 1);

String[] items = toDoList.getToDoList();

assertEquals("Test 12: The item \"walk the cat\" at position 1 was not replaced with \"walk the dog\".", "walk the dog", items[0]);

}

/** Test the removeLastItem method. **/

@Test public void removeLastItemTest() {

toDoList.addItem("walk the cat");

toDoList.addItem("walk the dog");

toDoList.addItem("eat Halloween candy");

toDoList.addItem("make dentist appointment");

toDoList.removeLastItem();

String[] items = toDoList.getToDoList();

assertEquals("Test 13: The last item should be: \"eat Halloween candy\".", "eat Halloween candy", items[items.length-1]);

}

/** Test the clearToDoList method. **/

@Test public void clearItemsTest() {

toDoList.addItem("walk the cat");

toDoList.addItem("walk the dog");

toDoList.addItem("eat Halloween candy");

toDoList.addItem("make dentist appointment");

toDoList.clearToDoList();

String[] items = toDoList.getToDoList();

assertEquals("Test 14: After adding items then calling clearToDoList, the item list should be empty.", 0, items.length);

}

/** Test the getToDoListAsString method on an empty list. **/

@Test public void initListGetItemStrTest() {

String listStr = toDoList.getToDoListAsString();

assertEquals("Test 15: The getItemStr method should return \"no items\" on an empty list.", "no items", listStr.trim());

}

/* Test private method expandList on empty list of length 5. */

@Test public void expandListTest1()throws Exception {

Class classToCall = Class.forName("TODOList");

Method methodToExecute = classToCall.getDeclaredMethod("expandList", new Class[]{String[].class});

methodToExecute.setAccessible(true);

String[] args = new String[5];

Object expandedListObj = methodToExecute.invoke(toDoList, new Object[]{args});

String[] expandedList = (String[])expandedListObj;

int result = expandedList.length;

assertEquals("Test 16: The expandList method did not create an array of double the length of the array passed in.", 10, result);

}

/* Test private method isFull on default list. */

@Test public void isFullTest1()throws Exception {

Class classToCall = Class.forName("TODOList");

Method methodToExecute = classToCall.getDeclaredMethod("isFull");

methodToExecute.setAccessible(true);

Boolean boolObj = (Boolean)methodToExecute.invoke(toDoList);

boolean result = boolObj.booleanValue();

assertEquals("Test 17: The isFull method should return false on an array with NULL values.", false, result);

}

}

1 This class encapsulates a List of user-defined items that should be done- a "TODO" List 2 Each item on the List is represented by a String object. 3 The List is implemented by a String array. The array is initialized in the constructor to an 4 initial length that is passed to the constructor. The initial array contains only NULL values. 5 A user adds an item (a String) to the list by calling the addItem method, passing in a String 6 that represents the to-do item 7Thus, the array may have fewer items (Strings) than its length. 8 9 For example, assume the List has an initial length of 5. It looks like this: 10 NULL, NULL, NULL, NULL, NULL 12 * Then, a user adds three items. It looks like this: 13 "eat lunch, "walk dog", "study Java", NULL, NULL 15 The length of the List is 5, the number of items is 3. The NULL values are unoccupied cells 16 The capacity of the array is its length. The size of the data stored in the array is less than 17 or equal to its capacity 18 19 20 21 If a user wants to add more items to a list that has no more NULL values, i.e. no more room, the expandArray method is called to double the length of the toDoList. The original strings are in the same positions, but the new array has double the capacity- more room for adding items. 23 public class TODOList f YOUR Instance variable declarations here. / 25 26 27 Constructor that initializes the initialLength variable to the value passed in. It also initializes the toDoList with the initial length Any other instance variables may be initialized as well 29 30 31 32 public TODOList(int initialLen) 34 35 36 37 38 39 40 41 42 43 /Add the item passed in to the end of the List For example, if the toDoList List contained: "eat lunch", "walk dog", the next item added, "study Java", would result in this List: "eat lunch", "walk dog", "study Java" Suggestion: use instance variable to keep track of the index of the next available cell public void addItem(String itemStr) Overwrite the item at "position" to be the parameter "itemstr". Note: position is a positive integer that has to be a valid position 45 46 in the toDoList. A valid position corresponds to an item stored in the 48 49 50 toDoList. For example, if this was the List: 1 walk the cat 2 order doughnuts 3g0 to the gym 4 wash dishes 52 53 54 valid positions would be 1, 2, 3, 4. All other integers are invalid. This method returns true if a valid position was passed in, false otherwise boolean return false; public replaceItemAt (String itemStr, int position) 56 57 58 59 60 61 62 Remove the last item in the toDoList For example, if the toDoList List contained: "eat lunch", "walk dog", "study Java", removing the last item would result in this List: "eat lunch", "walk dog

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David J. Auer

3rd Edition

0131986252, 978-0131986251

More Books

Students also viewed these Databases questions

Question

What is contractual adjustment?

Answered: 1 week ago