Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
    
did your professor teached you already about object oriented programming? –  Rod_Algonquin Jul 30 at 5:58
    
try using an IDE like eclipse. The IDE will allow you to choose a file option to create new classes, and will let you know if your code will not compile (as in you case). –  Scary Wombat Jul 30 at 5:58
    
I am more having trouble with creating the methods for the other classes to call and what parameters I need. –  user3862586 Jul 30 at 6:01
    
Ok so I think I figured out how to get the classes set up. Good idea on the new file for the classes on Eclipse. My new problem is how to call addValues, SubtractValues, divideValues, and multiplyValues is the Menu class. I'm not sure what arguments to put in the call? I think it should look similar to this...Action.addValues(....); I can't figure out what arguments to add though. –  user3862586 Jul 30 at 6:39
    
you must instantiate an object of the class, and then call the methods of that class. Action action = new Action(), and then action.addValues(..) –  robzillaDev Jul 30 at 7:02

2 Answers 2

up vote 0 down vote accepted

I think this is what you probably looking for :

import java.util.Scanner;
import java.io.*;
public class Driver {
public static void main(String[] args) {
    // get the Menu instance & call the method to get the menu list 
    Menu m = new Menu();
    m.getMenu();
 }
}

Note: Note here inorder to call a method that resides in another class you first need to get instance of the class and then on that instance you need to invoke the method. Like in the above m.getMenu()

In Menu class you need to write the following logic :

class Menu {
public void getMenu() {
// take the user inputs and write your validation and proper conversion logic e.g   'int' or 'double' what ever you want
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the first number :");
    double num1 = s.nextDouble();
    System.out.println("Enter the second number :");
    double num2 = s.nextDouble();
    System.out.println("Please select an operation :");
    System.out.println("1 - Add " + "\n" + "2 - Subtract" + "\n"
            + "3 - Multiply" + "\n" + "4 - Divide");

    int choice = s.nextInt();
    Double result;
    Action service = new Action();
    switch (choice) {
    // call the appropriate method based on user selection
    case 1:
        result = service.addValues(num1, num2);
        service.displayResult(result);
        break;
    case 2:
        result = service.subtractValues(num1, num2);
        service.displayResult(result);
        break;
    case 3:
        result = service.multiplyValues(num1, num2);
        service.displayResult(result);
        break;
    case 4:
        result = service.divideValues(num1, num2);
        service.displayResult(result);
        break;
    default:
        System.out.println("Your choice is invalid.");

    }

 }

}

NOte: Here you're doing nothing but taking the user inputs and passing those inputs as parameter to the appropriate methods. (Again note here, how the methods are being invoked).

In Action class put your service logic

class Action {
    public double addValues(double a, double b) {
    return a + b;
}

public double subtractValues(double a, double b) {
    return a - b;
}

public double multiplyValues(double a, double b) {
    return a * b;
}

public double divideValues(double a, double b) {
    return a / b;
}

public void displayResult(double result) {
    System.out.println("Result is :" + result);
}
}

Note: Here each method serves a very different purpose. When you invoke any method on the instance of this class by passing your user inputs as parameter then it simply serve its purpose.

P.S: Methods are much like your behaviors. Simply consider yourself as a machine. Now when you get thirsty you drink water, right? so here is your brain that is the main() instructing your hand to perform some task much like you're invoking a method to serve a purpose here. (I don't know how far I able to give a clarity of this, but I hope this will be helful to you in some extent :) )

share|improve this answer
    
That I think clarified everything pretty good, thank you!! I do have a question, in the code above it looks like you changed the methods from a String (how I originally had it) to a double. I think that was what was confusing me. I imagine it is better your way? That way you don't have to then convert from a string to a double everywhere like I was doing? Thanks again! –  user3862586 Jul 30 at 23:03
    
Also, why the m in Menu m = new Menu() & m.getMenu. Could I just call it like this...Menu.getMenu(); instead of what I guess is creating a new variable (m)? Also, where does service come into play? In other words, why service.addValues? Why wouldn't it be Action.addValues(num1, num2); don't you have to use Action because you're calling those from the Action class? I also don't see how service comes into play in the Action class. Thanks again! –  user3862586 Jul 30 at 23:33
    
I swear this is my last question...In the switch with the default being if they enter an option that is not listed, how can I get it to restart the switch if they enter the wrong option and not have to rerun the program? Can I do that inside the switch or would I need to put the switch inside a while loop? –  user3862586 Jul 30 at 23:39
    
@user3862586 yeah, u're right. Instead of creating an instance variable m here you can directly write something like : new Menu().getMenu(). Note that as I've previously mentioned, inorder to invoke a method which resides in another class you first need to obtain the instance of that class, much like new Menu() here. –  Rockstar Jul 31 at 4:20
    
comming to your second question, yes I've changed some variable names a bit from the original version that u had post. Actually, that doesn't matter. You can assign any name. see you can change it as per your convention service is the name of the Action class instance here, you can make it to action also. Action action= new Action(); and then invoke the corresponding methods like action.addValues(); etc.. –  Rockstar Jul 31 at 4:27

Firstly, I'd recommend a good book, or failing that, look through Oracle's resources such as this...

Java is an object oriented language. An object is an INSTANCE of a class. Think of classes like modules, that are logically separated and in theory should be runnable and logical not only in the environment which you create it, but in other scenarios as well.

For example, you may have a program called Car Park, which is the driver (contains the main method, the entry point to a Java program). You then have a class called Car, and a class called Person. A class should contain variables and method related to that class. So in the Car class, you may have variables such as colour, noOfWheels, noOfSeats etc. The car class may also contain methods to 'do things' associated with the class, such as updateColor() or changeNoOfWheels().

In regards to methods and parameters, it is really quite simple. When a method is defined as such:

public void updateColor(Color newColor)
{
    color = newColor;
}

The Color that is being passed inbetween the brackets is called the formal parameter. It defines the type that is required when the method is actually called. This can be any type, depending on what your method does. A method that adds two integers may accept (int x, int y as its parameters. This method simply takes the color that is passed to it, and assigns that to the color of the car (updates the color, as the method is called).

From the driver class, you must instantiate objects from the class. So,

Car myCar = new Car()

Once it is instantiated, you can call the methods that belong to it.

myCar.updateColor(red);

The color between the brackets here is the actual parameter. This assumes that red is a predefined variable for a color, otherwise you can call new Color() and define your own.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.