You must be able to code these algorithms quickly. Click on links for some of the solutions offered as a download.

1) Determine if a number is odd or even.

2) Find the bigger of two values.

3) Swop two values around.

4) Check username and password – allow or disallow access.

5) Continue to run until a special condition is met i.e. a while loop that executes until a special condition is encountered either from the keyboard, or from an input text file or from data from a database.

While with NOT and if with NOT

6) Guessing game – allow three guesses and give appropriate responses, whether or not the guess is correct or not.

7) Round a double value to two decimal places.

8) Upcase a full name to capital letters and return the last name with initial only (e.g. Joe Doe – DOE, J)

9) Generate a single random whole number that falls within a specified range (e.g. 23 to 27 inclusive)

10) Reverse a specified word e.g. complicate becomes etacilpmoc

11) Passing an array as a parameter

Whether these are part of a tiny one class literal program or part of a bigger, multi-variable, multi-class program is not important.

12) Bring it all together. 

Create a two class program – a UI class for input and output and and a method class for the five methods shown below. Do this by copying your relevant code from programs 1 to 10 above.

Your program must have five methods in the method class . . .

NOTE: We need to pass a parameter that is an ARRAY (not a single value)

Declare the array as normal . . .

int[ ] numbers = new int[2]

Call the object.method and pass the parameter as normal . . .

myVariable = myObject.myMethod(numbers)

Method header would look like this . . .

public int[ ] myMethod ( int[ ] n)

The assignment statement inside the method would look like this  . . .

int[ ] numbers = n

The return would look like this . . .

return numbers


Method 1. Allow the user to login with their own full name (e.g. peter hammond) and the secret password.

Method 2. Capture two odd integer values from the keyboard. Both numbers must be bigger than 100. Your code must reject all even numbers and reject numbers smaller than 100. This method returns an integer array.

Method 3. Determine which number is the “bigger” and which is the “smaller”. This method accepts and returns an integer array.

Method 4. Generate a random number from smaller to bigger (lower bound to upper bound). This method accepts and returns an integer array.

Method 5. Divide the generated random number by 17 and round to two decimal places. This is the final answer. Use DecimalFormat (NB DecimalFormat works in String)

Output from the UI class . . .

Last Name plus initial e.g. HAMMOND, P

The final answer expressed as rands and cents e.g. R34.56

Click here for model answer