The document describes how to create a new class in Java called StringCalculator. It involves right clicking the java directory, selecting new Java class, naming the class StringCalculator, and clicking ok. The document then provides an example implementation of an add method in the StringCalculator class that takes a string of numbers separated by delimiters, normalizes the input, validates the numbers, parses the numbers to integers, sums the integers under 1000, and returns the total. It also describes adding an InputNormalizer class to split the string into numbers based on the defined delimiters.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
38 views1 page
Nuevo Documento de Texto Enriquecido
The document describes how to create a new class in Java called StringCalculator. It involves right clicking the java directory, selecting new Java class, naming the class StringCalculator, and clicking ok. The document then provides an example implementation of an add method in the StringCalculator class that takes a string of numbers separated by delimiters, normalizes the input, validates the numbers, parses the numbers to integers, sums the integers under 1000, and returns the total. It also describes adding an InputNormalizer class to split the string into numbers based on the defined delimiters.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 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