Answered step by step
Verified Expert Solution
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 # valid input
Enter an integer
You entered number The sum of its digits is
Sample # valid input
Enter an integer
You entered number The sum of its digits is
Sample # invalid input
Enter an integer dqdwqqwd
Input ERROR. Number entered was not an integer.
Enter an integer
Input ERROR. Number entered was not an integer.
Enter an integer dad
Input ERROR. Number entered was not an integer.
Enter an integer
You entered number The sum of its digits is
Test cases:
import java.ioByteArrayInputStream;
import java.ioByteArrayOutputStream;
import java.ioPrintStream;
import java.ioPrintWriter;
import java.util.regex.Pattern;
public class TestDigitSum
public static boolean testsPrintWriter outputStream
int count ;
int expectedCount ;
outputStream.printr
Tests for DigitSumr
;
Test
outputStream.printr
Test r
;
String sep System.lineSeparator;
String userInput String.format;
String expectedOutput si;
outputStream.printr
Input:r
;
outputStream.printlnuserInput;
outputStream.printr
Expected output must fit RegEx:r
;
outputStream.printlnexpectedOutput;
PrintStream standard System.out;
ByteArrayInputStream bais new ByteArrayInputStreamuserInputgetBytes;
System.setInbais;
ByteArrayOutputStream baos new ByteArrayOutputStream;
PrintStream printStream new PrintStreambaos;
System.setOutprintStream;
DigitSum.mainnull;
String actual baos.toString;
printStream.close;
outputStream.printr
Actual Output:r
;
outputStream.printlnactual;
outputStream.println;
ifPatternmatchesexpectedOutput actual
outputStream.printfssr
"DigitSum TEST valid positive integer", "PASSED";
count;
else
outputStream.printfssr
"DigitSum TEST valid positive integer", "FAILED";
Test
outputStream.printr
Test r
;
userInput String.format;
expectedOutput si;
outputStream.printr
Input:r
;
outputStream.printlnuserInput;
outputStream.printr
Expected output must fit RegEx:r
;
outputStream.printlnexpectedOutput;
bais new ByteArrayInputStreamuserInputgetBytes;
System.setInbais;
baos new ByteArrayOutputStream;
printStream new PrintStreambaos;
System.setOutprintStream;
DigitSum.mainnull;
actual baos.toString;
printStream.close;
outputStream.printr
Actual Output:r
;
outputStream.printlnactual;
outputStream.println;
ifPatternmatchesexpectedOutput actual
outputStream.printfssr
"DigitSum TEST valid negative integer", "PASSED";
count;
else
outputStream.printfssr
"DigitSum TEST valid negative integer", "FAILED";
Test
outputStream.printr
Test r
;
userInput String.formatabcsssep, sep;
expectedOutput siERROR.ERROR;
outputStream.printr
Input:r
;
outputStream.printlnuserInput;
outputStream.printr
Expected output must fit RegEx:r
;
outputStream.printlnexpectedOutput;
bais new ByteArrayInputStreamuserInputgetBytes;
System.setInbais;
baos new ByteArrayOutputStream;
printStream new PrintStreambaos;
System.setOutprintStream;
DigitSum.mainnull;
actual baos.toString;
printStream.close;
outputStream.printr
Actual Output:r
;
outputStream.printlnactual;
outputStream.println;
ifPatternmatchesexpectedOutput actual
outputStream.printfssr
"DigitSum TEST inv
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started