Movie Class in Java
Posted by Samath
Last Updated: January 23, 2015
  13068

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());
    }   
}