Getters And Setters

Lesson 31
Author : Afrixi
Last Updated : October, 2017


Table of Content

Code

Copypublic class Movie{
     public String title;
     private String rating;
     public int duration;

     public Movie(String title, String rating, int duration){
          this.title = title;
          this.setRating(rating);
          this.duration = duration;
     }

     public String getRating(){
          return this.rating;
     }

     public void setRating(String rating){
          if(rating.equals("G") || rating.equals("PG") || rating.equals("PG-13") || rating.equals("R") || rating.equals("NR")){
               this.rating = rating;
          } else {
               this.rating = "NR";
          }
     }
}