use of getter setter in java

Solutions on MaxInterview for use of getter setter in java by the best coders in the world

showing results for - "use of getter setter in java"
Felipe
18 Mar 2019
1package gettersetterProgram; 
2public class Mensuration 
3{ 
4  public double x, y, r; 
5// Create getter method for each variable. 
6  public double getX() { 
7    return x; 
8  } 
9 public double getY() { 
10    return y; 
11} 
12public double getR() { 
13   return r; 
14} 
15// Create setter method for each variable in the class. 
16 public void setX(double x) { 
17    this.x = x; 
18 } 
19 public void setY(double y) { 
20    this.y = y; 
21 } 
22public void setR(double r) { 
23  this.r = r; 
24} 
25// Now write the logic of Rectangle and Circle. 
26  public double areaRec() { 
27    double area = x*y; 
28    return area; 
29  } 
30 public double perRec() { 
31    double per = 2*(x+y); 
32     return per; 
33  } 
34 public double areaCircle() { 
35    double area = 3.14*r*r; 
36     return area; 
37 } 
38 public double circumCircle() { 
39    double circumference = 2*3.14*r; 
40    return circumference; 
41  } 
42 } 
43public class MyTest { 
44public static void main(String[] args) 
45{ 
46  Mensuration mens; // Creating reference. 
47    men's = new Mensuration(); // Creating object.
48 
49// Set the values of the variables. 
50     mens.setX(20.5); 
51     mens.setY(30.5); 
52     mens.setR(12.5); 
53  
54  double areaRec = mens.areaRec(); 
55  double perRec = mens.perRec(); 
56  double areaCircle = mens.areaCircle(); 
57  double circumCircle = mens.circumCircle(); 
58
59  System.out.println("Area of rectangle: " +areaRec); 
60  System.out.println("Perimeter of rectangle: " +perRec); 
61  System.out.println("Area of circle: " +areaCircle); 
62  System.out.println("Circumference of circle: " +circumCircle); 
63 } 
64}
similar questions
queries leading to this page
use of getter setter in java