Automate getter-setter generator for Java using Python

Encapsulation is defined as the wrapping up of data under a single unit. Encapsulation can be achieved by declaring all the variables in the class as private and writing public methods in the class to set and get the values of variables. These public methods are called getters and setters. In practice, it is quite a time consuming and repetitive process to write the getter-setters for all the variables in Java.
Here we are provided with a list of variable names with their data types, we have to print all the getter-setter methods for them while following the basic Java programming conventions.

Algorithm :

  1. Change the first character of every variable to uppercase.
  2. Add “get” in the beginning of each changed variable and store them in a list.
  3. Add “set” in the beginning of each changed variable and store them in a list.
  4. For every variable and its corresponding datatype, print the getter and setter methods using string concatinations.

Below is the implementation.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function to print getter and setter methods
# for variables according to Java
def print_getter_setter(variables, dataypes):
   
    # List to store getVariable
    getters = []
   
    # List to store setVariable
    setters = []
   
    # Iterate for every variable
    for var in variables:
   
        # Prepend "get" in every variable and change
        # the first character to uppercase
        getter = "get" + var[0].capitalize() + var[1:]
        getters.append(getter)
   
        # Prepend "set" in every variable and change
        # the first character to uppercase
        setter = "set" + var[0].capitalize() + var[1:]
        setters.append(setter)
   
       
    for i in range(len(variables)):
          
        # Print the getter method
        print("public " + datatypes[i] + " " + getters[i] +
              "() {\n\treturn " + variables[i] + ";\n}\n")
   
        # Print the setter method
        print("public void " + setters[i] + "(" + datatypes[i] +
              " " + variables[i] + ") {\n\tthis." + variables[i] +
              " = " + variables[i] + ";\n}\n")
  
# Driver function
if __name__=="__main__":
    # The list of variables
    variables = ["abc", "empId", "GFG", "x"]
   
    # And the list of the variables's corresponding
    # datatypes
    datatypes = ["int", "float", "double" , "String"]
   
    print_getter_setter(variables, datatypes)

chevron_right


Output :

public int getAbc() {
    return abc;
}

public void setAbc(int abc) {
    this.abc = abc;
}

public float getEmpId() {
    return empId;
}

public void setEmpId(float empId) {
    this.empId = empId;
}

public double getGFG() {
    return GFG;
}

public void setGFG(double GFG) {
    this.GFG = GFG;
}

public String getX() {
    return x;
}

public void setX(String x) {
    this.x = x;
}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.