简体   繁体   中英

Method call and a class inside a class

I'm in a college summer class that is going super fast and I'm extremely new to programming in general (be nice haha).

One of our assignments this week is to create a simple calculator. Here are a few of the instructions.

"The Driver class is the only class with main(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."

My code is no where near completed I'm just trying to figure out what I'm doing wrong with calling the getInput method that's inside the Menu class. Per the instructions it seems like the method call needs to be in the Driver class. However, I'm getting a few errors when I try and compile.

Driver.java:6: error: cannot find symbol
        String user = getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");
              ^
  symbol:   method getInput(String)
  location: class Driver
Driver.java:11: error: cannot find symbol
        String s1 = getInput("Enter a numeric value: ");
                ^
  symbol:   method getInput(String)
  location: class Driver
Driver.java:12: error: cannot find symbol
        String s2 = getInput("Enter a numeric value: ");
                ^
 symbol:   method getInput(String)
 location: class Driver
Driver.java:14: error: non-static variable user cannot be referenced from a static context
        int userInt = Integer.parseInt(user);
                                   ^
Driver.java:70: error: Illegal static declaration in inner class Driver.Menu
        public static String getInput(String prompt) {
                             ^
  modifier 'static' is only allowed in constant variable declarations
5 errors

................

import java.util.Scanner;
import java.io.*;

public class Driver {

String user = getInput("Enter: \n 1=Add \n 2=Subtract \n 3=Multiply \n 4=Divide");

public static void main(String[] args) {


    String s1 = getInput("Enter a numeric value: ");
    String s2 = getInput("Enter a numeric value: ");

    int userInt = Integer.parseInt(user);
    double result = 0;

    switch (userInt) {
    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");
    }

    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)
        throws NumberFormatException {
    double d1 = Double.parseDouble(s1);
    double d2 = Double.parseDouble(s2);
    double result = d1 + d2;
    return result;
}



public class Menu { 
    public static String getInput(String prompt) {
        String user;
        Scanner scan = new Scanner(System.in);
        System.out.println(prompt);
        return user = scan.nextLine();
    }
}

}

This post is already pretty long so I won't post the rest of the instructions unless you'd like me to.

getInput() is a static method in another class, you should call it as follows:

String s1 = Menu.getInput("Enter a numeric value: ");
//            ^   the name of the class should come before the method name 

The javadoc says - Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args)

So you have to change

String s1 = getInput("Enter a numeric value: ");
String s2 = getInput("Enter a numeric value: ");

to

String s1 = Menu.getInput("Enter a numeric value: ");
String s2 = Menu.getInput("Enter a numeric value: ");

Moreover you can also refer to static methods with an object reference like

instanceName.methodName(args)

Edit:

Menu class

class Menu {
    public static String getInput(String prompt) {
        String user;
        Scanner scan = new Scanner(System.in);
        System.out.println(prompt);
        return user = scan.nextLine();
    }
}

Driver class

public class Driver {
   public static void main(String[] args) {
     String user = Menu.getInput("Enter: 1=Add  2=Subtract 3=Multiply 4=Divide");
     String s1 = Menu.getInput("Enter a numeric value: ");
     String s2 = Menu.getInput("Enter a numeric value: ");
        //rest of the codes same
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM