(Java) Create a movie class definition file containing the movie name and year.
ID: 3720180 • Letter: #
Question
(Java)
Create a movie class definition file containing the movie name and year. Now create a Queue of movie objects. Let the user pick whether they want to add a movie to the Queue, see what the next movie in the Queue would be, watch the next movie in the Queue, or see what the entire Queue of movies is. Use methods to show the first in first out of a Queue.
Then Change the Queue of movie objects 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).
Explanation / Answer
import java.util.Scanner;
class movies{
String name;
int year;
movies next;
movies()
{
name="test";
year=-100;
next=null;
}
movies(String n,int y){
name=n;
year=y;
next=null;
}
}
class queue{
movies head=null;
movies tail=null;
String check(String name,int movie){
Scanner sc=new Scanner(System.in);
System.out.println("Do u want to add "+name);
return sc.next();
}
void add_movie(String name,int year){
movies n=new movies(name,year);
String flag=check(name,year);
if(flag.charAt(0)=='y'){
if(head==null){
head=n;
tail=n;
}
else{
tail.next=n;
tail=n;
}
}
}
void view_all_movie(){
movies temp=head;
while(temp!=null){
System.out.println(temp.name+" "+temp.year);
temp=temp.next;
}
}
void watch_movie(){
if(head!=null){
System.out.println(head.name+" "+head.year);
head=head.next;
}
else{
System.out.println("No movie in queue");
}
}
}
class stack{
movies head=null;
String check(String name,int movie){
Scanner sc=new Scanner(System.in);
System.out.println("Do u want to add "+name);
return sc.next();
}
void add_movie(String name,int year){
movies n=new movies(name,year);
String flag=check(name,year);
if(flag.charAt(0)=='y'){
if(head==null){
head=n;
}
else{
movies temp=null;
while(head!=null){
movies node=new movies();
node.name=head.name;
node.year=head.year;
if(temp==null)temp=node;
else{
node.next=temp;
temp=node;
}
head=head.next;
}
n.next=temp;
temp=n;
while(temp!=null){
movies node=new movies();
node.name=temp.name;
node.year=temp.year;
if(head==null)head=node;
else{
node.next=head;
head=node;
}
temp=temp.next;
}
}
}
}
void view_all_movie(){
movies temp=head;
while(temp!=null){
System.out.println(temp.name+" "+temp.year);
temp=temp.next;
}
}
void watch_movie(){
if(head!=null){
System.out.println(head.name+" "+head.year);
head=head.next;
}
else{
System.out.println("No movie in stack");
}
}
}
class movie{
public static void main(String args[]){
queue s = new queue();
stack q=new stack();
s.add_movie("a",12);
s.add_movie("b",12);
s.add_movie("c",12);
s.view_all_movie();
q.add_movie("a",12);
q.add_movie("b",12);
q.add_movie("c",12);
q.add_movie("e",12);
q.view_all_movie();
q.watch_movie();
}
}
There are 3 classes movies, queue, stack and movie as tester class
class movies is used to store name,year and next movies object
class queue has three func to add new movie if user is interested and view all movies that are in queue and watch first movie
queue has two endpoints head, tail first movie to be watched is at head and last move is at tail
class stack has three func similar to queue but it has only one endpont jead so for insertion inversion we need two stacks primary stack is inverted and then ne movie is inserted in sec stack and then sec stack is inveretd to get a primary stack.
To run the code compile javac movie.java
java movie
press yes if u are intersted otherwise no to add movie to the queue
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.