Question

In: Computer Science

IN JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List...

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
  • ListNode class
  • MovieList class
  • List interface
  • Driver class

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

  1. Private Movie m;
  2. Private ListNode next

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

Solutions

Expert Solution

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;

}

  

}


Related Solutions

N JAVA Objectives Practice Link list, List node, compareTo, user defined data type, interfaces Movie List...
N 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 ListNode class MovieList class List interface Driver class 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...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the...
JAVA PROGRAMMING Objectives JAVA Practice incremental development. Practice getting input from and printing output for the user. Write a program with conditional statements. Write a program with loops. Specifications Think of three countries you’d like to travel to and look up the conversion rate between dollars and the money used in those countries. (See http://www.ratesfx.com/ rates/rate-converter.html for conversion rates.) Write a program that will convert money from dollars into each of those three types of currency, or from one of...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;   ...
Given: #include <iostream> using std::cout; template <typename T> struct Node { T data; Node *link;       Node(T data=0, Node *p = nullptr) { //Note, this constructor combines both default and parameterized constructors. You may modify the contructor to your needs this->data = data;        link = p; } }; template <typename T> class linked_list { Node<T> *head,*current; public: //default constructor linked_list() { head = nullptr;//the head pointer current = nullptr;//acts as the tail of the list } //destructor...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and...
The following Assignment Questions are related to Java Collection Interfaces (Queue, List, Map, Set, etc), and Java IOStream. As mentioned at the beginning of our Collection’s class that the basis of java interfaces is mainly based on "TREES". However, one of these interfaces forms a different "TREE". Describe that particular interface. [1 Marks]. Why do we need to declare a type of object within each interface in the collection packages by "<E>" for example: <Integer, boolean, float, etc>”. [1 Marks]....
Graphical User Interfaces using java. Please provide proper commenting so that I understant what is going...
Graphical User Interfaces using java. Please provide proper commenting so that I understant what is going on in the program: Develop a simple tool for analyzing a segment of text that determines the number of words in that segment and the average word length. The application should have a single window with a scrolling text box (JTextArea) and an area to display the statistics. The statistics area should be a panel with a titled border, containing labeled fields that display...
Caesar Cipher in Java Problem? Objective Practice the cumulative sum and char data type Problem You...
Caesar Cipher in Java Problem? Objective Practice the cumulative sum and char data type Problem You want to create an app to encrypt the text messages that you send to your friends. Once your friend gets the message, the message should be decrypted so that your friend understands it. To implement this app Caesar cipher algorithm should be used. Caesar Cipher text is formed by rotating each letter by a given amount. For example, if you rotate the letter ‘A’...
JAVA: Write a program that prompts the user to input a type of medication and the...
JAVA: Write a program that prompts the user to input a type of medication and the output will be a list of side effects that can occur from that medication.
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT