Help needed for Java 18-5. Change the Stack of movie objects in 18-4 into a Tree
ID: 3829535 • Letter: H
Question
Help needed for Java
18-5. Change the Stack of movie objects in 18-4 into a TreeSet of movie objects. Add extra choices to the user's picks so they can see what the next movie is at the beginning or end, and so they can watch what the next movie is at the beginning or end. Use methods to show the TreeSet is sorted and can not contain duplicate objects (including telling the user if they try to add a duplicate movie). Lab exercises 18-1, 18-3, 18-4 have the same setup, so they can be used as a guide (and some copy and paste). Lab exercise 18-3 has the movie18 class definition header file.
Add new cases for the following:
System.out.println("Enter 1 to add movie to treeset");
System.out.println("Enter 2 to see what next movie on treeset is");
System.out.println("Enter 3 to watch next movie on treeset");
System.out.println("Enter 4 to see what last movie on treeset");
System.out.println("Enter 5 to watch last movie on treeset");
System.out.println("Enter 6 to see treeset of movies");
In the switch, cases 2 and 3 would be - 2 - seeing what the first movie18 in the TreeSet is (and keeping it) and 3 - watching the first movie (so it would be removed from the treeset), and cases 4 and 5 would be similar - 4 - seeing and 5 - watching the last movie18 in the TreeSet.
here is the code that i have.
Exercise 18-1
This program will create a linked list of movies names
*/ package lists18;import java.util.LinkedList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Scanner;
import java.io.*;
public class ex181a
{
public static void main(String[] args) throws IOException
{
LinkedList<String> mlist = new LinkedList<String>();
int pick, year;
String name, m;
ListIterator li;
Scanner sc = new Scanner(System.in);
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name of movie (-1 to end): ");
name = br.readLine();
/* sentinel loop to add names to linked list mlist */
System.out.println(" Enter 1 to search for a movie in linked list");
System.out.println("Enter 2 to see linked list sorted ascending");
System.out.println("Enter 3 to see linked list sorted descending");
System.out.println("Enter 4 to see size of linked list");
System.out.print("Enter pick (-1 to end): ");
pick = sc.nextInt();
while (pick != -1)
{
switch(pick)
{
case 1:
/* input name, if in list, tell where movie name is found */
case 2:
/* sort list ascending order, use iterator to print sorted list */
case 3:
/* sort list descending order, use iterator to print sorted list */
case 4:
/* tell number of movies in list */
default:
System.out.println("Illegal choice");
}
System.out.println(" Enter 1 to search for a movie in linked list");
System.out.println("Enter 2 to see sorted linked list");
System.out.println("Enter 3 to see reversed linked list");
System.out.println("Enter 4 to see size of linked list");
System.out.print("Enter pick (-1 to end): ");
pick = sc.nextInt();
}
}
}
Exercise 18-3
This program will create a queue of movies
*/ package lists18;import java.util.Queue;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.Scanner;
import java.io.*;
public class ex183a
{
public static void main(String[] args) throws IOException
{
Queue<movie18> mlist = new LinkedList<movie18>();
movie18 m;
int pick, year;
String name;
Iterator li;
Scanner sc = new Scanner(System.in);
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
System.out.println("There are " + mlist.size() + " movies in queue");
System.out.println("Enter 1 to add movie to queue");
System.out.println("Enter 2 to see what next movie on queue is");
System.out.println("Enter 3 to watch next movie on queue");
System.out.println("Enter 6 to see queue of movies");
System.out.print("Enter pick (-1 to end): ");
pick = sc.nextInt();
while (pick != -1)
{
switch(pick)
{
case 1:
/* input two fields (name and year), create movei18 object, put into queue */
case 2:
/* if there are more movies in queue, tell just the name of what movie would be next
(but do not take it out of queue */
case 3:
/* if there are more movies in queue, take next movie out of queue and tell just
the name of the movie */
case 6:
/* use iterator to display all movies - name and year printed separately */
default:
System.out.println("Try again");
}
System.out.println(" There are " + mlist.size() + " movies in queue");
System.out.println("Enter 1 to add movie to queue");
System.out.println("Enter 2 to see what next movie on queue is");
System.out.println("Enter 3 to watch next movie on queue");
System.out.println("Enter 6 to see queue of movies");
System.out.print("Enter pick (-1 to end): ");
pick = sc.nextInt();
}
}
}
18-4. Change the Queue of movie objects in 18-3 into a Stack of movie objects. Use methods to show the last in first out of a Stack (including how you show the entire Stack of movies). It will be very similar to ex183b.java, with some of the methods changed to reflect the Stack methods instead of the Queue methods. Again use movie18.java included with lab exercise 18-3.
Explanation / Answer
Option 1: Make it implement Comparable<MyInt>:
Option 2: Supply a Comparator<MyInt> when constructing the TreeSet:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.