Computer Science Courses
COMP2100
Related sites
The first 10 minutes of the lab session time may be spent by your tutor to mark pre-lab tasks.
% ./greeting EricHello Eric% ./greeting AlistairGreetings Professor Rendell% ./greeting AnishG'Day Anish
You only need to have a few different greetings for different names and one default greeting. Please keep all your greetings positive and friendly.
This program must be completed in order to start the in-class activities.
Write a Java program called, StringCount that takes two string arguments from the command line and returns a count of how many times an occurrence of the first string can be found in the second
$ java StringCount "aaa" "aaaabbb"2
Occurrences may overlap. For example ‘wowow’ occurs twice in ‘wowowow’
(Hint - if the expression i == mainString.indexOf(subString, i)
evaluates to true then the subString will be within the mainString starting at position i.)
Extend the StringCount program to support loading files and counting occrrences of a string within the a file
$ echo "aaabbbaaa" >> TestFile$ java StringCount -f "TestFile" "aaa"2
Use the facade design pattern to implement the file load function
The objective is to write a Bash script to test the StringCount program with different string combinations. This may be done individually or in a small group of 2 or 3 students.
Create a file called 'cases' to contain the test cases. Each line should contain two strings. Here are a few lines to get you started. Add some more of your own
x xx xxwowow wowowowow
The following simple shell script will read words from each line of its input and print those words out again
#!/bin/bashwhile read x ydo echo ${x} ${y}done
The key feature of this script is the read command which reads a line of input and assigns the first word to the variable x and the rest of the line to the variable y. The read command gives an exit code of 0 (= success = True) if was was able to read the words, or 1 (= failure = False) if it encounters the end of its input. That makes it perfect for use in a while loop, as seen in this example. When the input runs out, it returns 1, and the loop terminates.
./tester < cases
string1 string2
, it outputs the number of occurrences of string1 that StringCount claims are in string2.Note: This does not mean rewrite the Java program as a bash script. It means write a bash script that runs the Java program on each line of its input and formats the results.
Modify your cases file so that each line now contains 3 things, the two words as before, plus the correct number of occurrences of the first word in the second. For example, your file will start:
x x 1x xx 2wowow wowowowow 3
Modify your tester script so that for each test it prints a line of the following form:
Test i: string1 string2: Expected n, got m
where i is the line number of this test, n is the correct number of occurrences of string1 in string2, and m is the number found by StringCount.
Modify your tester so that when strcnt gets the right answer, it prints a line of form:
Test i: Passed
The key to this is to use an if statement in your shell script. Remember that the form of an if statement is as follows:
if command-listthencommand-listelsecommand-listfi
Note: The line breaks (or semicolons in their place) are required. You can leave out the else part if you don't need it.
Have a look at the manual page for the test command (by typing man test). That should tell you everything you need to know now to complete this exercise. The test command evaluates a boolean expression (as well as being able to test for all sorts of other conditions). It basically lets you program in Bash in a way very like what you would do in Java.
The test command is so commonly used in shell scripts that it has a short-hand notation in which [ stuff ]
means the same as test stuff (those spaces inside the square brackets are required). Use this notation to simplify your solution.
Updated: 15 May 2016 / Responsible Officer: JavaScript must be enabled to display this email address. / Page Contact: JavaScript must be enabled to display this email address. / Powered by: Snorkel 1.4
+61 2 6125 5111
The Australian National University, Canberra
CRICOS Provider : 00120C
ABN : 52 234 063 906