Movie Class in Java
Posted by Samath
Last Updated: January 20, 2024

Implement a class named Movie. For the purpose of this exercise, a Movie should have properties representing a title and a year in which the movie was released. The year should be stored as an int, and the title as a String.

Supply two constructors -- one which takes just a title, and one which takes a title and a yearReleased. You should also write the methods getTitle(), getReleaseDate(), changeTitle(String newTitle) and setReleaseDate(int year).

Once you have implemented your Movie class, write a separate class, TestMovie, that includes a main method that tests all your public methods. You should be sure to test *all* the methods, including both constructors.

Movie:

public class Movie 
{
    private String title;
    private int year; 
    
    public Movie(String t)
    {
        title = t;
    }
    
    public Movie(String t, int yearReleased)
    {
        title = t;
        year = yearReleased;
    }
    
    public String getTitle()
    {
        return title;
    }
    
    public int getReleaseDate()
    {
        return year;
    }
    
    public void changeTitle(String newTitle)
    {
        title = newTitle;
    }
    
    public void setReleaseDate(int yr)
    {
        year = yr;
    }
}

 

TestMovie:

public class TestMovie {
    public static void main(String[] args) {
        
        Movie m1 = new Movie("Horrible Bosses 2");
        Movie m2 = new Movie("Mortdecai",2015);
        
        System.out.println("Title: " + m2.getTitle());
        System.out.println("Year: " + m2.getReleaseDate());
        m2.changeTitle("X-Men: Days of Future Past");
        m2.setReleaseDate(2014);
        System.out.println("Title: " + m2.getTitle());
        System.out.println("Year: " + m2.getReleaseDate());
    }   
}

 

Related Content
Movie Class in C++
Movie Class in C++
Samath | Jan 20, 2024
Simple Data Class using C++
Simple Data Class using C++
Samath | Jan 07, 2021
Account Class in C++
Account Class in C++
Samath | Jan 07, 2021
Car Class using C++
Car Class using C++
Samath | Jan 07, 2021
Circle Class in C++
Circle Class in C++
Samath | Jan 07, 2021
Invoice class using Java
Invoice class using Java
Samath | Jan 17, 2024
Rectangle Class in C++
Rectangle Class in C++
Samath | Jan 17, 2024
Student Class in Java
Student Class in Java
Samath | Jan 20, 2024
Glossary Class using Java
Glossary Class using Java
Samath | Jan 20, 2024
Vehicle Class using Java
Vehicle Class using Java
Samath | Jan 18, 2024
Phone Class using Java
Phone Class using Java
Samath | Jan 17, 2024
Airplane Class in Java
Airplane Class in Java
Samath | Jan 17, 2024