Question

In: Computer Science

Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[]...

Write two classes, ToDoList and Driver

ToDoList should implement the following UML class diagram:

ToDoList
-list:String[]
-size:int
+ToDoList()
+add(String item):void
+remove(String item): boolean
+toString():String

add(String item) should add the item as the last element in the list, updating size.

remove(String item) should remove the item and return true, or, if the item was not in the list, return false.  
To remove the item, check every list element from 0 to size-1, and if that item is equal to the parameter using s1.equalsIgnoreCase(s2), loop from i+1 to size, assigning each element the value of the following element in the list, so list[j-1] = list[j]. The size of the list will be one less.

toString() should return a numbered list, with a "\n" after each list item.

HW22 should run as in the sample run below. Use static methods as needed to input the choice and the list item.

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: j

Enter a number: 6

1 through 4 only: 3

Empty list

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 1

Enter a list item and press enter: Mow the lawn

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 1

Enter a list item and press enter: Wash the car

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 1

Enter a list item and press enter: Clean the kitchen

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 3

1. Mow the lawn

2. Wash the car

3. Clean the kitchen

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 2

Enter a list item and press enter: clean the kitchen

clean the kitchen removed

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 3

1. Mow the lawn

2. Wash the car

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 2

Enter a list item and press enter: Write a program

Could not remove Write a program

1. add an item

2. remove an item

3. print the list

4. quit

Enter your choice: 4

add(String item) should add the item as the last element in the list, updating size.

remove(String item) should remove the item and return true, or, if the item was not in the list, return

Solutions

Expert Solution

//------------ ToDoList.java ------------
public class ToDoList {
  
   private String[] list;
  
   private int size;
  
   public ToDoList()
   {
       size = 0;
       list = new String[1];
   }
   public void add(String item)
   {
       list[size] = item;
       size++;
       if(size == list.length)
       {
           String[] newList = list.clone();
           list = new String[list.length * 2];
           System.arraycopy(newList,0,list,0,newList.length);
       }
   }
   public boolean remove(String item)
   {
       int i;
       boolean found = false;
       for(i = 0;i<size;i++)
       {
           if(list[i] != null && list[i].equalsIgnoreCase(item))
           {
               int j;
               for(j = i;j<size-1;j++)
               {
                   list[i] = list[j+1];
               }
               list[j]=null;
               found = true;
               break;
           }
       }
       return found;
   }
   @Override
   public String toString()
   {
       String res = "";
       for(int i = 0;i<list.length;i++)
       {
           if(list[i] != null)
           {
               res += "\n"+(i+1)+". "+list[i];
           }
       }
       return res;
   }
}

//------------ Driver.java ------------
import java.util.Scanner;

public class Driver {
  
   Scanner in = new Scanner(System.in);
   public int getChoice()
   {
       System.out.print("\n1. add an item\n" +
               "2. remove an item\n" +
               "3. print the list\n" +
               "4. quit\n"
               +"Enter your choice: ");
       try
       {
           return Integer.parseInt(in.nextLine().trim());
       }
       catch(Exception e)
       {
           return 0;
       }
   }
   public String getItem()
   {
       System.out.print("\nEnter a list item and press enter: ");
       return in.nextLine().trim();
   }
   public static void main(String[] args)
   {
       Driver driver = new Driver();
       ToDoList list = new ToDoList();
       int choice;
       do
       {
           choice = driver.getChoice();
           String item;
           switch(choice)
           {
               case 1:
                   item = driver.getItem();
                   if(item.equals(""))
                   {
                       System.out.println("\nList item must not empty and must not contain only whitespaces");
                       continue;
                   }
                   list.add(item);
                   break;
               case 2:
                   item = driver.getItem();
                   if(item.equals(""))
                   {
                       System.out.println("\nList item must not empty");
                       continue;
                   }
                   if(list.remove(item))
                   {
                       System.out.println(item+" removed");
                   }
                   else
                   {
                       System.out.println("Could not remove "+item);
                   }
                   break;
               case 3:
                   System.out.println((list+"").equals("") ? "\nEmpty list" : list);
                   break;
               case 4:
                   System.out.println("\nQuitting...");
                   break;
               default:
                   System.out.println("\nEnter choice 1 through 4 only");
                   break;
           }
          
       }while(choice != 4);
   }
}


Related Solutions

Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
Given the following UML class diagram, implement the class as described by the model. Use your...
Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method. +---------------------------------------------------+ | Polygon | +---------------------------------------------------+ | - side_count : int = 3 | | - side_length : double = 1.0 | +---------------------------------------------------+ | + Polygon() | | + Polygon(side_count : int) | | + Polygon(side_count : int, side_length : double) | | + getSides() : int | | + setSides(side_count : int) | |...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the...
Java Write a Payroll class as demonstrated in the following UML diagram. Member variables of the class are: NUM_EMPLOYEES: a constant integer variable to hold the number of employees. employeeId. An array of integers to hold employee identification numbers. hours. An array of integers to hold the number of hours worked by each employee payRate. An array of doubles to hold each employee’s hourly pay rate wages. An array of seven doubles to hold each employee’s gross wages The class...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Write two classes Class A and Class B in class A you should read from the...
Write two classes Class A and Class B in class A you should read from the keyboard a number and you should call a public method of the class B that will print on the screen whether the number that was read from the keyboard in class A is multiple of 7 and has the last digit 5.
Make a class diagram (static structure with classes, interfaces and their relationship). It should include classes,...
Make a class diagram (static structure with classes, interfaces and their relationship). It should include classes, major attributes, major methods, class associations and multiplicities. The details of the project and its functional requirements are: Develop a sentiment analysis system for product rating. It is an e-commerce web application. The main objective of this sentiment analysis system is to understand the hidden sentiments of customers in feedback and comments and analyze their product rating patterns. When registered customers (they can create...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.
In java Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.+------------------------------+| Circle |+------------------------------+| - radius : double = 1.0 |+------------------------------+| + Circle(radius : double) || + getRadius() : double || + setRadius(radius : double) || + area() : double || + perimeter() : double |+------------------------------+The following information might also be useful:Formula for area of a circle with radius r:       A = πr^2Formula for...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String...
A UML class diagram has the following description about a member of the class: + getHeaderField(name:String):String Which of the following is a correct declaration of the member in Java? 1 public String getHeaderField(String name) 2 public String getHeaderField; 3 public String getHeaderField(String) 4 public static String getHeaderField( name) ------------------------------------------------------------------------------------------------------------------------------------- Note: all answers are case sensitive. Inside a class, the declaration of a constructor looks like the following: public JButton(String text) 1. From the constructor, you infer that the name of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT