Get startedGet started for free

Using a POJO

Here, a POJO class Movie has already been defined for you. Your task is to create and use an instance of Movie to hold data for the thriller "Jaws" - thus giving you a chance to see and practice how setters and getters work to encapsulate POJO data.

This exercise is part of the course

Data Types and Exceptions in Java

View Course

Exercise instructions

  • Create a instance of Movie and assign it to a variable jaws.
  • Set the Movie objects title field to "Jaws".
  • Set the Movie objects director field to "Speilberg".
  • Print out the Movie objects rating.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

public class Test2 {

	public static void main(String[] args) {
		// Create an instance of Movie
		Movie jaws = ____ ____();
		// Set the title to "Jaws"
		____.____(____);
		// Set the director to "Spielberg"
		____.____(____);
		jaws.setRating("PG");
		// Display the movie's rating
		System.out.println(____.____());
	}
}

public class Movie {
	private String title;
	private String director;
	private String rating;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDirector() {
		return director;
	}
	public void setDirector(String director) {
		this.director = director;
	}
	public String getRating() {
		return rating;
	}
	public void setRating(String rating) {
		this.rating = rating;
	}
}
Edit and Run Code