0% found this document useful (0 votes)
9 views

Day 3 Scriptgroovy

The document contains code examples demonstrating inheritance and method overriding in Java classes. It defines classes x and y, with y extending x and overriding m1(). It then creates an instance of y and calls methods m1() and m2().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Day 3 Scriptgroovy

The document contains code examples demonstrating inheritance and method overriding in Java classes. It defines classes x and y, with y extending x and overriding m1(). It then creates an instance of y and calls methods m1() and m2().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

class x {

def m1() {
println "I am in m1 of x"
}
def m2() {
println "In m2 of x"
}
}
class y extends x{
def m1() {
println "I am in m1 of y"
super.m1()
}
}
y1=new y()
y1.m2()
y1.m1()

/*
class x {
def x() {
println "I am in constr of x"
}
def m1() {
println "I am in m1 of x"
}
def m1(a) {
println "I am in m1 of x with one arg"
}
def m1(a,b) {
println "I am in m1 of x with two arg"
}

}
x1=new x()
x1.m1()
x1.m1(10,20)

class x {
def m1()
{
println "I am in m1 of x"
}
}
class y extends x{
def m2()
{
println "I am in m2 of y"
}
}
//x1=new x()
//x1.m1()
y1=new y()
y1.m2()
y1.m1()

class Cust {
//customer application
def final cbname="SBI" //static variables
def cname
def caddr
def caccno
def cbal
def Cust(cname, caddr, caccno, cbal)
{
this.cname=cname
this.caddr=caddr //instance variables
this.caccno=caccno
this.cbal=cbal
}
def deposit(damt)
{
cbal=cbal+damt
}
def withdraw(wamt)
{
if (wamt<cbal)
{
cbal=cbal-wamt
}
else
{
println "Insufficient bal"
}
}
def balenq()
{
println cbal
}
def display()
{
println cname
println caddr
println caccno
println cbal
}
}
c1=new Cust("John","Hyd",1001,1000)
c1.balenq()
c1.deposit(2000)
c1.balenq()
c1.withdraw(5000)
c1.withdraw(1000)
c1.balenq()

c2=new Cust("siri","pune",1002,2000)
c2.balenq()
c2.display()

class x {
def x() {
println "I am in constr of x"
}
def f1() {
println "I am in f1 of x"
}

}
x1=new x()

class x {
def f1() {
println "I am in f1 of x"
}
def f2(a,b) {
println(a)
println(b)
def sum=a+b
println "sum is $sum"
}
}
x1=new x()
x1.f1()
x1.f2(10,20)

class Example {
def hello()
{
println "I am in hello method in the Example class"
}
}
x1=new Example()
x1.hello()

// GroovyScript.groovy
//Importing Java Class in Groovy
//java program need to compile first
import JavaClass
import GroovyModule
def javaInstance = new JavaClass()
javaInstance.printMessage()
def groovyInstance = new GroovyModule()
groovyInstance.greet()

num1=System.console().readLine "Enter a num1:"


num2=System.console().readLine "Enter a num2:"
num1=num1.toInteger()
num2=num2.toInteger()
try {
result=num1/num2
println result
//println abc
}
catch (ArithmeticException exp)
{
println "second value should not be zero"
}
catch (Exception exp)
{
println "Something went wrong"
println exp.getMessage()
}
finally {
println "bye"
}

num1=System.console().readLine "Enter a num1:"


num2=System.console().readLine "Enter a num2:"
num1=num1.toInteger()
num2=num2.toInteger()
try {
result=num1/num2
println result
}
catch (Exception exp)
{
println "Something went wrong"
println exp.getMessage()
}
println "bye"

num1=System.console().readLine "Enter a num1:"


num2=System.console().readLine "Enter a num2:"
num1=num1.toInteger()
num2=num2.toInteger()
try {
result=num1/num2
println result
}
catch (Exception exp)
{
println "Something went wrong, $exp"
}
println "bye"

import arithmatic as arith


s1=new arith().sum(10,20)
println "sum of 10 and 20 is: $s1"
s2=new arith().sub(100,20)
println s2
s3=new arith().mul(10,20)
println s3
s4=new arith().div(10,2)
println s4

import arithmatic
s1=new arithmatic().sum(10,20)
println s1
s2=new arithmatic().sub(100,20)
println s2
s3=new arithmatic().mul(10,20)
println s3
s4=new arithmatic().div(10,2)
println s4

//function to find avg marks


def find_avg_marks(name,marks){
println "Hi $name"
sum_of_marks=marks.sum() //list1=[10,20,30]
total_sub=marks.size()
avg_marks=sum_of_marks/total_sub
//println "your avg marks $avg_marks"
return avg_marks}
//calculate the grade
def compute_grade(avg_marks){
if (avg_marks>=80){
grade='A' }
else if (avg_marks>=60) {
grade='B'
}
else if (avg_marks>=50){
grade='C'}
else {
grade='F' }
return grade }
marks=[45,67,89,98,90,71]
avg_marks=find_avg_marks('Bob',marks)
println "your avg marks $avg_marks"
grade=compute_grade(avg_marks)
println "Your grade is: $grade"
println "#"*40
marks=[87,89,89,98,90,81]
avg_marks=find_avg_marks('Siri',marks)
println "your avg marks $avg_marks"
grade=compute_grade(avg_marks)
println "Your grade is: $grade"

def sum1(x,y)
{
total=x+y
return total
}
println sum1(10,20)
k=sum1(11,22)
println k

def k=200
i="groovy"
def f1()
{
println "I am in f1"
a=10
println a
def x=100
println x
println i
//println k
}

def f2()
{
println "I am in f2"
println a
//println x
}

f1()
f2()

println "outside"
println a
println k

def f1(a,b)
{
println "sum of $a and $b is: ${a + b}"
println "sub of $a and $b is: ${a - b}"
println "mul of $a and $b is: ${a * b}"
println "#"*30
}
f1(10,20)
f1(10,2)
f1(10,3)
f1(10,4)
f1(10,5)

a=10
b=20
println "sum of $a and $b is: ${a + b}"
println "sub of $a and $b is: ${a - b}"
println "mul of $a and $b is: ${a * b}"
println "#"*30
a=10
b=2
println "sum of $a and $b is: ${a + b}"
println "sub of $a and $b is: ${a - b}"
println "mul of $a and $b is: ${a * b}"
println "#"*30
a=10
b=3
println "sum of $a and $b is: ${a + b}"
println "sub of $a and $b is: ${a - b}"
println "mul of $a and $b is: ${a * b}"
println "#"*30
a=10
b=4
println "sum of $a and $b is: ${a + b}"
println "sub of $a and $b is: ${a - b}"
println "mul of $a and $b is: ${a * b}"
println "#"*30
a=10
b=5
println "sum of $a and $b is: ${a + b}"
println "sub of $a and $b is: ${a - b}"
println "mul of $a and $b is: ${a * b}"
println "#"*30

def f1(a,b)
{
println a
println b
}
f1(10,20)

def f1()
{
println "Hello functions"
println "welcome to functions"
}
f1()
f1()
f1()
f1()
f1()

println "Hello functions"


println "welcome to functions"
println "Hello functions"
println "welcome to functions"
println "Hello functions"
println "welcome to functions"
println "Hello functions"
println "welcome to functions"
println "Hello functions"
println "welcome to functions"

a=10
def k=20
println a
println k

file1=new File('test100.txt')
for ( i in file1.readLines())
{
//if (i =~ /ab+c/)
//if (i =~ /\w+\s\w+\s\w+/)
//if (i =~ /[6-9][0-9]{9}/)
//if (i =~ /^groovy/)
if (i =~ /groovy$/)
{
println i
}
}

file1=new File('test1.txt')
for ( i in file1.readLines())
{
if (i =~ /groovy/)
{
println i
}
}

file1=new File('test100.txt')
//file1.write("groovy\npython\n")
file1.append("groovy\npython\n")
file1=new File('test1.txt')
for ( i in file1.readLines())
{
println i
}
*/

//println "Hello groovy console"

You might also like