Write and test the following programs in Java. Develop and test your programs in whatever way you like - you are responsible for how it behaves when I test it later. You should design your testing strategy carefully. You can use JUnit if you wish, or you can use a script, or you can test by hand.You can use DrJava, or Kate, or emacs, or eclipse, or...
Copies of the data and program files have been provided in your top level home directory and in the directory /dept/dcs/comp2100/exam
The computer environment for this exam has no Internet access outside the local domain. Copies and links to the normal resources have been created as follows.
A copy of the class web site is at http://csitexam/student/comp2100
The full Java 5 JDK and API documentations are at
/dept/dcs/comp2100/public/www/jdk/index.html
/dept/dcs/comp2100/public/www/jdk/api/index.html
The full Java Tutorial is at
/dept/dcs/comp2100/public/www/tutorial/index.html
The Apache Ant Tutorial is at
/dept/dcs/comp2100/public/www/ant/index.html
The Advanced Bash Scripting Guide is at
/dept/dcs/comp2100/public/www/abs-guide/HTML/index.html
Junit 4 (includes Junit 3.8) is at
/dept/dcs/comp2100/public/junit.jar
A copy of the exam starting files and datasets is in the directory
/dept/dcs/comp2100/exam
Setting up your directories
Your home directory has been set up with 4 directories: q1, q2, q3, q4. Your solutions must be in the correct directory (answers to question 1 in q1, etc)..
Question 1.
Bash: extracting information from files of text records 30 marks
A librarian wants to extract useful information from a file containing records of loans of books.
The LoansList.tsv file contains records in the format
memberID date time bookIDA single TAB character separates between these fields.
Write two Bash scripts. Leave your script files in directory q1.
- [15 marks] In a file called
Nloanswrite a Bash script with no command arguments. It shall count how many books have ever been borrowed by each one of the members from the fileLoansList.tsvThe output shall be in order of memberID numbers. The output shall be in the format like the following sample (this is not the whole of the answer)
8 u0362020 4 u1377093 10 u1471952This output format has one member on each line. The memberID comes after the count of how many loans for that member in the LoansList. There is (optional) white space before the count, then a required white space (tab or spaces), then the memberID).
Hint: the command
uniqhas a counting option; uniq works only if its input is sorted into order. The commandcutworks with tab-separated fields by default.The command
sortsorts the input to the output, but it produces lexicographic ordering (i.e. dictionary ordering, not numerical order). Sometimes this is the right thing, but sometimes you need numerical order. Sort has a switch to choose numerical order, and another to produce the result in reverse order.Your script must be in directory q1, in a file named
Nloans(no filename extension). It must be in Bash.You must test your script carefully; inspect the contents of the LoansList, run other commands, count by hand... to make sure that your answer is correct. Note that the LoansList has only a small number of memberIDs, in a larger number of Loans and a large number of bookIDs.
- [15 marks] In a file called
bigBorrowerwrite a script that outputs a list of the biggest borrowers (that is, a list of the members who borrowed the most books, with the count of how many they have borrowed.) The one command argument states how many of the biggest borrowers will be listed. For example,$ ./bigBorrower 2will output the count and memberID for 2 members only: the members who have the 2 largest counts.
./bigBorrower 5would give us the 5 biggest borrowers, etc.The argument can be any number greater than zero.
The output shall be in exactly the same format as for part 1.
You can make use of your Nloans command in this script if you wish. Your script for this part must be in a subdirectory called q1 of your home directory in a file called
bigBorrowerHint: you may want to use the
headcommand.
Question 2.
Java: Small Numbers to Words 50 marks
This task is similar to Homework 6, but read this question carefully for differences.
Java programs must compile standalone. Create no packages in your development environment. Leave your program java file in directory q2.You are given a starting point for this program in the attachment (this is a link):
/dept/dcs/comp2100/exam/Words.javaSave your finished program in a new subdirectory of your top level directory q1 in a file named
Words.javaWrite the missing parts for a Java program called ‘Words’ that shall print out a small integer in words.
The program shall take exactly one command line argument, which shall be a non-negative integer (including 0) less than one thousand (that is, no more than three digits).
If it gets the wrong number of arguments, or if its argument isn't an integer, or if the argument is not in the range 0–999, it shall print a usage message (starting with the exact word 'Usage:' - upper case 'U', lower case 'sage', colon, space-see example below) - and then exit.
If the input is OK, it shall print out the value of its argument, written out in words on one line, in exactly the style specified below.
For example:
comp2100@partch java Words 17 seventeen comp2100@partch java Words 123 one hundred and twenty-three comp2100@partch java Words 210 two hundred and ten comp2100@partch java Words 600 six hundred comp2100@partch java Words 70 seventy comp2100@partch java Words 999 nine hundred and ninety-nine comp2100@partch java Words 1000 Usage: words n (where 0 < n < 1000) comp2100@partch java Words 0 zeroNotice the use of the word ‘and’ in the output. This program must write numbers in the Australian/British style (“one hundred and thirty-seven”), not the American style (“one hundred thirty-seven”).
Note the use of the hyphen ' -' between tens (thirty) and units (seven), and note that there is no hyphen except where there is a word for the tens: 201 (two hundred and one), 230 (two hundred and thirty), 241 (two hundred and forty-one).
The spelling of every word must be correct, as follows: zero one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
twenty thirty forty fifty sixty seventy eighty ninety
hundred and
Hint: cut and paste can be useful to avoid retyping into your program.
You must provide output in exactly the format specified. Check this very, very carefully.
Only one space between words. All lower case letters. No punctuation symbols in normal output except for '-' (punctuation is acceptable in the Usage message). One line of output. All output on standard output, not the standard error stream.
You will be marked on good style and clarity of your coding as well as on the correctness.
Hints for style: Try to avoid writing code like
switch (n) { case 1: dWord = "one"; break; case 2: dWord = "two"; break; ...Instead store the words in an array of strings so that the item at index 1 is the string "one" and so on. Then you can replace that whole ugly switch with a single line something like “dWord = digits[n]”. You'll need one array for the one digit numbers, one for the ‘teens’ and one for the multiples of ten: twenty, thirty, forty etc. Or one array for single and teens, and another for tens.
Alternatively, instead of arrays, you might prefer to use HashTables as in the assignment code.
Question 3. 10 marks
THIS IS A HARDER QUESTION. DO NOT ATTEMPT IT UNTIL YOU HAVE COMPLETED Question 1 and Question 2.
Bash: extract and join information
leave your script file in directory q3
The librarian has a further request to provide information, using a second file containing the list of all bookIDs and book titles.
In a file called
Last2Borrowedwrite a script that outputs a set of the 2 latest loans for each members within a given year and month. The output shall be in order of memberID, the memberID on a line by itself heading a list of the 2 loans, listing each loan on a separate line, ordered by increasing date. Each loan shall list the date (not the time), the bookID and the title of the book.The second file BookList.tsv file (also in the exam directory) contains records in the format
bookID author yearPublished ISBN titleAgain, a single TAB character separates these fields.
The command shall take 2 arguments: a month (exactly 2 digits, with a leading zero if necessary) and a year (4 digits). Note that the LoansList contains only 2006 July-December and 2007 January-June dates.
For example:
$ ./Last2Borrowed 08 2006
u1377093
2006-08-06 2006413 Bead & Button, Issue 73, June 2006
u3248221
2006-08-07 1982066 The Cimabue Crucifix
2006-08-23 2004383 Creative Drawing Ideas
u3334840
2006-08-06 1996244 Fashion Memoir: Alaa
2006-08-13 2000326 Denton Corker Marshall: rule playing and the ratbag element
You can make use of your other command scripts in this script if you wish, but you must copy them into this directory.
Hint: consider using a for-do-done Bash construct. Consider the command
grepto extract matching records.
Question 4. 10 marks
Java: numbers with decimal fractions to words
THIS IS A HARDER QUESTION. DO NOT ATTEMPT IT UNTIL YOU HAVE A GOOD SOLUTION TO Q2.
Write an extended version of the small numbers to words program in Java called DecimalPoint. You must have separate programs for the 2 Java questions. DecimalPoint.java must be left in directory q4.
Input: one command line argument in the form of an number of 1-3 decimal digits including zero, an optional fractional part (indicated by a fullstop (period) character as the decimal point, followed by up to 3 decimal digits). eg. 234, 0.00, 123.456, 0.77, 0)
Output: the number written out in words, in the form
- if the number has no decimal point, or the fractional part equals zero, and the number is greater than zero: same as Question 2 e.g.
two hundred and thirty-four- if the number equals 0 (with or without fractional part): the word zero
- if the number has non-zero digits before the point, and a fractional part not equal to zero:
words for the whole number part (as in Q2), followed by the word "point", followed by the names of the single digits e.g.
one hundred and twenty-three point four five six- if the number has zero whole part with non-zero fractional part: the word "zero" followed by "point" and names of digits as above e.g.
zero point seven sevenHints: do not use float (or things become approximate representations and you lose the required accurate handling). You may need to handle parsing the input as separate pieces after splitting it into separate string parts.