Monday, November 18, 2024
Google search engine
HomeLanguagesAutomate getter-setter generator for Java using Python

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 concatenations.

Below is the implementation.
 

Python3




# Function to print getter and setter methods
# for variables according to Java
def print_getter_setter(variables, datatypes):
  
    # 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 corresponding
    # datatypes
    datatypes = ["int", "float", "double" , "String"]
  
    print_getter_setter(variables, datatypes)


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

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments