Heres code for LLStringNode.java package movieList; public class LLStringNode {
ID: 3750194 • Letter: H
Question
Heres code for LLStringNode.java
package movieList;
public class LLStringNode {
private String info;
private LLStringNode next;
public LLStringNode(String info) {
this.info = info;
next = null;
}
public void setInfo(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setNext(LLStringNode next) {
this.next = next;
}
public LLStringNode getNext() {
return next;
}
}
Heres code for MovieListInterface.java
package movieList;
public interface MovieListInterface {
void add(String movie);
boolean onThisList(String movie);
}
MovieListinterface.java specifies a Java interface for an ADT that maintains a list of movie titles. This interface includes two operations: add(String movie) - add a movie title to the list onThisList(String movie) check if a given movie title is on the list Develop three implementations of this interface, each with a different underlying data structure: Array Linked list ArrayList from the java.util package Each implementation is a separate class, and each of these classes is to include a toString0 method to list out the movies, one title per line. The source code for the interface is provided in the movielist package in the attached Eclipse project export file for this assignment. The linked list implementation must use the LLStringNode.java class provided in the movieList package; do not use the LinkedList class from the java.util package. To earn full credit you must make use of appropriate ArrayList methods to efficiently develop the movie list ADT based on that data structure, along with an enhanced for loop to traverse the list of movie titles for the toString) method. In addition to the three ADT classes, create a Java program - i.e., a Java class with a main) method - to test each of the implementations. For each implementation: Instantiate a list object and add the titles of seven of your favorite movies Using System.out.println0, display the list of movies Use the onThislist) method to test whether Chariots of Fire is on your list, and report out the result and Use the onThisList0 method to test another title, and report out that result make sure to use a title that produces the opposite result from the first test.Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// MovieListArray.java
/**
* Implementation of a Movie list with Array as underlying collection You can
* refactor the class name according to your wish
*/
public class MovieListArray implements MovieListInterface {
private String movies[];
private int count; // current number of movies
private static final int MAX_SIZE = 50; // maximum number of movies
/**
* default constructor
*/
public MovieListArray() {
movies = new String[MAX_SIZE];
count = 0;
}
@Override
public void add(String movie) {
// adding to array if it is not full
if (count < movies.length) {
movies[count] = movie;
count++;
}
}
@Override
public boolean onThisList(String movie) {
// looping through the array, checking if the movie exists
for (int i = 0; i < count; i++) {
if (movies[i].equalsIgnoreCase(movie)) {
return true;
}
}
return false;
}
@Override
public String toString() {
// returning a String containing all movie titles in seperate lines
String data = "";
for (int i = 0; i < count; i++) {
data += movies[i] + " ";
}
return data;
}
}
// MovieListLL.java
/**
* Implementation of a Movie list with Linked List as underlying collection You
* can refactor the class name according to your wish
*/
public class MovieListLL implements MovieListInterface {
private LLStringNode head;
/**
* default constructor
*/
public MovieListLL() {
head = null;
}
@Override
public void add(String movie) {
// creating a LLStringNode object
LLStringNode node = new LLStringNode(movie);
if (head == null) {
// adding as the head
head = node;
} else {
// finding the last node and appending new node to the end
LLStringNode temp = head;
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(node);
}
}
@Override
public boolean onThisList(String movie) {
// iterating through the nodes and checking if the movie exists
LLStringNode temp = head;
while (temp != null) {
if (temp.getInfo().equalsIgnoreCase(movie)) {
return true;
}
temp = temp.getNext();
}
return false;
}
@Override
public String toString() {
// returning a String containing all movie titles in seperate lines
String data = "";
LLStringNode temp = head;
while (temp != null) {
data += temp.getInfo() + " ";
temp = temp.getNext();
}
return data;
}
}
// MovieListAL.java
import java.util.ArrayList;
/**
* Implementation of a Movie list with ArrayList as underlying collection You
* can refactor the class name according to your wish
*/
public class MovieListAL implements MovieListInterface {
private ArrayList<String> movies;
/**
* default constructor
*/
public MovieListAL() {
movies = new ArrayList<String>();
}
@Override
public void add(String movie) {
//adding to the list
movies.add(movie);
}
@Override
public boolean onThisList(String movie) {
// using contains method of array list to check if the movie exists
return movies.contains(movie);
}
@Override
public String toString() {
// returning a String containing all movie titles in seperate lines
String data = "";
for (String movie : movies) {
data += movie + " ";
}
return data;
}
}
//Test.java
public class Test {
public static void main(String[] args) {
/**
* Creating and testing MovieListArray object and methods
*/
MovieListArray movieArray=new MovieListArray();
movieArray.add("Alice in Wonderland");
movieArray.add("Chariots of Fire");
movieArray.add("Shawshank Redemption");
movieArray.add("Forrest Gump");
movieArray.add("Green Mile");
movieArray.add("Cast Away");
movieArray.add("Pulp Fiction");
System.out.println("Movie list using Arrays: ");
System.out.println(movieArray);
System.out.println("Contains 'Chariots of Fire'?: "+movieArray.onThisList("Chariots of Fire"));
System.out.println("Contains 'Inferno'?: "+movieArray.onThisList("Inferno"));
/**
* Creating and testing MovieListLL object and methods
*/
MovieListLL movieLinkedList=new MovieListLL();
movieLinkedList.add("Alice in Wonderland");
movieLinkedList.add("Chariots of Fire");
movieLinkedList.add("Shawshank Redemption");
movieLinkedList.add("Forrest Gump");
movieLinkedList.add("Green Mile");
movieLinkedList.add("Cast Away");
movieLinkedList.add("Pulp Fiction");
System.out.println("Movie list using Linked List: ");
System.out.println(movieLinkedList);
System.out.println("Contains 'Chariots of Fire'?: "+movieLinkedList.onThisList("Chariots of Fire"));
System.out.println("Contains 'Inferno'?: "+movieLinkedList.onThisList("Inferno"));
/**
* Creating and testing MovieListAL object and methods
*/
MovieListAL movieArrayList=new MovieListAL();
movieArrayList.add("Alice in Wonderland");
movieArrayList.add("Chariots of Fire");
movieArrayList.add("Shawshank Redemption");
movieArrayList.add("Forrest Gump");
movieArrayList.add("Green Mile");
movieArrayList.add("Cast Away");
movieArrayList.add("Pulp Fiction");
System.out.println("Movie list using Array List: ");
System.out.println(movieArrayList);
System.out.println("Contains 'Chariots of Fire'?: "+movieArrayList.onThisList("Chariots of Fire"));
System.out.println("Contains 'Inferno'?: "+movieArrayList.onThisList("Inferno"));
}
}
//OUTPUT
Movie list using Arrays:
Alice in Wonderland
Chariots of Fire
Shawshank Redemption
Forrest Gump
Green Mile
Cast Away
Pulp Fiction
Contains 'Chariots of Fire'?: true
Contains 'Inferno'?: false
Movie list using Linked List:
Alice in Wonderland
Chariots of Fire
Shawshank Redemption
Forrest Gump
Green Mile
Cast Away
Pulp Fiction
Contains 'Chariots of Fire'?: true
Contains 'Inferno'?: false
Movie list using Array List:
Alice in Wonderland
Chariots of Fire
Shawshank Redemption
Forrest Gump
Green Mile
Cast Away
Pulp Fiction
Contains 'Chariots of Fire'?: true
Contains 'Inferno'?: false