at-m42:lecture4
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at-m42:lecture4 [2009/04/09 18:32] – eechris | at-m42:lecture4 [2011/01/14 12:45] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ~~SLIDESHOW~~ | ||
+ | ====== Simple IO and a Case Study ====== | ||
+ | |||
+ | Simple input and output and introduction of the case study. | ||
+ | |||
+ | * [[#Simple IO]] | ||
+ | * [[#Case Study: An Adventure Game|Case Study]] | ||
+ | |||
+ | The slides and notes in this presentation are adapted from //Groovy Programming// | ||
+ | |||
+ | An index to the source code for all the examples in this lecture is [[/ | ||
+ | |||
+ | ===== Simple IO ===== | ||
+ | |||
+ | Console input and output is not part of the Groovy language. Nonetheless, | ||
+ | * to produce text output in Groovy we use: | ||
+ | |||
+ | print xxx | ||
+ | println xxx | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The methods '' | ||
+ | |||
+ | ===== Simple output ===== | ||
+ | |||
+ | <code groovy 1 | Example 1: Simple output (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | Example 1 demonstrates how to print a simple '' | ||
+ | |||
+ | When the groovy script is executed, the output is: | ||
+ | |||
+ | My name is Chris. | ||
+ | This is my first program. | ||
+ | Isn't this fun! | ||
+ | |||
+ | Lines 2 and 3 are responsible for the first line of output. Method '' | ||
+ | |||
+ | ===== Printing numbers ===== | ||
+ | |||
+ | <code groovy 1 | Example 2: Printing numbers (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | From [[lecture2# | ||
+ | |||
+ | The output from running this script is: | ||
+ | |||
+ | My age is: 49 | ||
+ | My age is: 49 | ||
+ | |||
+ | ===== Printing lists and maps ===== | ||
+ | |||
+ | <code groovy 1 | Example 3: Printing lists and maps (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | We can also print the contents of a '' | ||
+ | |||
+ | The output is: | ||
+ | |||
+ | |||
+ | numbers: [11, 12, 13, 14] | ||
+ | staff telephones: [Chris: | ||
+ | |||
+ | ===== Formatted output ===== | ||
+ | |||
+ | Formatted output is achieved with the '' | ||
+ | <code groovy> | ||
+ | printf(String format, List values) | ||
+ | </ | ||
+ | Example | ||
+ | <code groovy l> | ||
+ | //Simple formatted output | ||
+ | printf(' | ||
+ | printf(' | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Two examples of '' | ||
+ | |||
+ | The expression on line 2 is equivalent to '' | ||
+ | |||
+ | ===== Conversion specifications ===== | ||
+ | |||
+ | <code groovy 1 | Example 4: Formatted output (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | In these examples, we include a list of values. The format string then includes conversion specifications for each of the values. The conversion specifications are introduced by the percent (%) character. In the first example (line 6), '' | ||
+ | |||
+ | The output is: | ||
+ | |||
+ | The sum of 10 and 15 is 25 | ||
+ | 56.780000 from 1.234000 gives -55.546000 | ||
+ | |||
+ | Lines 14--16 illustrates using the '' | ||
+ | |||
+ | The output is: | ||
+ | |||
+ | [Hello there] | ||
+ | [ Hello there] | ||
+ | [hello there ] | ||
+ | |||
+ | A more detailed discussion of the conversion specifications [[Formatted Output|is available]]. | ||
+ | |||
+ | ===== Simple input ===== | ||
+ | |||
+ | <code groovy 1> | ||
+ | // Get an input reader | ||
+ | def input = new BufferedReader(new InputStreamReader(System.in)) | ||
+ | |||
+ | print " | ||
+ | def name = input.readLine() | ||
+ | println "Hello ${name}!" | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The object '' | ||
+ | |||
+ | ===== Inputting data values ===== | ||
+ | |||
+ | <code groovy> | ||
+ | def a = input.readLine().toInteger() | ||
+ | def x = input.readLine().toDouble() | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Method '' | ||
+ | |||
+ | In Example 5, below, we define three helper [[lecture5|methods]] to make it easier to read a '' | ||
+ | |||
+ | <code groovy 1 | Example 5: Simple input (at-m42/ | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | **Note** | ||
+ | Scripts with keyboard input cannot be run inside the // | ||
+ | < | ||
+ | <pre> | ||
+ | e: | ||
+ | Please enter your first name: < | ||
+ | Hello Chris! | ||
+ | Please enter your surname: < | ||
+ | Please enter your age: < | ||
+ | Please enter your weight in kg: < | ||
+ | Hello Chris Jobling: your age is 49 and you weigh | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== The Console class ===== | ||
+ | |||
+ | <code groovy> | ||
+ | package console | ||
+ | |||
+ | class Console { | ||
+ | def static readString() { ... } | ||
+ | def static readLine() { ... } | ||
+ | def static readInteger() { ... } | ||
+ | def static readDouble() { ... } | ||
+ | def static readBoolean() { ... } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The methods '' | ||
+ | |||
+ | The methods of class '' | ||
+ | |||
+ | <code groovy 1|Example 6: Input using the Console class (at-m42\Examples\lecture04\example6.groovy)> | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | To use '' | ||
+ | <cli prompt='>'> | ||
+ | h: | ||
+ | </ | ||
+ | If the classpath is set properly, Example 6 should run ok. | ||
+ | |||
+ | |||
+ | ===== Case Study: An Adventure Game ===== | ||
+ | |||
+ | This part of the lecture module will illustrate how '' | ||
+ | * For the first iteration we use a '' | ||
+ | * For the second iteration we use a '' | ||
+ | The // | ||
+ | |||
+ | ===== Iteration 1: Specification ===== | ||
+ | |||
+ | * A text-based adventure game maintains a record of items being carried by its players. | ||
+ | * Each item is known by its name and each player by his/her name. | ||
+ | * The database of items being carried could be represented in a number of ways. | ||
+ | * We will use '' | ||
+ | |||
+ | ===== Iteration 1: Using a List ===== | ||
+ | |||
+ | * The game is represented by a '' | ||
+ | * Each element in the '' | ||
+ | * the player' | ||
+ | * an //item// that the player is carrying. | ||
+ | |||
+ | ===== Iteration 1: List Implementation ===== | ||
+ | |||
+ | |||
+ | <code groovy 1> | ||
+ | def dng = [ [' | ||
+ | [' | ||
+ | [' | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | The '' | ||
+ | |||
+ | ===== Adding a new item ==== | ||
+ | |||
+ | <code groovy> | ||
+ | dng << | ||
+ | dng.add([' | ||
+ | </ | ||
+ | |||
+ | ===== Printing a location ===== | ||
+ | |||
+ | <code groovy> | ||
+ | println "dng: ${dng}" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== A simple adventure game ===== | ||
+ | |||
+ | The [[http:// | ||
+ | dng: [[Chris, Sword], [Chris, Food], [Jenny, Dagger], [Jenny, Food], [Joe, Magic Wand]] | ||
+ | Player Chris has a sword? true | ||
+ | Number of items in the dng: 5 | ||
+ | |||
+ | ---- | ||
+ | |||
+ | <code groovy l|Case study iteration 1: Adventure game location with List (at-m42\Examples\lecture04\casestudy1.groovy)> | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | We can determine if '' | ||
+ | <code groovy> | ||
+ | dng.contains([' | ||
+ | </ | ||
+ | and determine the number of items being carries by players in the location with; | ||
+ | <code groovy> | ||
+ | dng.size() | ||
+ | </ | ||
+ | |||
+ | ===== Iteration 2: Using a Map ===== | ||
+ | |||
+ | * Each location is represented by a '' | ||
+ | * Each //key// in the '' | ||
+ | * Each //value// in the '' | ||
+ | |||
+ | ===== Iteration 2: Map Implementation ===== | ||
+ | |||
+ | |||
+ | <code groovy 1> | ||
+ | def dng = [ ' | ||
+ | ' | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | in this revised implementation we associate a list of items with a player in a location. The key is the player' | ||
+ | |||
+ | ===== Adding items ==== | ||
+ | |||
+ | <code groovy 1> | ||
+ | // add a new player | ||
+ | dng[' | ||
+ | |||
+ | // update an existing player | ||
+ | dng[' | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | We can add a new player and his items to the game as shown in line 2. To update a player' | ||
+ | |||
+ | ===== Location queries ===== | ||
+ | |||
+ | <code groovy 1> | ||
+ | def hasSword = dng[' | ||
+ | def nPlayers = dng.size() | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | As with the '' | ||
+ | ===== More Queries ===== | ||
+ | |||
+ | <code groovy l> | ||
+ | def players = dng.keySet().sort() | ||
+ | def carriedItems = dng[' | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | Line 1 in the slide obtains the players' | ||
+ | |||
+ | ===== Putting it all together ===== | ||
+ | |||
+ | The [[http:// | ||
+ | dng: [Chris: | ||
+ | Player Chris has a sword? true | ||
+ | Number of items in the dng: 3 | ||
+ | Players: [Chris, Jenny, Joe] | ||
+ | Number of items being carried by Chris: 2 | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | <code groovy l|Case study iteration 2: Adventure game location with Map (at-m42\Examples\lecture04\casestudy2.groovy)> | ||
+ | extern> http:// | ||
+ | </ | ||
+ | |||
+ | We can determine if '' | ||
+ | <code groovy> | ||
+ | dng.contains([' | ||
+ | </ | ||
+ | and determine the number of items being carries by players in the location with; | ||
+ | <code groovy> | ||
+ | dng.size() | ||
+ | </ | ||
+ | |||
+ | ===== Summary of this Lecture ===== | ||
+ | |||
+ | |||
+ | Simple input and output and introduction of the case study. | ||
+ | |||
+ | * [[#Simple IO]] | ||
+ | * [[#Case Study: An Adventure Game|Case Study]] | ||
+ | |||
+ | |||
+ | ===== Lab Exercises ===== | ||
+ | |||
+ | * [[at-m42: | ||
+ | |||
+ | ---- | ||
+ | |||
+ | [[Home]] | [[lecture3|Previous Lecture]] | [[Lectures]] | [[lecture5|Next Lecture]] | ||
at-m42/lecture4.txt · Last modified: 2011/01/14 12:45 by 127.0.0.1