at-m42:labs:lab1
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
at-m42:labs:lab1 [2009/04/10 09:28] – eechris | at-m42:labs:lab1 [2011/01/14 12:59] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Lab 1: Programming the Java Platform 1 ====== | ||
+ | These exercises are designed to reinforce the material presented during the first day's lectures. Create a new folder in your work directory '' | ||
+ | |||
+ | ===== Part 1: Basic Building Blocks ===== | ||
+ | |||
+ | These exercises support [[at-m42: | ||
+ | |||
+ | Present each of your solutions to exercises 1-7 in a single script called // | ||
+ | <code groovy> | ||
+ | // Lab 1: Exercise n.m | ||
+ | // Student number: 0123456 | ||
+ | </ | ||
+ | |||
+ | Use the '' | ||
+ | <code groovy> | ||
+ | def x = 12 | ||
+ | def y = 2 | ||
+ | assert (x + 3 <= y * 10) == true | ||
+ | </ | ||
+ | |||
+ | In general, you should present your answers using an // | ||
+ | |||
+ | ==== Exercise 1.1 ==== | ||
+ | |||
+ | Use the rules of [[at-m42: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | def y = 2 | ||
+ | x + 3 <= y * 10 | ||
+ | </ | ||
+ | < | ||
+ | x = 20 | ||
+ | y = 2 | ||
+ | x + 3 <= y * 10 | ||
+ | </ | ||
+ | < | ||
+ | y = 1 | ||
+ | x +3 != y * 10 | ||
+ | </ | ||
+ | < | ||
+ | y = 2 | ||
+ | x + 3 == y * 10 | ||
+ | </ | ||
+ | < | ||
+ | y = 5 | ||
+ | x + 3 > y * 10 | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 1.2 ==== | ||
+ | |||
+ | Evaluate each of the following expressions: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 1.3 ==== | ||
+ | |||
+ | Evaluate each of the following expressions: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 1.4 ==== | ||
+ | |||
+ | Declare a '' | ||
+ | <code groovy> | ||
+ | def str = " | ||
+ | </ | ||
+ | Then evaluate each of the following expressions: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 1.5 ==== | ||
+ | |||
+ | Declare the following: | ||
+ | <code groovy> | ||
+ | def str = " | ||
+ | </ | ||
+ | Then evaluate the following expressions: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 1.6 ==== | ||
+ | |||
+ | Declare the following: | ||
+ | <code groovy> | ||
+ | def str = " | ||
+ | </ | ||
+ | Then evaluate the following expressions: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 1.7 ==== | ||
+ | |||
+ | Evaluate each of the following expressions: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ===== Part 2: Lists, Maps and Ranges ===== | ||
+ | |||
+ | These exercises support [[at-m42: | ||
+ | |||
+ | ==== Exercise 2.1 ==== | ||
+ | |||
+ | A list of the first few prime numbers is defined as: | ||
+ | <code groovy> | ||
+ | def primes = [2, 3, 5, 7, 11, 13] | ||
+ | </ | ||
+ | Write some suitable assertions to determine: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ==== Exercise 2.2 ==== | ||
+ | |||
+ | The School of Engineering has a number of disciplines, | ||
+ | <code groovy> | ||
+ | def school = [' | ||
+ | ' | ||
+ | ' | ||
+ | ] | ||
+ | </ | ||
+ | Write some suitable assertions to determine: | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ===== Part 3: Simple IO and Case Study I ===== | ||
+ | |||
+ | These exercises support [[at-m42: | ||
+ | |||
+ | ==== Exercise 3.1 ==== | ||
+ | |||
+ | Given the following | ||
+ | <code groovy> | ||
+ | def staffNumber = 123 | ||
+ | def staffSalary = 458.78 | ||
+ | </ | ||
+ | prepare a formatted output statement to produce the following output: | ||
+ | STAFF PAY | ||
+ | 123 | ||
+ | |||
+ | Note how the numerical values are directly aligned with the text. | ||
+ | |||
+ | ==== Exercise 3.2 ==== | ||
+ | |||
+ | Use the '' | ||
+ | |||
+ | ==== Exercise 3.3 ==== | ||
+ | |||
+ | use the '' | ||
+ | |||
+ | ==== Exercise 3.4 ==== | ||
+ | |||
+ | Extend the definition of an item for the '' | ||
+ | |||
+ | ===== Part 4: Methods ===== | ||
+ | |||
+ | These exercises support the first part of [[at-m42: | ||
+ | |||
+ | ==== Exercise 4.1 ==== | ||
+ | |||
+ | Prepare and test a method entitled '' | ||
+ | |||
+ | ==== Exercise 4.2 ==== | ||
+ | |||
+ | Pre-decimal UK coinage had 12 pence in a shilling and 20 shillings in a pound. Write and test methods to add and subtract two of these monetary amounts. Both methods will require 6 parameters. The first three parameters will be pounds, shilling, pence for the first amount. The second will be pounds, shilling, pence for the second amount. The result should be returned in pence. | ||
+ | |||
+ | ==== Exercise 4.3 ==== | ||
+ | |||
+ | Ammend exercise 4.2 to take the parameters in two lists like '' | ||
+ | |||
+ | ==== Exercise 4.4 ==== | ||
+ | |||
+ | // | ||
+ | |||
+ | ==== Exercise 4.5 ==== | ||
+ | |||
+ | // | ||
+ | |||
+ | <code groovy> | ||
+ | gcd(n, m) = n if n == m | ||
+ | gcd(n, m) = gcd(n, m - n) if n < m | ||
+ | gcd(n, m) = gcd(n - m, m) otherwise | ||
+ | </ | ||
+ | |||
+ | Implement a method to realize '' | ||
+ | |||
+ | ===== Part 5: Flow of Control ===== | ||
+ | |||
+ | These exercises support the second part of [[at-m42: | ||
+ | |||
+ | ==== Exercise 5.1 ==== | ||
+ | |||
+ | Write a program that reads a single positive integer data value and then displays each individual value as a word. For example the input 932 should display | ||
+ | 932: nine three two | ||
+ | |||
+ | ==== Exercise 5.2 ==== | ||
+ | |||
+ | The Groovy language does not support the '' | ||
+ | <code java> | ||
+ | do { | ||
+ | // statements | ||
+ | } while (condition) | ||
+ | </ | ||
+ | |||
+ | Write a code that implements this structure. | ||
+ | |||
+ | ===== Part 6: Closures ===== | ||
+ | |||
+ | These exercises support the first part of [[at-m42: | ||
+ | |||
+ | ==== Exercise 6.1 ==== | ||
+ | |||
+ | A software house is contracted to develop Groovy, Java and C# projects. Each project has one or more programmers involved, with perhaps the same individual associated with more than one project. For example, the following shows Chris, Renate and Paul involved with the Groovy project: | ||
+ | <code groovy> | ||
+ | def softwareHouse = [' | ||
+ | ' | ||
+ | ' | ||
+ | ] | ||
+ | </ | ||
+ | Predict the output of each of the following (verify by testing the code): | ||
+ | < | ||
+ | <ol style=" | ||
+ | < | ||
+ | < | ||
+ | softwareHouse[' | ||
+ | softwareHouse[' | ||
+ | if (g == j) println " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | </ol> | ||
+ | </ | ||
+ | |||
+ | ===== Part 7: Files ===== | ||
+ | |||
+ | These exercises support the second part of [[at-m42: | ||
+ | |||
+ | ==== Exercise 7.1 ==== | ||
+ | |||
+ | Re-write the '' | ||
+ | |||
+ | ==== Exercise 7.2 ==== | ||
+ | |||
+ | Write a program the copy one text file to another, removing any blank lines in the original file. The file names are given as command line arguments. | ||
+ | |||
+ | ==== Exercise 7.3 ==== | ||
+ | |||
+ | |||
+ | Extend the '' | ||
+ | |||
+ | ==== Exercise 7.4 ==== | ||
+ | |||
+ | |||
+ | Prepare a variant of '' | ||
+ | |||
+ | ===== Acknowledgements ===== | ||
+ | |||
+ | The exercises are based on those given // | ||
+ | |||
+ | ---- | ||
+ | |||
+ | [[at-m42: |