In: Computer Science
I have trouble with this assignment. hope I can learn
it from the answer.
1. Add Multiplication Function(*). addition(+) and subtraction(-)
is already there.
Junit Test
2. Add one method to test some valid values to try ParseInt. You will have to use AssertEquals. You have to make sure that tryParseInt returns the expected value. Values to test are: "-2", "-1", "0", "1", "2". For example - Calc.tryParseInr.
3. Add one method to test some valid values for isCharAllowed. Values to test: ('a', "abc"), ('b', "abc"), ('a', "a"), ('',"").
4. Add one method to test some incorrect values for isCharAllowed. Values to test: ('a',""), ('b',"xyz"), ('a',null), ('z'," ").
import java.util.Scanner;
public class Validation {
private static final Scanner keyboard = new Scanner(System.in);
public static String userString(String x) {
System.out.print(x + ": ");
return keyboard.nextLine();
}
public static int userInt(String x) {
Integer s = null;
while (s == null) {
String b = userString(x);
s = Calc.tryParseInt(b);
}
return s;
}
public static char userChar(String x, String r) {
char c = 0;
while (!Calc.isCharAllowed(c, r)) {
String i = userString(x + " (" + r + ")");
if (i.length() > 0) {
c = i.charAt(0);
}
}
return c;
}
}
public class Main {
public static void main(String[] args) {
int valueOne = IOHelper.userInputInt("Enter first
value");
int valueTwo= IOHelper.userInputInt("Enter second value");
char opr = IOHelper.userInputChar("Choose an operation", "+-");
double result;
switch (operation) {
case '+':
result = MathHelper.addValues(valueOne, valueTwo);
break;
case '-':
result = MathHelper.subtractValues(valueOne, valueTwo);
break;
default:
System.out.println("Unrecognized operation!");
return;
}
System.out.println("The answer is " + result);
}
}
public class CalculationMath {
public static double addValues(double valueOne, double valueTwo)
{
return valueOne + valueTwo;
}
public static double subtractValues(double valueOne,
double vlueTwo) {
return valueOne - valueTwo;
}
}
public class Calc {
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);
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: The files attached below are: Main.java, CalculationMath.java and JUnitTest.java. Calc.java and Validation.java files are not modified, hence not included.
//Main.java (errors fixed, updated)
public class Main {
public static void main(String[] args) {
int valueOne = Validation.userInt("Enter first value");
int valueTwo = Validation.userInt("Enter second value");
//including * operation
char opr = Validation.userChar("Choose an operation", "+-*");
double result;
switch (opr) {
case '+':
result = CalculationMath.addValues(valueOne, valueTwo);
break;
case '-':
result = CalculationMath.subtractValues(valueOne, valueTwo);
break;
case '*':
//multiplication
result = CalculationMath.multiplyValues(valueOne, valueTwo);
break;
default:
System.out.println("Unrecognized operation!");
return;
}
System.out.println("The answer is " + result);
}
}
//CalculationMath.java
public class CalculationMath {
public static double addValues(double valueOne, double valueTwo) {
return valueOne + valueTwo;
}
public static double subtractValues(double valueOne, double valueTwo) {
return valueOne - valueTwo;
}
//method to find product of valueOne and valueTwo
public static double multiplyValues(double valueOne, double valueTwo) {
return valueOne * valueTwo;
}
}
//JUnitTest.java
import static org.junit.Assert.*;
import org.junit.Test;
public class JUnitTest {
@Test
public void testParseInt() {
assertEquals(new Integer(-2), Calc.tryParseInt("-2"));
assertEquals(new Integer(-1), Calc.tryParseInt("-1"));
assertEquals(new Integer(0), Calc.tryParseInt("0"));
assertEquals(new Integer(1), Calc.tryParseInt("1"));
assertEquals(new Integer(2), Calc.tryParseInt("2"));
assertEquals(null, Calc.tryParseInt("x"));
}
@Test
public void testIsCharAllowedWithValidInput(){
assertTrue(Calc.isCharAllowed('a', "abc"));
assertTrue(Calc.isCharAllowed('b', "abc"));
assertTrue(Calc.isCharAllowed('a', "a"));
assertTrue(Calc.isCharAllowed(' ', " "));
}
@Test
public void testIsCharAllowedWithInvalidInput(){
assertFalse(Calc.isCharAllowed('a', ""));
assertFalse(Calc.isCharAllowed('b', "xyz"));
assertFalse(Calc.isCharAllowed('a', null));
assertFalse(Calc.isCharAllowed('z', " "));
}
}
/*OUTPUT (of Main)*/
Enter first value: 25
Enter second value: 3
Choose an operation (+-*): *
The answer is 75.0
/*OUTPUT of JUnit test*/