Rits rishi
okay! i got it! finally..😀 my program is running... now i am actually having confusion at function calling..
my new program:-
import java.util.*;
public class pro {
int a,b;
static Scanner s1=new Scanner(System.in);
public static void main(String args[]){
pro p1=new pro();
int o=p1.menu();
System.out.println("o="+o);
p1.input();
p1.choice(o);
}
public int menu() //first function
{ int num;
System.out.println("welcome to my calculator");
System.out.println("choose your operation: \n enter 1 for addition \n enter 2 for subtraction \n enter 3 for multiplication \n enter 4 for division \n enter 5 for modulus");
System.out.println("enter your choice number:");
int c=s1.nextInt();
num=s1.nextInt();
return num;
}
public void input() //2nd function
{
System.out.println("enter first number:");
a=s1.nextInt();
System.out.println("enter second number:");
b=s1.nextInt();
}
public int add(int x,int y) //3rd function
{
int z=x+y;
return z;
}
public int sub(int x,int y) //4th function
{
int z=x-y;
return z;
}
public int mul(int x,int y) //5th function
{
int z=x*y;
return z;
}
public int divide(int x,int y) //6th function
{
int z=x/y;
return z;
}
public int mod(int x,int y) //7th function
{
int z=x%y;
return z;
}
public int choice(int c) //8th function
{
int f=0;
if(c==1)
f=add(a,b);
else if (c==2)
f=sub(a,b);
else if (c==3)
f=mul(a,b);
else if (c==4)
f=divide(a,b);
else if (c==5)
f=mod(a,b);
return f;
}
}
//this calculator is not returning values..KINDLY help me in configuring my mistakes.. i want to be "code confident!". 😐
First of all this code lacks documentation.
You should document each and every logic that is used describing the logic on higher level of abstraction.
When debugging, try to use break-points, watch variables, stack-trace, it will give you immense knowledge about your own program.
If you are using normal notepad and command prompt, try debugging by printing the values.
something like this: int c=s1.nextInt();
System.out.println("c = "+c);
I notice that you are not using c anywhere.
int c=s1.nextInt();
num=s1.nextInt();
do you think this might be a problem.
Try inputing same value twice.