Arrays in Java Create a data set of 4 movies and assign a random number of Year
ID: 3883901 • Letter: A
Question
Arrays in Java
Create a data set of 4 movies and assign a random number of Year ( the range of the year would be 1920 to 2017), and Genre (choosing from Comedy, Drama, Sci-Fi and make sure there are no more than 2 movies in any category) for each of the movies. Use a separate class to create the movie objects. All the movie objects must be stored using an array. You can use additional arrays as necessary. The sorting code must be included as part of the project.
Create a menu based (no GUI) program with the following options:
1.List all the movies 2. Display the movies sorted according to year, starting with the oldest one 3. Sort the movies according to ratings, starting with the highest rating 4. Ask user for a genre, and display all the movies belonging to that specific genre 5. Search for a specific movie by name, and display all the details if the movies exists 6. Add a movie to the list of movies (ask the user for all the details) 7. Exit
Explanation / Answer
//This is your Movie Class
public class Movie {
String name;
int year;
String genre;
double rating;
public Movie(String name,int year,String genre,double rating)
{
this.name=name;
this.genre=genre;
this.year=year;
this.rating=rating;
}
}
//And This is your Main Driver Class
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
//Defining Movie type of array-it contains objects of type Movie
Movie[] mov=new Movie[4];
mov[0]=new Movie("Anabelle",2014,"Thriller",8.2);
mov[1]=new Movie("Dumb And Dumber",1996,"Comedy",7.1);
mov[2]=new Movie("Aatar",2012,"Science Fiction",8.9);
mov[3]=new Movie("Blade",1998,"Thriller",7.8);
//defining temporary variable of type Movie
Movie temp;
// Prob. 1-List all the movies
System.out.println("The Movies are as follows:");
int i=0;
while(i<4)
{
System.out.println(mov[i].name);//accessing name attribute of every movie object stored in the array
i++;
}
//2.Movies sorted according to year- oldest first
int n=4;//arrayLength
for(i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if((mov[j-1].year>mov[j].year))
{
//swap elements
temp=mov[j-1];
mov[j-1]=mov[j];
mov[j]=temp;
}
}
}
System.out.println("The Movies in yearby sorted order are as follows:");
i=0;
while(i<4)
{
System.out.println(mov[i].year+":"+mov[i].name);
i++;
}
//3.Movies sorted according to rating- highest first
n=4;//arrayLength
for(i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if((mov[j-1].rating)<mov[j].rating)
{
//swap elements
temp=mov[j-1];
mov[j-1]=mov[j];
mov[j]=temp;
}
}
}
System.out.println("The Movies in sorted order of rating are as follows:");
i=0;
while(i<4)
{
System.out.println(mov[i].rating+":"+mov[i].name);
i++;
}
//4.Movies selected according to genre
System.out.println("Enter the genre number you want to select:");
System.out.println("1:Action");
System.out.println("2:Thriller");
System.out.println("3:Comedy");
System.out.println("4:Science Fiction");
Scanner sc = new Scanner(System.in);
int numSelected=sc.nextInt();
i=0;
if(numSelected==1)
{
while(i<4)
{
if(mov[i].genre.equals("Action"))
System.out.println(mov[i].name);
i++;
}
}
else if(numSelected==2)
{
while(i<4)
{
if(mov[i].genre.equals("Thriller"))
System.out.println(mov[i].name);
i++;
}
}
else if(numSelected==3)
{
while(i<4)
{
if(mov[i].genre.equals("Comedy"))
System.out.println(mov[i].name);
i++;
}
}
else if(numSelected==4)
{
while(i<4)
{
if(mov[i].genre.equals("Science Fiction"))
System.out.println(mov[i].name);
i++;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.