CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Data Types and Exceptions in Java

Afficher le cours

Instructions

  • Create an 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 "Spielberg".
  • Print out the Movie objects rating.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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;
	}
}
Modifier et exécuter le code