You are on page 1of 2

COMP102: Computer Programming

Practical 4: Algorithms in Classes

Thursday, 16 September 2021

Question 1: String Algorithms


Create a class named StringUtility that contains no attributes. Implement the
following methods in the class:

1. A method that accepts an integer in the form of a string. The method should
then add all of the digits in even locations from the left-hand side of the string,
i.e. index 0, 2, 4, 6, … The sum should then be returned to the caller
2. A method that accepts a string as input. It should then reverse this string and
return it to the caller
3. A method that accepts a string as input and determines whether it begins will
an uppercase letter or not. The method should return a Boolean value of true
or false
4. A method that accepts a string as input and determines whether it contains
at least one numeric digit or not. The method should return a Boolean value
of true or false
5. A method that accepts a string as input and determines whether all letters in
the string are in uppercase or not. The method should return true if the string
strictly contains only uppercase letters. If it contains any lowercase or non-
alphabetic characters, it should return false

Create another class called TestStringUtility and test the functionality of the methods
you created above.

Question 2: Number Algorithms


Create a class named NumberUtility that contains no attributes. Implement the
following methods:

1. A method that accepts three integers, calculates which is the greatest, and
then returns that integer. You are not permitted to use the max() method in
the Maths from Java’s standard library. You must implement this method from
first principles.
2. A method that accepts an integer and determines whether it is a prime
number of not. The method should return a Boolean value
3. A method that accepts an integer upper bound as an argument. It will then
display all of the prime numbers smaller than this upper bound.

1
4. A method that receives three integers as input. These integers represent the
three sides of a triangle. The method must then classify the triangle as an
equilateral, isosceles or scalene. No value should be returned by the method,
the classification is to be given to the user in the form of a message displayed
on the console.
5. A perfect square is an integer whose square root is a whole number, i.e.
contains no fractional apart. For example, √16 = 4, therefore 16 is a perfect
square. However, because √20 = 4,472 …, 20 is not a perfect square. Write a
method that determines whether an integer passed in as an argument is a
perfect square of not. The method should return a Boolean value

Create another class called TestNumberUtility and test the functionality of the
methods you created above.

Question 3: MyDate Class


Create a class named MyDate. The class should have the following attributes:

• Year
• Month
• Day

Implement the following methods without the use of any code from Java’s Date
class:

• Determine whether another date object represents a date after the date
represented by the calling object. If the date is after, the method should
return 1. If the date falls on the same date as that represented by the calling
object, return a 0. If the date falls before the date represented by the calling
object, return a -1
• Create a set of methods to change the value of the year, month and day
attributes of a MyDate object. Ensure that the day cannot fall outside the
range of days in the specified month. For example, April has 30 days, so we
cannot set the day attribute to 31 for this month. You will need to
accommodate the 29th of February for leap years.
• Create a set of methods to allow someone to get the year, month and day
from a MyDate object

Create a constructor that allows someone to create legal MyDate objects. For
example, the month and day values must be within appropriate ranges. The year
cannot be a negative value

Create another class called TestMyDate and test the functionality of all methods
created above.

You might also like