Java Programming A. Objectives 1. Using arrays to store and process data (10 poi
ID: 3882833 • Letter: J
Question
Java Programming
A. Objectives
1. Using arrays to store and process data (10 points)
2. Use at least one array of objects (10 points)
3. Use sorting to arrange data (10 points)
4. Use methods to do separate and repetitive work. The main method should not have more than 20 lines of code (10 points)
5. Implement the project correctly (50 points)
10 points will be awarded for use of meaningful identifiers, consistent indentation, explanatory comments in your code and properly formatted output, including screen shots of execution.
B. Description
Netflix provides an online video streaming service. Users can browse and choose videos based on their preference; titles can also be sorted based on ratings and other attributes. For this project, we simulate a movie streaming service similar to Netflix, but on a much smaller scale. Each movie has the following attributes: Name, year, running time (minutes), genre, rating (on a scale of 10). The following needs to be done as part of the project:
1. Create a data set of 30 movies
2. Assign names to each of the 30 movies (you can use actual movie names, or Movie1, Movie2, ..., Movie30)
3. Assign a random year to each of the movies (the range of the year would be 1920 to 2017), such that there is no more than one movie in a year
4. Assign a random running time to each of the movies (the range would be 60 to 200 minutes)
5. Assign a genre to each movie choosing from one of the following: Comedy, Drama, Sci-Fi, Action, Documentary; make sure there are no more than 10 movies in any category
6. Assign a random rating to each movie (range 0.1 to 10.0)
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 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 running time, starting with the shortest
4. Sort the movies according to ratings, starting with the highest rating
5. Ask user for a genre, and display all the movies belonging to that specific genre
6. Search for a specific movie by name, and display all the details if the movies exists
7. Add a movie to the list of movies (ask the user for all the details)
8. Exit The program continues to execute until the user selects option to “Exit”. The user is allowed to add a maximum of 5 movies.
Explanation / Answer
Code to copy:
import java.util.Random;
import java.util.Scanner;
class Movie
{
String name,genre;
int year,time;
float rating;
public Movie ()
{
name="";
year=0;
time=0;
genre="";
rating=0;
}
public void SetDetails (String mname, int myear,
int mtime, String mgenre, float mrating)
{
name=mname;
year=myear;
time=mtime;
genre=mgenre;
rating=mrating;
}
public void ShowDetails()
{
System.out.println(" Movie Name = "+name);
System.out.println(" Year = "+year);
System.out.println(" Running time = "+time);
System.out.println(" Genre = "+genre);
System.out.println(" Rating = "+rating);
}
public int GetYear()
{
return year;
}
public int GetTime()
{
return time;
}
public float GetRating()
{
return rating;
}
public String GetGenre()
{
return genre;
}
public String GetName()
{
return name;
}
public void SetYear(int myear)
{
year=myear;
}
public void SetTime(int mtime)
{
time=mtime;
}
public void SetRating(float mrating)
{
rating=mrating;
}
public void SetGenre(String mgenre)
{
genre=mgenre;
}
public void SetName(String mname)
{
name=mname;
}
}
//Define the class test.
public class test
{
public static void Display(Movie m[],int size)
{
for(int i=0;i<size;i++)
{
System.out.println(m[i].GetName());
}
}
public static void DisplayByYear(Movie m[],int size)
{
int min=0;
for(int i=0;i<size;i++)
{
min=i;
for(int j=0;j<size;j++)
{
if(m[j].GetYear() < m[min].GetYear())
{
min=j;
}
}
System.out.println(m[min].GetName());
}
}
public static void SortByTime(Movie m[],int size)
{
int min=0;
for(int i=0;i<size;i++)
{
min=i;
for(int j=0;j<size;j++)
{
if(m[j].GetYear() < m[min].GetYear())
{
min=j;
}
}
Movie temp;
temp=m[i];
m[i]=m[min];
m[min]=temp;
}
}
public static void SortByRating(Movie m[],int size)
{
int min=0;
for(int i=0;i<size;i++)
{
min=i;
for(int j=0;j<size;j++)
{
if(m[j].GetYear() < m[min].GetYear())
{
min=j;
}
}
Movie temp;
temp=m[i];
m[i]=m[min];
m[min]=temp;
}
}
public static void SearchByGenre(Movie m[], int size)
{
int flag=0;
String genre;
Scanner sc=new Scanner(System.in);
System.out.print(" Enter a genre to display: ");
genre=sc.nextLine();
for(int i=0;i<size;i++)
{
if(genre.equals(m[i].GetGenre()))
{
flag=1;
System.out.println(m[i].GetName());
}
}
if(flag==0)
{
System.out.println(" No movies found");
}
}
public static void SearchByName(Movie m[],int size)
{
String name;
Scanner sc=new Scanner(System.in);
System.out.print(" Enter a name to search: ");
name=sc.nextLine();
int flag=0;
for(int i=0;i<size;i++)
{
if(name.equals(m[i].GetName()))
{
flag=1;
System.out.println(" Movie found.");
m[i].ShowDetails();
}
}
if(flag==0)
{
System.out.println(" Movie not found.");
}
}
public static int max=0;
public static void AddMovie(Movie m[], int size)
{
String name,genre;
int year,time;
float rating;
if(max==5)
{
System.out.println(" You have already"+
" entered 5 movies.");
return;
}
Scanner sc=new Scanner(System.in);
System.out.println(" Enter the name: ");
name=sc.nextLine();
System.out.println(" Enter the year: ");
year=sc.nextInt();
System.out.println(" Enter the running time: ");
time=sc.nextInt();
System.out.println(" Enter the genre: ");
genre=sc.nextLine();
System.out.println(" Enter the rating: ");
rating=sc.nextFloat();
m[size+max].SetDetails(name, year, time, genre, rating);
max=max+1;
System.out.println(" Movie Entered!");
}
public static void SetRandom(Movie m[],int size)
{
Random rand=new Random();
int year, time;
float rating;
String name,genre;
String[] option={"Comedy", "Drama", "Sci-Fi",
"Action", "Documentary"};
for(int i=0;i<size;i++)
{
year = rand.nextInt((2017-1920)+1)+1920;
time = rand.nextInt((200-60)+1)+60;
rating = rand.nextFloat()*10;
name = "movie"+(i+1);
genre = option[i%5];
m[i].SetYear(year);
m[i].SetTime(time);
m[i].SetRating(rating);
m[i].SetGenre(genre);
m[i].SetName(name);
}
}
public static void DisplayMenu()
{
System.out.println(" Mini Netflix ");
System.out.println("1. List all the movies.");
System.out.println
("2. Display the movies sorted according to year.");
System.out.println
("3. Sort the movies according to running time.");
System.out.println
("4. Sort the movies according to ratings.");
System.out.println
("5. Display all the movies belonging"
+" to a specific genre");
System.out.println
("6. Search for a specific movie by name.");
System.out.println
("7. Add a movie to the list of movies.");
System.out.println
("8. Exit The program.");
}
public static void operate(Movie m[], int size, int choice)
{
switch(choice)
{
case 1:
Display(m,size);
break;
case 2:
DisplayByYear(m,size);
break;
case 3:
SortByTime(m,size);
break;
case 4:
SortByRating(m,size);
break;
case 5:
SearchByGenre(m,size);
break;
case 6:
SearchByName(m,size);
break;
case 7:
AddMovie(m,size);
break;
case 8:
System.out.println(" Exiting...");
System.exit(0);
default:
System.out.println(" Invalid input!");
}
}
//Define the main() method.
public static void main(String[] args)
{
Movie m[]=new Movie[35];
int size=30;
int choice=0;
for(int i=0;i<35;i++)
{
m[i]=new Movie();
}
SetRandom(m,size);
Scanner input=new Scanner(System.in);
while(choice!=8)
{
DisplayMenu();
System.out.print(" Enter your choice : ");
choice=input.nextInt();
operate(m, size, choice);
}
}
}
Sample output:
Mini Netflix
1. List all the movies.
2. Display the movies sorted according to year.
3. Sort the movies according to running time.
4. Sort the movies according to ratings.
5. Display all the movies belonging to a specific genre
6. Search for a specific movie by name.
7. Add a movie to the list of movies.
8. Exit The program.
Enter your choice : 1
movie1
movie2
movie3
movie4
movie5
movie6
movie7
movie8
movie9
movie10
movie11
movie12
movie13
movie14
movie15
movie16
movie17
movie18
movie19
movie20
movie21
movie22
movie23
movie24
movie25
movie26
movie27
movie28
movie29
movie30
Mini Netflix
1. List all the movies.
2. Display the movies sorted according to year.
3. Sort the movies according to running time.
4. Sort the movies according to ratings.
5. Display all the movies belonging to a specific genre
6. Search for a specific movie by name.
7. Add a movie to the list of movies.
8. Exit The program.
Enter your choice : 6
Enter a name to search: movie6
Movie found.
Movie Name = movie6
Year = 1932
Running time = 141
Genre = Comedy
Rating = 4.5804424
Mini Netflix
1. List all the movies.
2. Display the movies sorted according to year.
3. Sort the movies according to running time.
4. Sort the movies according to ratings.
5. Display all the movies belonging to a specific genre
6. Search for a specific movie by name.
7. Add a movie to the list of movies.
8. Exit The program.
Enter your choice : 5
Enter a genre to display: Comedy
movie1
movie6
movie11
movie16
movie21
movie26
Mini Netflix
1. List all the movies.
2. Display the movies sorted according to year.
3. Sort the movies according to running time.
4. Sort the movies according to ratings.
5. Display all the movies belonging to a specific genre
6. Search for a specific movie by name.
7. Add a movie to the list of movies.
8. Exit The program.
Enter your choice : 8
Exiting...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.