Define a class named movieMagic with the following description:
| Data Members | Purpose |
|---|---|
| int year | To store the year of release of a movie |
| String title | To store the title of the movie |
| float rating | To store the popularity rating of the movie (minimum rating=0.0 and maximum rating=5.0) |
| Member Methods | Purpose |
|---|---|
| movieMagic() | Default constructor to initialize numeric data members to 0 and String data member to "". |
| void accept() | To input and store year, title and rating |
| void display() | To display the title of the movie and a message based on the rating as per the table given below |
| Rating | Message to be displayed |
|---|---|
| 0.0 to 2.0 | Flop |
| 2.1 to 3.4 | Semi-Hit |
| 3.5 to 4.4 | Hit |
| 4.5 to 5.0 | Super-Hit |
Write a main method to create an object of the class and call the above member methods.
import java.util.Scanner;
public class movieMagic
{
private int year;
private String title;
private float rating;
public movieMagic() {
year = 0;
title = "";
rating = 0.0f;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter Title of Movie: ");
title = in.nextLine();
System.out.print("Enter Year of Movie: ");
year = in.nextInt();
System.out.print("Enter Rating of Movie: ");
rating = in.nextFloat();
}
public void display() {
String message = "Invalid Rating";
if (rating <= 2.0f)
message = "Flop";
else if (rating <= 3.4f)
message = "Semi-Hit";
else if (rating <= 4.4f)
message = "Hit";
else if (rating <= 5.0f)
message = "Super-Hit";
System.out.println(title);
System.out.println(message);
}
public static void main(String args[]) {
movieMagic obj = new movieMagic();
obj.accept();
obj.display();
}
}
Enter Title of Movie: 3 Idiots
Enter Year of Movie: 2009
Enter Rating of Movie: 5
3 Idiots
Super-Hit
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.