(1) Create a project using your name "johs8972"
That will be your "main" class (since it's the class that has this
o public static void main(String[] args)
From here on, I'll refer to that as the "main" class.
(2) Use main_class.txt
Copy the contents of main_class.txt into your main class. (SECOND PAGE)
The code will not compile, until you do finish step (4)
(3) Create a class named Helper
Its purpose is to hold some methods to help you deal with input and numbers.
(4) Move methods into Helper
Move these methods from your main class into the Helper class.
o addValues
o subtractValues
o multiplyValues
o divideValues
o getInput
You may have to import a package so it compiles without error
Replace all the private static with public
At this point, your code should compile and run
(5) Create the moduloValues method
In Helper create a method named moduloValues to do the modulo operation (%)
(6) Use moduloValues
In your main class, use moduloValues in the same way that the other operations are used
The operation symbol will be: %
Don't forget to change the operation prompt so the user knows about "%"
(7) Comments and Formatting
Make sure you have the standard header comment in each .java file
Add comments throughout the code
Ensure your code is formatted by hitting Alt+Shift+F
main_class.txt
/*
* This is the code that you will put into your main class
* And delete this comment block afterward
*/
public static void main(String[] args) {
Helper helper = new Helper();
String operandOne = helper.getInput("Enter a numeric value: ");
String operandTwo = helper.getInput("Enter a numeric value: ");
String operation = helper.getInput("Choose an operation (+ - * /):");
double result = 0;
try {
switch (operation) {
case "+":
result = helper.addValues(operandOne, operandTwo);
break;
case "-":
result = helper.subtractValues(operandOne, operandTwo);
break;
case "*":
result = helper.multiplyValues(operandOne, operandTwo);
break;
case "/":
result = helper.divideValues(operandOne, operandTwo);
break;
default:
System.out.println("Unrecognized operation!");
return;
}
System.out.println("The answer is " + result);
} catch (Exception e) {
System.out.println("Number formatting exception " + e.getMessage());
}
}
private static double addValues(String operandOne, String operandTwo) {
double d1 = Double.parseDouble(operandOne);
double d2 = Double.parseDouble(operandTwo);
return d1 + d2;
}
private static double subtractValues(String operandOne, String operandTwo) {
double d1 = Double.parseDouble(operandOne);
double d2 = Double.parseDouble(operandTwo);
return d1 - d2;
}
private static double multiplyValues(String operandOne, String operandTwo) {
double d1 = Double.parseDouble(operandOne);
double d2 = Double.parseDouble(operandTwo);
return d1 * d2;
}
private static double divideValues(String operandOne, String operandTwo) {
double d1 = Double.parseDouble(operandOne);
double d2 = Double.parseDouble(operandTwo);
return d1 / d2;
}
private static String getInput(String prompt) {
System.out.print(prompt);
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}