Hct216: Programming 2: Assignment 1: Flow Control and Core Apis
Hct216: Programming 2: Assignment 1: Flow Control and Core Apis
Submission format: Document uploaded on Tsime. Submit both source code and compiled code. All
files must be zipped into one ZIP file. Please note that this is an individual assignment. Late
submissions will not be heavily penalised. Plagarised / copied assignments will not be accepted.
Notes
1. Ensure that your code has been compiled and run on a computer and is producing the
correct results before submitting it. Thus both source code and compiled code classes must
be submitted. Every Java class must be in its own source code file.
2. Answers to questions that are not code must be on a separate document; and they must be
clearly labeled.
Questions
1. Looping
All alphabetic characters 'a' to 'z' have integer representations, e.g. 'a' is 97; and 'b' is 98;
and so on. Use the loop constructs below to write code that prints only the characters of the
alphabet backwards starting from 'z' to 'a' whose integer representations are even numbers.
The code will print the characters one on each line, together with their integer
representation, e.g.
z : 122
x : 120
…
b:…
(Use 4 separate classes that uses the following different loop constructs with their own main
methods):
a) for loop (use class called A1Q1a) [5]
b) while loop (use class called A1Q1b) [5]
c) do ... while loop (use class called A1Q1c) [5]
d) for ... each loop (use class called A1Q1d) [6]
[21 marks]
1 of 4
2. Arrays
a) Create a class called A1Q2 with the main method. Inside the main method, write code that
puts the following integers into an array: in the order they are here: 5,1,8,6,0,9,2,-3,7,4. [4]
b) Write a method named "printUsingDoWhile" of the class that uses the "do ... while" loop
construct to print all the integers in the array. The method must take the array as a
parameter. Call the method inside your main class. [4]
c) Write a method named "searchMyArray" that uses the search method learnt in the course
to search for the following integers: 6, -2, 19, 2, -7 and prints the results. The method must
take the array as a parameter. Call the method inside your main class. [4]
d) Write a method named "sortMyArray" that sorts the elements of the array. The method
must take the array as a parameter. Call the method inside your main class. [3]
e) Write a method named "printUsingForEach" that uses the "for ... each" loop construct to
print all characters in the array. Call the method inside your main class for it to print the
sorted array. [4]
f) Call the same search method in question (c) to search for the same set of characters in the
sorted array. [2]
i. Write down answers from (c) and those you got now besides each other to compare
them. How do the results relate to those in (c)? [2]
ii. Explain any difference for each of the five searches. [2]
g) Write a method named “squareRoots” which takes an array of integers as a parameter and
creates and returns a list (of type of an API class learnt in the course) of the square roots of
the integers in the array in the corresponding order of the integers in the array. For example,
if there is an array with integers {2,3,4}; then the returned list will have the square roots
{4,9,16}. Inside the main method of your main class, call the method “square roots” by
passing the sorted array from (d) and print all the elements (square roots) of the resultant
list. [5]
i) In the main method, all code that calls the method “numbersAndSquareRoots” and then
uses a do … while loop construct to print the 2-D array returned by the method call using the
format below:
[2, 4]
[3, 9]
2 of 4
[4, 16]
The square brackets should be included in the printing out of the output. [2]
[37 marks]
3. String API
In this question; you are supposed to write to a class called "ValidateAndFormatID" that; when
run, is able to get different formats of a national ID number, validate it and format it. For the
purpose of this question, a valid national ID number must have the following:
i. 2 digits,
ii. followed by 7 digits,
iii. followed by 1 alphabetic character that excludes ‘i’ and ‘o’,
iv. ending with 2 digits
The input can be in any of the following formats that can have separators like space ‘ ‘, TAB
character and dash ‘-‘, for example:
i. 29-1526374X 45
ii. 29 1526374X 45
iii. 29-1526374X-45
iv. 29 1526374 X 45
v. 29-1526374-X-45
vi. 291526374X45
Basically, any of the 3 separators can be on any place and can appear as many times and it
will just be ignored. The single format that should be returned is the one in example (i)
above; i.e. 29-1526374X 45
Write the class with the main class and it should have 1 input parameter on the command, e.g.:
Note that for valid IDs, the output must be printed out in the required output format.
[25 marks]
3 of 4
you are able to return the exact time when the football match ends. For the purpose of this
example, you are going to assume the following match periods:
“normal” : 1 hour 45 minutes (including breaks)
“extra time” : 2 hours 20 minutes 30 seconds (including breaks)
a) Create a class called A1Q4 with the main method. When being run, the main method must
take 2 parameters that represents the match period type and start date/time on the
command prompt in the format:
java A1Q4 “extra time” “2019-04-14 17:30:00”
Note that a single space separates the date and the time. The program must correctly parse
the date/time parameter into the appropriate date/time type learnt in the course; and
reject any wrong date/time and invalid values for match period type by printing out "Invalid
input, try again" and exiting. Note that the match period type (normal / extra time) can be
inputted in any case, e.g. “Normal”, NORMAL”, nORMal” are all acceptable. Compile and
run the class. [10]
b) In the class "A1Q4", create a class method called "calculateMatchEndTime" which takes 2
parameters:
i. the object that represents the inputted starting date/time of the match
ii. the match period type
The method must return an object that represents the date/time that represents the time
when the match ends. [9]
c) In main method of class "A1Q4", add code that now calls the method
“calculateMatchEndTime” and prints the end of the match. Print the results of the program
in the format:
“The match will end at 08:35:30 PM on Wednesday, 25 April
2019"
[5]
[24 marks]
4 of 4