So I am super new to programming in general and I'm only a few weeks into a beginning summer course on Java and I'm super confused on this assignment for a simple java calculator that we need to use three classes in.
I decided to write the code and get "working" in one class and then try and split it up into the necessary multiple classes. However, I'm having trouble doing that. I guess I'm more confused then I thought I was on how methods and parameters work. I'd greatly appreciate any help explaining how they work particularly in this program or something similar so that I can understand it more clearly.
Here are a part of the instructions that I'm having a hard time with.... "The Driver class is the only class wit hmain(String[] args)method.Driver class should call a method in the Menu class to print the menu, and from that method there should be calls to methods in Actions class for respective functionality from the menu."
The Menu class and the method calls are working correctly and I planned on basically adding the rest to the Action class but I just keep confusing myself more every time I mess with it. I don't know if it makes a difference but I thought about adding the switch to the Menu class, also if I do, do I need to make it a method to call the switch? How would that work?
import java.util.Scanner;
import java.io.*;
public class Driver {
public static void main(String[] args) {
String s1 = Menu.getInput("Enter a numeric value: ");
String s2 = Menu.getInput("Enter a numeric value: ");
String option = Menu.getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");
class Action {
int optionInt = Integer.parseInt(option);
double result = 0;
switch (optionInt) {
case 1:
result = addValues(s1, s2);
break;
case 2:
result = subtractValues(s1, s2);
break;
case 3:
result = multiplyValues(s1, s2);
break;
case 4:
result = divideValues(s1, s2);
break;
default:
System.out.println("You entered an incorrect value");
}
public static String(
System.out.println("The answer is " + result);
}
private static double divideValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 / d2;
return result;
}
private static double multiplyValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
private static double subtractValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 - d2;
return result;
}
private static double addValues(String s1, String s2) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
double result = d1 + d2;
return result;
}
}
}
class Menu {
public static String getInput(String prompt) {
String option;
Scanner scan = new Scanner(System.in);
System.out.println(prompt);
return option = scan.nextLine();
}
}
Thanks for any help!!
Scott
Action action = new Action()
, and thenaction.addValues(..)
– robzillaDev Jul 30 at 7:02