I am trying to write a Calculator. I must write the operations in one Class and the result in another. So this is what i did...
public class Calc { calc_main c=new calc_main(); private int add; private int sub; private int mul; private int div; public Calc(){ add=0; sub=0; mul=0; div=0; } public void adder(int adds){ adds=c.number1+c.number2; add=adds; } public int added(){ return add; } public void suber(int subs){ subs=c.number1-c.number2; sub=subs; } public int subed(){ return sub; } public void muler(int muls){ muls=c.number1*c.number2; mul=muls; } public int muled(){ return mul; } public void diver(int divs){ divs=c.number1/c.number2; div=divs; } public int dived(){ return div; } }
There are no errors in any lines. But when i run it, it asks me to enter the numbers and operator twice and just stop.public class calc_main { public int number1; public int number2; public String oper; public calc_main(){ Scanner num=new Scanner(System.in); System.out.println("Enter first number:"); number1=num.nextInt(); System.out.println("Enter operation:"); oper=num.next(); System.out.println("Enter second number:"); number2=num.nextInt(); } } public static void main(String[] args) { calc_main c=new calc_main(); Calc r=new Calc(); if(c.oper=="+") System.out.println(c.number1+"+"+c.number2+"="+r.added()); else if(c.oper=="-") System.out.println(c.number1+"-"+c.number2+"="+r.subed()); else if(c.oper=="*") System.out.println(c.number1+"*"+c.number2+"="+r.muled()); else if(c.oper=="/") System.out.println(c.number1+"/"+c.number2+"="+r.dived()); }