Skip to main content

Posts

Showing posts from February, 2020

Methods, procedures or encapsulation

Methods, procedures or encapsulation When creating a method, the word def is used in front of the method or the method gives a return type to clarify that is a method. There are three modifiers that can be used for a method: public, private, or protected.  The default modifier in Groovy is public, so if no modifier is given, then it will automatically be public. Parameters for methods in Groovy are handled the same way as they are in Java, the parameter names mus differ from each other. In Groovy, if no parameters are given inside the method, Groovy has default parameters that are passed in.  An example is def method(parameterA = 0, parameterB =0, parameterC=0) { } A main method in Groovy is as seen below. static void main(String[] args) { }  Groovy also has instance methods. These methods can be accessed by instantiating an object to call upon the object. There are optional modifiers that can be used on methods. These include static, final, or s...
Repeated action syntax (loops or recursion) While loops While loops in Groovy are executed in the manner they are in Java. The condition in the while loop is a boolean value that if true, makes the functions inside the while loop to execute. If the boolean is changed to false then the while loop is then terminated.  For Loops Similar to the syntax Java uses for for loops, groovy starts the for loop statement with a variable declaration, an expression and then incrementation.  For In Loops This is not a normal for loop. Instead, a for in loop takes a variable and then checks to see if the variable is in a certain range. For example if there was an array of numbers such as int [] arrary = [0,1,2,3,4,5,6,7,8,9], a for in loop could take a value, and check to iterate all of the given values that are inside the array, the loop would be set up in the way shown below. for ( int i in array) {        println "${i}" } So then the ou...
Conditionals Groovy has the same conditionals that are used in Java. There are if, else if, and else conditionals in Groovy. The syntax used in Java for if, else if, and else statements is the same as it is in java. The only difference below is that Groovy does not use semi colons. Groovy also allows assert statements to be used with the if, else if, and else statements. After a conditional statement is executed, variables can be set to other variables with the assert. This can be seen below. When users create a variable, the word def replaces the type of the variable.  Besides if, else if, and else statements, another type of conditional statement used in groovy are switch and cases. The value that the coder wishes to compare to all the other values goes inside the parameters for the switch. The value passed in the parameter is compared to all the cases.  The syntax for this is underneath the switch line, there are brackets that signify that the cases go in between....
        Above is a screenshot of  the basic arithmetic functions executed in Groovy. The arithmetic symbols used in Java is very similar to Groovy. A new symbol used in Groovy that is not used in Java is the dollar sign symbol. This is used in print statements. In java, when a person is coding a print line and they are concatenating words with a variable, quotations and a plus symbol is used. However, in Groovy, the whole print statement is inside quotation marks and a dollar sign is used to separate the variable with the words. This can be seen below. The types of variables in Groovy are ints, floats, long, short, bytes, char, booleans, strings and doubles. These are similar to Java. When creating a variable in Groovy, the word def is used in front of the new variable. For example, def a = 2. However, if a person is checking to see if a is equal to 2, they would write the line, assert a==2. Lists can be made in Groovy, simply by writing, def list = [0,...