Question

In: Computer Science

I have trouble with this assignment. hope I can learn it from the answer. 1. Add...

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);
}
}

Solutions

Expert Solution

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*/


Related Solutions

IS141 Assignments unit 1 reflection what did I learn from the assignment? How did this assignment...
IS141 Assignments unit 1 reflection what did I learn from the assignment? How did this assignment go for you, including any difficulties or misunderstandings you had?
This assignment requires using the Excel add-in called Solver... 1. Can a linear programming problem have...
This assignment requires using the Excel add-in called Solver... 1. Can a linear programming problem have no solution? More than one solution? Explain. 2. To find the optimal solution to a linear optimization problem, do you have to examine all the points in the feasible region? Explain.
Please I need answer for This question I have assignment for this subjects . Please be...
Please I need answer for This question I have assignment for this subjects . Please be brief and to the point . Write than 3 pages (maximum),but It is enough to give the name of the authors Only legible answers will be considered.Thanks in advance/Ha !-What are Structural discrimination in the housing market in Sweden?.Give an overview of the theories of discrimination used in economics. What are the main differences between these theories?
Please I need answer for This question I have assignment for this subjects . Please be...
Please I need answer for This question I have assignment for this subjects . Please be brief and to the point . Write than 3 pages (maximum),but It is enough to give the name of the authors Only legible answers will be considered.Thanks in advance/Ha 1-Discrimination in the Swedish housing market ?Give an overview of the theories of discrimination used in economics. What are the main differences between these theories?
Please I need answer for This question I have assignment for this subjects . Please be...
Please I need answer for This question I have assignment for this subjects . Please be brief and to the point . ,but It is enough to give the name of the authors Only legible answers will be considered.Thanks in advance/Ha 1- Give an overview of the Becker theory of Discrimination. a-Discrimination definition. b-Explicit Discrimination . c-Hypothesis formulation.
Python: I am not sure where to begin with this question, and I hope I can...
Python: I am not sure where to begin with this question, and I hope I can get an input on it. This is in regards to Downey's program, and we are asked to make two changes. Downey prints time as they do in the Army: 17:30:00 hours. We want to print that as 5:30 PM. Downey lets you define the time 25:00:00 - we want to turn over at 23:59:59 to 00:00:00. (I am asked to identify my changes with...
I'm having trouble thinking of a way that I can delete duplicates from a c string...
I'm having trouble thinking of a way that I can delete duplicates from a c string of alphabets. So what I'm supposed to do is I'm supposed to delete the repeated letters in the c string using pointers and also dynamically allocating space. I'm guessing that I will have to dynamically allocate space for an array to put in the letters that only appear once. To do that, can I create 2 pointers in a function and have 1 pointer...
What did Renaissance scholars hope to learn from the classical past? If the word renaissance means...
What did Renaissance scholars hope to learn from the classical past? If the word renaissance means “rebirth,” what exactly was reborn?
Working on a c++ data structures assignment.   Linked List add node. I have the head case...
Working on a c++ data structures assignment.   Linked List add node. I have the head case and the tail case working but the middle/general case I can not get to work for the life of me. I have included the header file and the data struct file below   #ifndef LINKEDLIST_H #define LINKEDLIST_H #include "data.h" #include <iostream>   //take this out using std::cout; class LinkedList{     public:         LinkedList();         ~LinkedList();         bool addNode(int, string);         bool deleteNode(int);         bool getNode(int, Data*);         void printList(bool = false);         int getCount();         void...
I am having trouble with my assignment and getting compile errors on the following code. The...
I am having trouble with my assignment and getting compile errors on the following code. The instructions are in the initial comments. /* Chapter 5, Exercise 2 -Write a class "Plumbers" that handles emergency plumbing calls. -The company handles natural floods and burst pipes. -If the customer selects a flood, the program must prompt the user to determine the amount of damage for pricing. -Flood charging is based on the numbers of damaged rooms. 1 room costs $300.00, 2 rooms...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT