Creating a Java Movie Class with Encapsulation and Details Method
Java Encapsulatiion: Exercise-13 with Solution
Write a Java program to create a class called Movie with private instance variables title, director, and duration. Provide public getter and setter methods to access and modify these variables. Add a method called getMovieDetails() that returns a formatted string containing the movie details.
Sample Solution:
Java Code:
Movie.java
// Define the Movie class
public class Movie {
// Private instance variables
private String title;
private String director;
private int duration; // duration in minutes
// Public getter for the title variable
public String getTitle() {
return title;
}
// Public setter for the title variable
public void setTitle(String title) {
this.title = title;
}
// Public getter for the director variable
public String getDirector() {
return director;
}
// Public setter for the director variable
public void setDirector(String director) {
this.director = director;
}
// Public getter for the duration variable
public int getDuration() {
return duration;
}
// Public setter for the duration variable
public void setDuration(int duration) {
this.duration = duration;
}
// Method to get the movie details as a formatted string
public String getMovieDetails() {
return "Title: " + title + ", Director: " + director + ", Duration: " + duration + " minutes";
}
}
Main.java
public class Main {
// Main method to test the Movie class
public static void main(String[] args) {
// Create a new Movie object
Movie movie = new Movie();
// Set the title, director, and duration of the movie
movie.setTitle("Arrival");
movie.setDirector("Christopher Nolan");
movie.setDuration(146);
// Print the details of the movie
System.out.println(movie.getMovieDetails());
}
}
Output:
Title: Arrival, Director: Christopher Nolan, Duration: 146 minutes
Explanation:
- Private Instance Variables: The title, director, and duration variables are declared as private to ensure encapsulation.
- Public Getters and Setters: These methods provide controlled access to the private variables.
- getTitle(): Returns the title.
- setTitle(String title): Sets the title.
- getDirector(): Returns the director.
- setDirector(String director): Sets the director.
- getDuration(): Returns the duration.
- setDuration(int duration): Sets the duration.
- getMovieDetails Method: Returns a formatted string containing the movie details.
- Main Method: Tests the functionality of the Movie class by creating an instance, setting its properties, and printing the details.
Note on Encapsulation
Encapsulation works in the above exercise by:
- Hiding Data: The private instance variables title, director, and duration are not accessible directly from outside the class.
- Controlled Access: The public getter and setter methods provide controlled access to the private variables, allowing for validation and modification when necessary.
- Data Integrity: Encapsulation helps maintain data integrity by ensuring that the internal state of the object can only be changed through well-defined methods.
Java Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Creating a Java Account Class with Encapsulation and Transaction Methods.
Next: Creating a Java Product Class with Encapsulation and discount Method.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/encapsulation/java-encapsulation-exercise-13.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics