1

Firstly this is not a homework question. I am practicing my knowledge on java. I figured a good way to do this is to write a simple program without help. Unfortunately, my compiler is telling me errors I don't know how to fix. Without changing much logic and code, could someone kindly point out where some of my errors are? Thanks

import java.lang.*;
import java.util.*;

public class Calculator
{
    private int solution;
    private int x;
    private int y;
    private char operators;

    public Calculator()
    {
        solution = 0;
        Scanner operators = new Scanner(System.in);
        Scanner operands = new Scanner(System.in);
    }

    public int addition(int x, int y)
    {
       return x + y;
    }
    public int subtraction(int x, int y)
    {
       return x - y;
    }
    public int multiplication(int x, int y)
    {    
       return x * y;
    }
    public int division(int x, int y)
    {
       solution = x / y;
       return solution;
    }
    public void main (String[] args)
    {
      System.out.println("What operation? ('+', '-', '*', '/')"); 

      System.out.println("Insert 2 numbers to be subtracted");
       System.out.println("operand 1: ");
       x = operands;
       System.out.println("operand 2: ");
       y = operands.next();
      switch(operators)
      {
          case('+'):
            addition(operands);
            operands.next();
            break;
          case('-'):
            subtraction(operands);
            operands.next();
            break;
          case('*'):
            multiplication(operands);
            operands.next();
            break;
          case('/'):
            division(operands);
            operands.next();
            break;
       }
  }
}
dukevin
  • 22,384
  • 36
  • 82
  • 111
  • 3
    What kind of errors are you getting? – Lars Andren Apr 29 '10 at 01:29
  • 2
    Thanks for posting your code. However, it also helps a lot when you post the text of the error messages you're getting from the compiler - this makes it easier for people to quickly identify the problem (without having to read the whole code or compile it themselves). – Greg Hewgill Apr 29 '10 at 01:42

10 Answers10

4

operands and operators are out of scope for main. You declare local variables in the constructor, so when you exit the ctor they're eligible for GC and gone.

You have compilation errors - 10 of them.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Would you suggest redeclaring them in main like: this.operands; this.operators; or is this for another purpose? – dukevin Apr 29 '10 at 01:53
  • @Kevin Duke: You cannot add fields to a class at runtime in the language. You will need to specify them in Calculator, like you specified solution and operators. Main also does not have "this" as it must be static. – Yann Ramin Apr 29 '10 at 03:02
4
package org.com;

import java.lang.*; 
import java.util.*; 

public class Calculator 
{ 
    private int solution; 
    private static int x; 
    private static int y; 
    private char operators; 

    public Calculator() 
    { 
        solution = 0; 
        Scanner operators = new Scanner(System.in); 
        Scanner operands = new Scanner(System.in); 
    } 

    public int addition(int x, int y) 
    { 
       return x + y; 
    } 
    public int subtraction(int x, int y) 
    { 
       return x - y; 
    } 
    public int multiplication(int x, int y) 
    {     
       return x * y; 
    } 
    public int division(int x, int y) 
    { 
       solution = x / y; 
       return solution; 
    } 

    public void calc(int ops){
         x = 4; 
         System.out.println("operand 2: "); 
         y = 5; 

         switch(ops) 
         { 
             case(1): 
               System.out.println(addition(x, y)); 

           //    operands.next(); 
               break; 
             case(2): 
                 System.out.println(subtraction(x, y)); 
              // operands.next(); 
               break; 
             case(3): 
                 System.out.println(multiplication(x, y)); 
             //  operands.next(); 
               break; 
             case(4): 
                 System.out.println(division(x, y));
             //  operands.next(); 
               break; 
          } 
    }
    public static void main (String[] args) 
    { 
      System.out.println("What operation? ('+', '-', '*', '/')");  
      System.out.println(" Enter 1 for Addition");
      System.out.println(" Enter 2 for Subtraction");
      System.out.println(" Enter 3 for Multiplication");
      System.out.println(" Enter 4 for Division");

       Calculator calc = new Calculator();
       calc.calc(1);


  } 
} 

This will work

gmhk
  • 15,598
  • 27
  • 89
  • 112
2

Another issue is, the line

y = operands.next();

is attempting to place a String returned from Scanner.next() into the a variable y which is declared as a type int.

The Scanner.nextInt() method can be used to attempt to return an int.

coobird
  • 159,216
  • 35
  • 211
  • 226
2
package com.abc;

import java.util.Scanner;

public class Calculator {
    private static final String pos = "+";
    private static final String neg = "-";
    private static final String mult = "*";
    private static final String div = "/";

    private enum operation {
        pos, neg, mult, div
    };
    private int solution;
    private int x;
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private int y;



    static Scanner operators;

    public Calculator() {
        solution = 0;
        operators = new Scanner(System.in);

    }

    public int addition(int x, int y) {
        return x + y;
    }

    public int subtraction(int x, int y) {
        return x - y;
    }

    public int multiplication(int x, int y) {
        return x * y;
    }

    public int division(int x, int y) {
        solution = x / y;
        return solution;
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println("Insert 2 numbers");

        System.out.println("operand 1: ");

        calc.setX(Integer.parseInt(operators.next()));

        System.out.println("operand 2: ");
        calc.setY(Integer.parseInt(operators.next()));

        System.out.println("What operation? ('pos', 'neg', 'mult', 'div')");
        operation ttt = operation.valueOf(operators.next());
        int output = 0 ;
        switch(ttt){
        case pos:
            output = calc.addition(calc.getX(), calc.getY());

            break;
          case neg:
              output = calc.subtraction(calc.getX(), calc.getY());

            break;
          case mult:
              output = calc.multiplication(calc.getX(), calc.getY());

            break;
          case div:
              output = calc.division(calc.getX(), calc.getY());

            break;
        }
        System.out.println("output ="+output);
    }
}
Anonymous
  • 11,748
  • 6
  • 35
  • 57
Shashank T
  • 669
  • 1
  • 6
  • 10
1

This is all great, but what program are you using to write your java? Maybe you should consider using an IDE like Eclipse, as it can detect errors automatically and also adds imports. (I'm not sure if yours does this) It also tells you what the problem with your program is 'in english'. Also, consider this class as maybe an easier and less complicated way of doing a calculator:

public class Calculator {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter an Operator: ");
    String in = sc.next();
    char oper = in.charAt(0);

    System.out.print("Enter a number: ");
    in = sc.next();
    double num1 = Double.parseDouble(in);

    System.out.print("Enter another number: ");
    in = sc.next();
    double num2 = Double.parseDouble(in);

    if(oper == '+') {
        double result = num1 + num2;
        System.out.println(result);
    } else if(oper == '-') {
        double result = num1 - num2;
        System.out.println(result);
    } else if(oper == 'x') {
        double result = num1 * num2;
        System.out.println(result);
    } else if(oper == '/') {
        double result = num1 / num2;
        System.out.println(result);
    } else {
        double result = num1 % num2;
        System.out.println(result);
    }
        System.out.println("Hope this helped your mathmatical troubles!");
}

}
And as a matter of habit, instead of doing:

import java.util.*;

it is better to do:

import java.util.Scanner;

This probably doesn't make much of a difference here, but if you are running a much bigger program importing the whole of java.util will considerably slow down your program.

Hope this helps!

Dan299
  • 165
  • 1
  • 11
  • Your last point is wrong. Importing wildcard packages has no bearing on runtime performance. It is only used for compile-time symbol resolution – Kirk Woll Dec 23 '12 at 00:27
  • Sorry about that. It's just I've heard tonnes of people say how importing the entire java libraries slows down the program. Or did I mishear? – Dan299 Dec 23 '12 at 11:23
  • I suspect you didn't mishear -- there is just a ton of misinformation surrounding that. Take a look at [this question](http://stackoverflow.com/questions/7128348/performance-difference-between-a-wild-card-import-and-the-required-class-import). – Kirk Woll Dec 23 '12 at 16:40
1

In addition to the other answers, your main() method must be static in order to be a program entry point. In main() you will need to construct your own Calculator object, and call methods on that.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
0

Your main method needs to be declared like this:

public static void main(String[] args) {..}

Furthermore, it seems like you are only supplying one argument to your all your arithmetic methods (addition, subtraction etc), although they require two.

public int addition(int x, int y);

Can not be called with addition(operands), that is only one argument, and the argument is of the wrong type (the method needs two int, you give it a Scanner). The same goes for all those methods. You need to extract the ints from the Scanner. You can do that with Scanner.nextInt().

Lars Andren
  • 8,601
  • 7
  • 41
  • 56
-1
import java.lang.*;

import java.util.*;


public class Calculator
{
    private int solution;
    private int x;
    private int y;
 private char operators;

    public Calculator()
    {
        solution = 0;
        Scanner operators = new Scanner(System.in);
        Scanner operands = new Scanner(System.in);
    }

    public int addition(int x, int y)
    {
       return x + y;
    }
    public int subtraction(int x, int y)
    {
       return x - y;
    }
    public int multiplication(int x, int y)
    {    
       return x * y;
    }
    public int division(int x, int y)
    {
       solution = x / y;
       return solution;
    }
    public void main (String[] args)
    {
      System.out.println("What operation? ('+', '-', '*', '/')"); 

      System.out.println("Insert 2 numbers to be subtracted");
       System.out.println("operand 1: ");
       x = operands;
       System.out.println("operand 2: ");
       y = operands.next();
      switch(operators)
      {
          case('+'):
            addition(operands);
            operands.next();
            break;
          case('-'):
            subtraction(operands);
            operands.next();
            break;
          case('*'):
            multiplication(operands);
            operands.next();
            break;
          case('/'):
            division(operands);
            operands.next();
            break;
       }
  }
}
-1

You are asking for the user to type in integers, but you put the statement operands.next(); as the input. Try to keep consistent with your variables and user input, so changing it to operands.nextInt() would help.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
-2

Just as a tip, it's generally not a good idea to start throwing

import java.util.*;
into your program because it makes the program unnecessarily large and slow. All you need for this is to
import java.util.Scanner;
If I'm correct, most if not everything in java.lang is already imported for you.
Jazzy Josh
  • 272
  • 1
  • 7
  • 3
    This is wrong. Importing wildcard packages has no bearing on runtime performance (or the size of your program, for that matter). It is only used for compile-time symbol resolution. – Kirk Woll Dec 23 '12 at 00:26