You are on page 1of 1

Programming 2

Tutorial 2 : Flow Control and Arrays

Preparation
1. Ensure you use a text editor and you have JDK 8 installed on your machine
2. Create a folder CT216 and another one called T2 inside it so that you have folder structure:
CT216\T2

Exercises
1. Looping
a) Create a sub-folder inside T2 called "ex1"
b) Write code that prints even numbers from 50 to 100 using (use 3 separate classes with their
own main methods):
i. for loop
ii. while loop
iii. do ... while loop

2. Conditional statements
a) Create a sub-folder inside T2 called "ex2"
b) Create a class called T2Ex2; with a single method called "weekdays". The method should
take a string representing a day name, e.g. "Monday" and prints the day number, e.g.
"Sunday" will result in 1 while "Monday" results in 2. Use the "if" statement for this. Write a
main method for the class and test your methods by passing many values of "day name" to
see if it work correctly
c) Add method called "weekdaysUsingSwitch" which does exactly the same as the one in (b);
but uses the "switch ... case" statement. Also call it within the main method by adding more
lines and run it.

3. Arrays
a) Create a class called T2Ex3 with the main method. Write code that puts the following
numbers: in the order they are here: 8,1,5,6,0,9,3,2,7,4
b) Use the "for .. each" statement to print all numbers in the array
c) Sort the array
d) Use binarySearch to search for the following numbers: 4, 7, -5, 15
e) Sort the array
f) Use binarySearch to search for the following numbers: 4, 7, -5, 15. How do the results
related to those in (d)? Explain any difference.

You might also like