Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Start with source file DigitSum.java. Write a takes inputs an an integer number ( either positive or negative ) , prints it out, then finds

Start with source file DigitSum.java.
Write a takes inputs an an integer number (either positive or negative), prints it out, then finds and prints out the sum of all digits in that number.
INPUT VALIDATION: If user enters a string that cannot be parsed into integer, the input must be rejected with an error message containing word ERROR and the prompt must be repeated.
See sample program runs in the example below.
Requirements:
Do not use arrays in your solution.
Do not convert integer into a string in order to extract digits. Use % operator instead.
Sample #1- valid input
Enter an integer =>11111
You entered number 11111. The sum of its digits is 5
Sample #2- valid input
Enter an integer =>1234
You entered number 1234. The sum of its digits is 10
Sample #3- invalid input
Enter an integer => dqdwqqwd
Input ERROR. Number entered was not an integer.
Enter an integer =>9.87
Input ERROR. Number entered was not an integer.
Enter an integer => dad
Input ERROR. Number entered was not an integer.
Enter an integer =>-66
You entered number -66. The sum of its digits is 12
Test cases:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.regex.Pattern;
public class TestDigitSum {
public static boolean tests(PrintWriter outputStream)
{
int count =0;
int expectedCount =3;
outputStream.print("\r
----Tests for DigitSum--------------------------------------------------------\r
");
// Test 1//
outputStream.print("\r
____Test 01____________________________________________________________________________\r
");
String sep = System.lineSeparator();
String userInput = String.format("222222222");
String expectedOutput ="(?si).*?222222222.*18.*";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
PrintStream standard = System.out;
ByteArrayInputStream bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
System.setOut(printStream);
DigitSum.main(null);
String actual = baos.toString();
printStream.close();
outputStream.print("\r
Actual Output:\r
");
outputStream.println("\""+actual+"\"");
outputStream.println();
if(Pattern.matches(expectedOutput, actual))
{
outputStream.printf("%-80s%-10s\r
", "DigitSum TEST 01- valid positive integer", "PASSED");
count++;
}
else
{
outputStream.printf("%-80s%-10s\r
", "DigitSum TEST 01- valid positive integer", "FAILED");
}
// Test 2//
outputStream.print("\r
_____Test 02____________________________________________________________________________\r
");
userInput = String.format("-112121");
expectedOutput ="(?si).*?-112121.*8.*";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
System.setOut(printStream);
DigitSum.main(null);
actual = baos.toString();
printStream.close();
outputStream.print("\r
Actual Output:\r
");
outputStream.println("\""+actual+"\"");
outputStream.println();
if(Pattern.matches(expectedOutput, actual))
{
outputStream.printf("%-80s%-10s\r
", "DigitSum TEST 02- valid negative integer", "PASSED");
count++;
}
else
{
outputStream.printf("%-80s%-10s\r
", "DigitSum TEST 02- valid negative integer", "FAILED");
}
// Test 3//
outputStream.print("\r
_____Test 03____________________________________________________________________________\r
");
userInput = String.format("abc%s9.33%s123",sep, sep);
expectedOutput ="(?si).*?ERROR.*ERROR.*123.*6.*";
outputStream.print("\r
Input:\r
");
outputStream.println(userInput);
outputStream.print("\r
Expected output must fit RegEx:\r
");
outputStream.println("\""+expectedOutput+"\"");
bais = new ByteArrayInputStream(userInput.getBytes());
System.setIn(bais);
baos = new ByteArrayOutputStream();
printStream = new PrintStream(baos);
System.setOut(printStream);
DigitSum.main(null);
actual = baos.toString();
printStream.close();
outputStream.print("\r
Actual Output:\r
");
outputStream.println("\""+actual+"\"");
outputStream.println();
if(Pattern.matches(expectedOutput, actual))
{
outputStream.printf("%-80s%-10s\r
", "DigitSum TEST 03- inv

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

Factors Affecting Conflict

Answered: 1 week ago

Question

Describe the factors that lead to productive conflict

Answered: 1 week ago

Question

Understanding Conflict Conflict Triggers

Answered: 1 week ago