Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
first commit
  • Loading branch information
frank committed Feb 15, 2022
0 parents commit 0634abc
Show file tree
Hide file tree
Showing 22 changed files with 905 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
notes.txt
test.txt
56 changes: 56 additions & 0 deletions README.md
@@ -0,0 +1,56 @@
## Install Groovy on Windows

1. Go here http://groovy-lang.org/download.html

2. Click Windows Installer and click next a bunch of times until it is installed.

---

## Install Groovy on Ubuntu 20.04

### Install Open JDK
Install OpenJDK 14 version
`sudo apt install openjdk-14-jdk*`
`sudo apt install openjdk-14-jre*`

edit your .bashrc file
`vi ~/.bashrc`
add the following 2 lines to the end of the file
```
export JAVA_HOME=/usr/lib/jvm/java-14-openjdk-amd64/
export PATH=$JAVA_HOME/bin:$PATH
```

Source .bashrc so changes take effect
`source ~/.bashrc`

### Install Groovy 3.x
`sudo snap install groovy --classic`

---

## Install Groovy on Mac
1. Update Java to at least Java 7 in the System Preferences Java Control Panel

2. Type in Terminal /usr/libexec/java_home -V
to get something like
1.7.0_55, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home

3. Type in terminal export JAVA_HOME=`/usr/libexec/java_home -v 1.7.0_55, x86_64`

4. Type in terminal java -version and make sure you have at least Java 7

5. Install HomeBrew at http://brew.sh/

6. Type in terminal brew install groovy

---

### Update Atom for Groovy
In Atom Open Command Palette -> Install Packages Themes -> Type language-groovy and install

---

## Run Groovy code
Groovy files have the extension `.groovy`. You can run a groovy file at the terminal with:
`groovy myfile.groovy`
5 changes: 5 additions & 0 deletions lesson_01_hello-world/hello_world.groovy
@@ -0,0 +1,5 @@
class GroovyHelloWorld{
static void main(String[] arg){
println("Hello World!!!");
}
}
97 changes: 97 additions & 0 deletions lesson_02_math/math.groovy
@@ -0,0 +1,97 @@
class GroovyMath{
static void main(String[] arg){

// ---------- MATH ----------
// Everything in Groovy is an object
// including numbers

// def is used when you define a variable
// Variables start with a letter and can
// contain numbers and _
// Variables are cynamically typed and can
// hold any value

def age = "Dog";
age = 40;

// The basic integer math operators
println("\nBasic Math");
println("5 + 4 = " + (5 + 4));
println("5 - 4 = " + (5 - 4));
println("5 * 4 = " + (5 * 4));
println("5 / 4 = " + (5.intdiv(4)));
println("5 % 4 = " + (5 % 4));

// Floating point math operators
println("\nFloat Math");
println("5.2 + 4.4 = " + (5.2.plus(4.4)));
println("5.2 - 4.4 = " + (5.2.minus(4.4)));
println("5.2 * 4.4 = " + (5.2.multiply(4.4)));
println("5.2 / 4.4 = " + (5.2 / 4.4));

// Order of operations
println("\nOrder of operations")
println("3 + 2 * 5 = " + (3 + 2 * 5));
println("(3 + 2) * 5 = " + ((3 + 2) * 5));

// Increment and decrement
println("\nIncrement or decrement")
println("age++ = " + (age++));
println("++age = " + (++age));
println("age-- = " + (age--));
println("--age = " + (--age));

// Largest values
println("\nLargest or smallest values")
println("Biggest Int " + Integer.MAX_VALUE);
println("Smallest Int " + Integer.MIN_VALUE);

println("Biggest Float " + Float.MAX_VALUE);
println("Smallest Float " + Float.MIN_VALUE);

println("Biggest Double " + Double.MAX_VALUE);
println("Smallest Double " + Double.MIN_VALUE);

// Decimal Accuracy
println("\nDecimal accuracy example");
println("1.1000000000000001111111111111111111111111111111111111 + 1.1000000000000001111111111111111111111111111111111111 "
+ (1.1000000000000001111111111111111111111111111111111111 + 1.1000000000000001111111111111111111111111111111111111));

// Math Functions
println("\nMore advanced math functions")
def randNum = 2.0;
println("Math.abs(-2.45) = " + (Math.abs(-2.45)));
println("Math.round(2.45) = " + (Math.round(2.45)));
println("randNum.pow(3) = " + (randNum.pow(3)));
println("3.0.equals(2.0) = " + (3.0.equals(2.0)));
println("randNum.equals(Float.NaN) = " + (randNum.equals(Float.NaN)));
println("Math.sqrt(9) = " + (Math.sqrt(9)));
println("Math.cbrt(27) = " + (Math.cbrt(27)));
println("Math.ceil(2.45) = " + (Math.ceil(2.45)));
println("Math.floor(2.45) = " + (Math.floor(2.45)));
println("Math.min(2,3) = " + (Math.min(2,3)));
println("Math.max(2,3) = " + (Math.max(2,3)));

// Number to the power of e
println("\nPower of e")
println("Math.log(2) = " + (Math.log(2)));

// Base 10 logarithm
println("\nlog base 10")
println("Math.log10(2) = " + (Math.log10(2)));

// Degrees and radians
println("\nConvert degrees to radians and vice versa")
println("Math.toDegrees(Math.PI) = " + (Math.toDegrees(Math.PI)));
println("Math.toRadians(90) = " + (Math.toRadians(90)));

// sin, cos, tan, asin, acos, atan, sinh, cosh, tanh
println("\nTrig stuff")
println("Math.sin(0.5 * Math.PI) = " + (Math.sin(0.5 * Math.PI)));

// Generate random value from 1 to 100
println("\nRandon numbers")
println("Math.abs(new Random().nextInt() % 100) + 1 = " + (Math.abs(new Random().nextInt() % 100) + 1));

}
}
77 changes: 77 additions & 0 deletions lesson_03_strings/strings.groovy
@@ -0,0 +1,77 @@
class GroovyStrings{
static void main(String[] arg){
// ---------- STRINGS ----------

def name = "Frank";

// A string surrounded by single quotes is taken literally
// but backslashed characters are recognized
println('I am ${name}\n');
println("I am $name\n");
println("I am ${name}\n");

// Triple quoted strings continue over many lines
def multString = '''I am
a string
that goes on
for many lines''';

println(multString);

// You can access a string by index
println("3rd Index of Name " + name[3]);
println("Index of r " + name.indexOf('r'));

// You can also get a slice
println("1st 3 Characters " + name[0..2]);

// Get specific Characters
println("Every Other Character " + name[0,2,4]);

// Get characters starting at index
println("Substring at 1 " + name.substring(1));

// Get characters at index up to another
println("Substring at 1 to 4 " + name.substring(1,4));

// Concatenate strings
println("My Name " + name);
println("My Name ".concat(name));

// Repeat a string
println("Repeat a string")
def repeatStr = "What I said is " * 2;
println(repeatStr);

// Check for equality
println("Frank == Frank : " + ('Frank'.equals('Frank')));
println("Frank == frank : " + ('Frank'.equalsIgnoreCase('frank')));

// Get length of string
println("Size or length " + repeatStr.length());

// Remove first occurance
println(repeatStr - "What");

// Split the string
println(repeatStr.split(' '));
println(repeatStr.toList());
println(repeatStr.split('a'))

// Replace all strings
println(repeatStr.replaceAll('I', 'she'));

// Uppercase and lowercase
println("Uppercase " + name.toUpperCase());
println("Lowercase " + name.toLowerCase());
println("Capitalize " + name.toLowerCase().capitalize())
// println("Capitalize " + capitalizeAll(repeatStr));

// <=> returns -1 if 1st string is before 2nd
// 1 if the opposite and 0 if equal
println("Ant <=> Banana " + ('Ant' <=> 'Banana'));
println("Banana <=> Ant " + ('Banana' <=> 'Ant'));
println("Ant <=> Ant " + ('Ant' <=> 'Ant'));

}
}
17 changes: 17 additions & 0 deletions lesson_04_output/output.groovy
@@ -0,0 +1,17 @@
class GroovyOutput{
static void main(String[] arg){
// ---------- OUTPUT ----------
// With double quotes we can insert variables
def randString = "Random";
println("A $randString string");

// You can do the same thing with printf
printf("A %s string \n", randString);

// Use multiple values
printf("%-10s %d %.2f %10s \n", ['Stuff', 10, 1.234, 'Random']);

// Single quotes are string literals
println('A $randString string');
}
}
19 changes: 19 additions & 0 deletions lesson_05_input/input.groovy
@@ -0,0 +1,19 @@
class GroovyInput{
static void main(String[] arg){
// ---------- INPUT ----------
print("Whats your name ");
def fName = System.console().readLine();
println("Hello " + fName);

// You must cast to the right value
// toInteger, toDouble
print("Enter a number ");
def num1 = System.console().readLine().toDouble();

print("Enter another number ");
def num2 = System.console().readLine().toDouble();

printf("%.2f + %.2f = %.2f \n", [num1, num2, (num1 + num2)]);

}
}
68 changes: 68 additions & 0 deletions lesson_06_lists/lists.groovy
@@ -0,0 +1,68 @@
class GroovyLists{
static void main(String[] arg){
// ---------- LISTS ----------
// Lists hold a list of objects with an index

def primes = [2,3,5,7,11,13];

println("My list is $primes")

// Get a value at an index
println("2nd Prime " + primes[1]);
println("3rd Prime " + primes.get(2));

// They can hold anything
def employee = ['Derek', 40, 6.25, [1,2,3]];

println("2nd Number " + employee[3][1]);

// Get the length
println("Length " + primes.size());

// Add an index
primes.add(17);

// Append to the right
primes<<19;
primes.add(23);

// Concatenate 2 Lists
def primes_cat = primes + [29,31]
println("Primes cat is $primes_cat")
primes + [29,31];
// println("Primes after adding [29,31] is '$primes'")


// Remove the last item
primes - [31];

// Check if empty
println("Is empty " + primes.isEmpty());

// Get 1st 3
println("1st 3 " + primes[0..2]);

println(primes);

// Get matches
println("Matches " + primes.intersect([2,3,7, 122]));

// Reverse
println("Reverse " + primes.reverse());

// Sorted
println("Sorted " + primes.sort());

// Pop last item
println("First " + primes.pop());
println("Primes after first item popped $primes")

//Remove last item
println("Last " + primes.removeLast())
println("Primes after last element removed $primes")

// Push an element to the front of the list
primes.push 2
println("Primes after pushing 2 to the front $primes")
}
}

0 comments on commit 0634abc

Please sign in to comment.