In: Computer Science
import java.util.LinkedList;
import java.util.ListIterator;
public class ProjectList
{
/** This is the main function for the program */
public static void main()
{
System.out.println("\n\tBegin ProjectList demo program");
// 1. TO DO: Describe your project (1 Pt)
System.out.println("Project: . . . . . . \n");
System.out.println("Create a LinkedList to store the
tasks");
// 2. TO DO: Declare a LinkedList of String objects called "tasks"
(1 Pt)
System.out.println("Add initial tasks into the list");
// 3. TO DO: Add initial list of four or more tasks (5 Pts)
System.out.println("\nDisplay Initial list: ");
printList(tasks);
System.out.println("Add a step to the front of the list");
// 4. TO DO: Add a task to the front of the list (2 Pts)
printList(tasks);
System.out.println("Create a ListIterator for the tasks");
// 5. TO DO: Declare a List iterator for the tasks (2 Pts)
System.out.println("\nIterate two or more steps forward and insert
one or more tasks");
// 6. TO DO: Iterate two or more steps forward and insert one or
more tasks (3 Pts)
printList(tasks);
System.out.println("Move forward one or more tasks and remove a
task");
// 7. TO DO: Iterate down one or more tasks and remove a task (3
Pts)
printList(tasks);
System.out.println("Move back one or more tasks and add some
tasks");
// 8. TO DO: Iterate back one or more tasks and add some tasks (3
Pts)
printList(tasks);
System.out.println("Add remaining task(s) to finish the
project");
// 9. TO DO: Add at least one remaining task(s) to finish the
project (3 Pts)
System.out.println("\nFinal list of elements:");
printList(tasks);
System.out.println("\tEnd LinkedList demo program\n");
}
/**
* This method displays the elements in the LinkedList
* It declares a LinkedList iterator for traversal
* @param tasks - reference to the LinkedList to be displayed
*/
private static void printList(LinkedList<String> tasks)
{
int number = 1;
ListIterator iterator = tasks.listIterator();
while (iterator.hasNext())
System.out.printf("%3d - %s\n", number++,
iterator.next().toString());
System.out.println();
}
}
import java.util.LinkedList;
import java.util.ListIterator;
public class ProjectList {
/** This is the main function for the program
*/
public static void main(String[] args)
{
System.out.println("\n\tBegin
ProjectList demo program");
// 1. Describe your project (1
Pt)
System.out.println("Project:
LinkedList demo program\n");
System.out.println("Create a
LinkedList to store the tasks");
// 2. Declare a LinkedList of
String objects called "tasks" (1 Pt)
LinkedList<String> tasks =
new LinkedList<String>();
System.out.println("Add initial
tasks into the list");
// 3. Add initial list of four or
more tasks (5 Pts)
tasks.add("Task 1");
tasks.add("Task 2");
tasks.add("Task 3");
tasks.add("Task 4");
System.out.println("\nDisplay
Initial list: ");
printList(tasks);
System.out.println("Add a step to
the front of the list");
// 4. Add a task to the front of
the list (2 Pts)
tasks.addFirst("Task 5");
printList(tasks);
System.out.println("Create a
ListIterator for the tasks");
// 5. Declare a List iterator for
the tasks (2 Pts)
ListIterator<String> iterator
= tasks.listIterator();
System.out.println("\nIterate two
or more steps forward and insert one or more tasks");
// 6. Iterate two or more steps
forward and insert one or more tasks (3 Pts)
iterator.next();
iterator.next();
iterator.add("Task 6");
printList(tasks);
System.out.println("Move forward
one or more tasks and remove a task");
// 7. Iterate down one or more
tasks and remove a task (3 Pts)
iterator.next();
iterator.remove();
printList(tasks);
System.out.println("Move back one
or more tasks and add some tasks");
// 8. Iterate back one or more
tasks and add some tasks (3 Pts)
iterator.previous();
iterator.add("Task 7");
iterator.add("Task 8");
printList(tasks);
System.out.println("Add remaining
task(s) to finish the project");
// 9. Add at least one remaining
task(s) to finish the project (3 Pts)
tasks.add("Task 9");
System.out.println("\nFinal list of
elements:");
printList(tasks);
System.out.println("\tEnd
LinkedList demo program\n");
}
/**
* This method displays the elements in the
LinkedList
* It declares a LinkedList iterator for
traversal
* @param tasks - reference to the LinkedList to be
displayed
*/
private static void printList(LinkedList<String>
tasks)
{
int number = 1;
ListIterator iterator =
tasks.listIterator();
while (iterator.hasNext())
System.out.printf("%3d - %s\n",
number++, iterator.next().toString());
System.out.println();
}
}
//end of program
Output: