Computer Applications

Define a class called MovieMagic, Constructor based program

  1. Define a class called MovieMagic with the following description:

Instance variables/Data members:

      int year            – to stores the year of release of the movie.

      String title       – to stores the title of the movie.

      float rating      – to stores the popularity rating of the movie. (minimum = 0.0 and maximum = 5.0)

Member methods:

  • MovieMagic( ) – default constructor to initialize data members to zero and string data members to “ ”.
  • void accept( ) – to input and store the year, title and rating.
  • void display( ) – to display the title of the movie and a message based on the rating as per the table below.

Rating                            Message to be displayed

0.0 to 2.0                        Flop

2.1 to 3.4                        Semi-Hit

3.5 to 4.5                        Hit

4.6 to 5.0                        Super-Hit

Write a main( ) method where user will create an object of the class and call all the member functions.

import java.io.*;
import java.util.*;

class MovieMagic
{
	int year;
	String title;
	float rating;

	MovieMagic( )                      //constructor
	{
		year = 0;
		title = " ";
		rating = 0.0;
	}

	void accept( )
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter Year =");
		year = sc.nextInt();
		System.out.println("Enter Title =");
		title = sc.nextLine();
		System.out.println("Enter rating =");
		rating = sc.nextFloat();
	}

	void display( )
	{
		System.out.println("Year = "+year);
		System.out.println("Title = "+title);
		System.out.println("Rating = "+rating);
		if (rating>=0.0 && rating<=2.0)
			System.out.println("Flop.");
		else if (rating>=2.1 && rating<=3.4)
			System.out.println("Semi-Hit.");
		else if (rating>=3.5 && rating<=4.5)
			System.out.println("Hit.");
		elseif (rating>=4.6 && rating<=5.0)
			System.out.println("Super-Hit.");
	}

	public static void main(String args[ ])
	{
		MovieMagic app = new MovieMagic( );
		app.accept( );
		app.display( );
	}
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top