gs99 (2) [Avatar] Offline
#1
Using Eclipse Kepler; Windows 7.

Book gina = new Book('Groovy in Action')
assert gina.getTitle() == 'Groovy in Action'
assert getTitleBackwards(gina) == 'noitcA ni yvoorG'
String getTitleBackwards(book) {
title = book.getTitle()
return title.reverse()
}

Get error on line: String getTitleBackwards(book) {
Groovy:Method definition not expected here.

//////////////////////////////////////
GrovyInActionBook.groovy:
package groovyproject3
class GroovyInActionBook {
static void main(def args) {

Book gina = new Book('Groovy in Action')

assert gina.getTitle() == 'Groovy in Action'
assert getTitleBackwards(gina) == 'noitcA ni yvoorG'

String getTitleBackwards(book) {
title = book.getTitle()
return title.reverse()
}
}
}
///////////////////////////////////////////
The Book.groovy:
package groovyproject3

class Book {
private String title

Book (String theTitle) {
title = theTitle
}

String getTitle(){
return title
}
}
There is no error on this getTitle function.

Message was edited by:
gs99
gs99 (2) [Avatar] Offline
#2
Re: Listing 2.3 Using the Book class from a script
This is perhaps better code layout:
package groovyproject3

class GroovyInActionBook {
static void main(args) {
Book gina = new Book('Groovy in Action')

assert gina.getTitle() == 'Groovy in Action'
assert getTitleBackwards(gina) == 'noitcA ni yvoorG'
}
String getTitleBackwards(book) {
title = book.getTitle()
return title.reverse()
}
}

but now I get
Caught: groovy.lang.MissingMethodException: No signature of method: static groovyproject3.GroovyInActionBook.getTitleBackwards() is applicable for argument types: (groovyproject3.Book) values: [groovyproject3.Book@2aece37d]
Possible solutions: getTitleBackwards(java.lang.Object)