In: Computer Science
IN JAVA
Objectives
Practice Link list, List node, compareTo, user defined data type, interfaces
Movie List
Create a link list of the movies along with the rating, number of the people who watched the movie and the genre of the movie.
Required classes
Movie class implements Comparable
Attributes: movie’s name, genre, rating, number of people watched
Methods: constructor, getter, setter, equals, compreTo, toString
ListNode class
Attributes: each node has two attributes
Methods:
Constructors
public ListNode(Movie m): initializes the instance variable
m
public ListNode() : empty body
public ListNode(Movie m, ListNode next): initializes the instance variables m and next
getter methods
public Movie getMovie()
public ListNode getNext()
setter methods
public void setNext(ListNode next)
List interface
public void add(String name, String ganra, int star, int
people);
public void add(int index, String name, String genre, int star, int
people); public int indexOf(String movieName);
public void remove(String movieName);
public int size();
public String toString();
public Movie get(int position);
MovieList implements List :
all the methods in this class must use link list concept
public class MovieList implements List
{
private ListNode front;
public static int size = 0;
//constructor
public MovieList(){}
//add the movie to the end of the list
public void add(String name, String ganra, int star, int people){}
//adds the movie at the given index
public void add(int index, String name, String ganra, int star, int people){}
//returns the movie at the given index
public int indexOf(String movieName){}
//removes the movie from the list
public void remove(String movieName){}
//returns the size of the list
public int size(){}
//create a string from all the movies in the list
public String toString(){}
//returns the movie at the given index
public Movie get (int pos){} //returns the list of the movie with the give star
public String getMovie(int star){}
//return the movie with the max number of peopel watched.
public Movie mostWatched(){}
Driver class
Make sure that your code works with the following driver. Copy and paste this class to your file.
class Driver
{
public static void main (String [] args)
{
MovieList list = new MovieList();
list.add("Reservoir Dogs", "drama",5, 20000);
list.add("Airplane", "Funny", 3, 1200);
list.add("Doctor Zhivago","comedy", 4,23000);
list.add("The Deer Hunter", "Family", 3, 2345);
System.out.println("Here is the list of the movies\n");
System.out.println(list);
System.out.println("\nhere is the the movie that was most
watched");
System.out.println(list.mostWatched());
System.out.println("Here is the list of 5 stars ratings");
System.out.println(list.getMovie(5));
System.out.println("removing Reservoir movie");
list.remove("Reservior Dogs");
System.out.println(list);
System.out.println("Displaying the second movie in the
list");
System.out.println(list.get(1));
System.out.println("adding a movie at position 2");
list.add(2, "Up", "Carton",3,4500);
System.out.println(list);
int i = list.indexOf("Up");
System.out.println("The movie up is in the position " + i);
}
}
Sample output
Here is the list of the movies
Reservoir Dogs, drama, *****, 20000
Airplane, Funny, ***, 1200
Doctor Zhivago, comedy, ****, 23000
The Deer Hunter, Family, ***, 2345
here is the the movie that was most watched
Doctor Zhivago, comedy, ****, 23000
Here is the list of 5 stars ratings
Reservior Dogs, drama, *****, 20000
removing Reservoir movie
Airplane, Funny, ***, 1200
Doctor Zhivago, comedy, ****, 23000
The Deer Hunter, Family, ***, 2345
Displaying the second movie in the list
Doctor Zhivago, comedy, ****, 23000
adding a movie at position 2
Airplane, Funny, ***, 1200
Doctor Zhivago, comedy, ****, 23000
Up, Carton, ***, 4500
The Deer Hunter, Family, ***, 2345
The movie up is in the position 2
class Movie implements Comparable<Movie> {
private String name;
private String genre;
private int rating;
private int peopleWatched;
Movie(String name,String genre , int rating , int peopleWatched){
this.name=name;
this.genre=genre;
this.peopleWatched=peopleWatched;
this.rating=rating;
}
public String getGenre() {
return genre;
}
public String getName() {
return name;
}
public int getPeopleWatched() {
return peopleWatched;
}
public int getRating() {
return rating;
}
public void setGenre(String genre) {
this.genre = genre;
}
public void setName(String name) {
this.name = name;
}
public void setPeopleWatched(int peopleWatched) {
this.peopleWatched = peopleWatched;
}
public void setRating(int rating) {
this.rating = rating;
}
public String toString(){
String getstars="";
int rating =getRating();
for(int i=0;i< rating ;i++){
getstars+="*";
}
String str =getName()+","+getGenre()+","+getstars+","+getPeopleWatched()+"\n";
return str;
}
@Override
public int compareTo(Movie m) {
if(m.genre==genre && m.name==name && m.peopleWatched==peopleWatched && m.rating==rating ){
return 1;
}
return 0;
}
};
//////////////////// List.java //////////
public interface List {
public void add(String name,String genre,int star,int people);
public void remove(String movieName);
public String toString();
public Movie get(int position);
}
/////////////////////// ListNode.java ////////
public class ListNode {
private Movie m;
private ListNode next;
public ListNode(Movie m){
this.m=m;
}
public ListNode(){
}
public ListNode(Movie m, ListNode next){
this.m=m;
this.next=next;
}
public Movie getMovie() {
return m;
}
public ListNode getNext() {
return next;
}
public void setNext(ListNode next) {
this.next = next;
}
}
/////////// MovieList.java ////////////
public class Movielist implements List {
private ListNode front;
public static int size=0;
public Movielist(){}
public void add(String name,String genre,int star,int people){
ListNode newNode = new ListNode(new Movie(name, genre, star, people), null);
if(front == null){
front=newNode;
return;
}
ListNode temp =front;
while(temp.getNext()!=null){
temp=temp.getNext();
}
temp.setNext(newNode);
size++;
}
public void add(int index,String name,String genre,int star,int people){
ListNode newNode = new ListNode(new Movie(name, genre, star, people), null);
ListNode temp =front;
if(index==0){
newNode.setNext(front);
front=newNode;
return;
}
for(int i=0;i<index-1;i++){
temp=temp.getNext();
}
newNode.setNext(temp.getNext());
temp.setNext(newNode);
size++;
}
public int indexOf(String movieName){
int count=0;
ListNode temp=front;
while(temp!=null){
if(temp.getMovie().getName().equals(movieName)){
break;
}
temp=temp.getNext();
count++;
}
return count;
}
public void remove(String movieName){
ListNode temp=front;
ListNode temp1=temp;
if(temp.getMovie().getName().equals(movieName)){
front=temp.getNext();
temp1.setNext(null);
return;
}
temp=temp.getNext();
while(temp!=null){
if(temp.getMovie().getName().equals(movieName)){
temp1.setNext(temp.getNext());
temp.setNext(null);
break;
}
temp1=temp1.getNext();
temp=temp.getNext();
}
}
public int size(){
return size;
}
public String toString(){
String str="";
ListNode temp=front;
while(temp!=null){
str+= temp.getMovie().toString() ;
temp=temp.getNext();
}
return str;
}
public Movie get (int pos){
ListNode temp=front;
for(int i=0;i<pos;i++){
temp=temp.getNext();
}
return temp.getMovie();
}
public String getmovie(int star){
String str ="";
ListNode temp=front;
while(temp!=null){
if(temp.getMovie().getRating()==star){
str+=temp.getMovie().toString();
}
temp=temp.getNext();
}
return str;
}
public Movie mostWatched(){
int mostWatched=0;
Movie m=null;
ListNode temp=front;
while(temp!=null){
if(temp.getMovie().getPeopleWatched() > mostWatched){
mostWatched =temp.getMovie().getPeopleWatched();
m=temp.getMovie();
}
temp=temp.getNext();
}
return m;
}
}