Question 4:
☑Kizspy.me
Question 4: (3 marks)
The interface IUtilities below is already given in Java code format, thus you can use it without creating
an IUtilities.java file.:
<<Interface>>
IUtilities
+getPrime(n: int): int
+getCharacterByIndex(strInput: String, index: int):char
Write a class named MyUtilities, which implements the interface IUtilities. The class MyUtilities
implements all methods in IUtilities.
•
getPrime (n: int): int - return the nth prime number (the primes start from 2, ex: 2 3 5 7,...). If n
is less than or equal to 0, return 0. For example: n=3, return the 3th prime number is 5
getCharacterByIndex(strInput: String, index: int): char - return the character at index of the string
strInput in uppercase. If the index is out of range then return the character @
Hints: You can refer to the methods: toLowerCase(), toCharArray(), valueOf() of the String class,
toUpperCase() of the Character class and StringBuffer class
The program output might look something like this:
1. Test getPrime
2. Test getCharacterByIndex
Enter TC (1/2): 1
Enter a number: 3
OUTPUT:
5
1. Test getPrime
+ 100%
1. Test getPrime
2. Test getCharacterByIndex
Enter TC (1/2): 1
Enter a number: -1
OUTPUT:
0
1. Test getPrime
Zoom
Close