Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Data Types and Exceptions in Java

Cursus bekijken

Oefeninstructies

  • 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.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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;
	}
}
Code bewerken en uitvoeren