views:

337

answers:

7

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;
       }
  }
}
+2  A: 

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
Would you suggest redeclaring them in main like:this.operands;this.operators;or is this for another purpose?
Kevin Duke
@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
A: 

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
+1  A: 

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
+1  A: 

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
A: 

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
A: 
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

harigm
A: 

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);
}

}

Shashank T