Java - Programming Language
This course covers the basics of programming in Java. Work your way through the videos and we'll teach you everything you need to know to start your programming journey!

Getters And Setters

Lesson 31
Author : Mike Dane
Last Updated : October, 2017


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";
          }
     }
}