In: Computer Science
JAVA programming language
Please add or modify base on the given code
Adding functionality
Adding JUnit tests
----------------------------------------------------
Given code
Main.java
public class Main {
public static void main(String[] args) {
int operandOne =
IOHelper.userInputInt("Enter first numeric value");
int operandTwo =
IOHelper.userInputInt("Enter second numeric value");
char operation =
IOHelper.userInputChar("Choose an operation", "+-");
double result;
switch (operation)
{
case '+':
result = MathHelper.addValues(operandOne, operandTwo);
break;
case '-':
result = MathHelper.subtractValues(operandOne, operandTwo);
break;
default:
System.out.println("Unrecognized operation!");
return;
}
System.out.println("The
answer is " + result);
}
}
-------------------------------------
IOHelper.java
import java.util.Scanner;
public class IOHelper {
private static final Scanner keyboard = new
Scanner(System.in);
public static String userInputString(String
x) {
System.out.print(x + ":
");
return
keyboard.nextLine();
}
public static int userInputInt(String x)
{
Integer o = null;
while (o == null)
{
String u = userInputString(x);
o = ValidationHelper.tryParseInt(u);
}
return o;
}
public static char userInputChar(String x, String r) {
char c = 0;
while
(!ValidationHelper.isCharAllowed(c, r)) {
String i = userInputString(x + " (" + r + ")");
if (i.length() > 0) {
c = i.charAt(0);
}
}
return c;
}
}
-----------------------
MathHelper.java
public class MathHelper {
public static double addValues(double
operandOne, double operandTwo) {
return operandOne +
operandTwo;
}
public static double subtractValues(double
operandOne, double operandTwo) {
return operandOne -
operandTwo;
}
}
------------------------
ValidationHelper.java
public class ValidationHelper {
public static Integer tryParseInt(String
userInput) {
try {
return Integer.parseInt(userInput);
} catch (Exception e)
{
return null;
}
}
public static boolean isCharAllowed(char ch,
String r) {
if (r == null) {
return false;
}
return (r.indexOf(ch)
>= 0);
}
}
NOTE: The ValidationHelper.java and IOHelper.java remains the same as given in the question. Please find MathHelper.java, Main.java and JunitTests.java below.
// MathHelper.java
public class MathHelper {
/**
* This method adds the two given double values and
returns the result.
*
* @param operandOne
* - value 1
* @param operandTwo
* - value 2
*/
public static double addValues(double operandOne,
double operandTwo) {
return operandOne +
operandTwo;
}
/**
* This method subtracts the second double value from
the first double value
* and returns the result.
*
* @param operandOne
* - value 1
* @param operandTwo
* - value 2
*/
public static double subtractValues(double operandOne,
double operandTwo) {
return operandOne -
operandTwo;
}
/**
* This method multiplies the two given double values
and returns the
* result.
*
* @param operandOne
* - value 1
* @param operandTwo
* - value 2
*/
public static double multiplyValues(double operandOne,
double operandTwo) {
return operandOne *
operandTwo;
}
}
// Main.java
public class Main {
public static void main(String[] args) {
int operandOne =
IOHelper.userInputInt("Enter first numeric value");
int operandTwo =
IOHelper.userInputInt("Enter second numeric value");
char operation =
IOHelper.userInputChar("Choose an operation", "+-*");
double result;
switch (operation) {
case '+': // Addition
result =
MathHelper.addValues(operandOne, operandTwo);
break;
case '-': // Subtraction
result =
MathHelper.subtractValues(operandOne, operandTwo);
break;
case '*': // Multiplication
result =
MathHelper.multiplyValues(operandOne, operandTwo);
break;
default:
System.out.println("Unrecognized operation!");
return;
}
System.out.println("The answer is "
+ result);
}
}
// JunitTests.java
import org.junit.Test;
import static org.junit.Assert.*;
public class JunitTests {
/**
* This method tests valid integers by passing them to
the
* ValidationHelper.tryParseInt() method.
*/
@Test
public void testValidInts() {
assertEquals(-2, (int)
ValidationHelper.tryParseInt("-2"));
assertEquals(-1, (int)
ValidationHelper.tryParseInt("-1"));
assertEquals(0, (int)
ValidationHelper.tryParseInt("0"));
assertEquals(1, (int)
ValidationHelper.tryParseInt("1"));
assertEquals(2, (int)
ValidationHelper.tryParseInt("2"));
}
/**
* This method tests invalid integers by passing them
to the
* ValidationHelper.tryParseInt() method.
*/
@Test
public void testInvalidInts() {
assertNull(ValidationHelper.tryParseInt(""));
assertNull(ValidationHelper.tryParseInt(" "));
assertNull(ValidationHelper.tryParseInt("1.2.3"));
assertNull(ValidationHelper.tryParseInt("."));
assertNull(ValidationHelper.tryParseInt("x"));
assertNull(ValidationHelper.tryParseInt("one"));
assertNull(ValidationHelper.tryParseInt(null));
}
/**
* This method tests valid integers by passing them to
the
* ValidationHelper.isCharAllowed() method.
*/
@Test
public void testValidChar() {
assertTrue(ValidationHelper.isCharAllowed('a', "abc"));
assertTrue(ValidationHelper.isCharAllowed('b', "abc"));
assertTrue(ValidationHelper.isCharAllowed('a', "a"));
assertTrue(ValidationHelper.isCharAllowed(' ', " "));
}
/**
* This method tests invalid integers by passing them
to the
* ValidationHelper.isCharAllowed() method.
*/
@Test
public void testInvalidChar() {
assertFalse(ValidationHelper.isCharAllowed('a', ""));
assertFalse(ValidationHelper.isCharAllowed('b', "xyz"));
assertFalse(ValidationHelper.isCharAllowed('a', null));
assertFalse(ValidationHelper.isCharAllowed('z', " "));
}
}
CODE SCREENSHOTS
SAMPLE OUTPUT
OUTPUT of Main.java
JunitTest.java output