Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 1

1.

4 Create New Class


• Right-Click java directory on the project view situated on the left panel
• Select New!Java Class
• A popup appears that prompts you to enter the name of the new class!Name: StringCalculator
• Click Ok
Implement the add method that takes an input of numbers in string format separated by delimiters that can
take any form e.g.1,2#
3,4. The method returns the sum of the input of numbers.
public class StringCalculator {
public int add(String input) {
int output = 0;
String[] numbers = new InputNormalizer().normalize(input);
InputValidator.validate(numbers);
for (String num:numbers) {
int numInt = Integer.parseInt(num);
if(numInt < 1000) {
output += numInt;
}
}
return output;
}
}
Add the InputNormalizer class. This class takes the input of numbers in string format and split them up based
on the defined
delimiters. The delimiters are defined at the beginning of the string, the start of the delimiters is marked by //
and the end of the
delimiters is marked by n. Each delimiter is enclosed within the open and closed brackets. e.g.//[***]
[%]n1***2%3

You might also like