#simple low level program question using methods , objects
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
import java.util.*;
class addfunc
{
int a,b,ans,d;
addfunc(int m,int n)
{
a=m;
b=n;
}
void calculate()
{
ans=a+b;
}
void display1()
{
System.out.println("Answer is "+ans);
d=ans;
}
void display2()
{
System.out.println("Answer is "+ans);
}
public static void main(String args[])
{
int f,s,rep=0,d;
Scanner sc=new Scanner(System.in);
do
{
if(rep==1||rep==0)
{
System.out.println("enter the first number and second number");
f=sc.nextInt();
s=sc.nextInt();
addfunc ob=new addfunc(f,s);
ob.calculate();
ob.display1();
}
else
{
System.out.println("enter the second number");
s=sc.nextInt();
ob.display1();
f=d;
addfunc ob=new addfunc(f,s);
ob.calculate();
ob.display2();
}
System.out.println("press 1 to retry , 2 to continue , 3 to end");
rep=sc.nextInt();
}while(rep==1||rep==2);
}
}
Detected code, here are some useful tools:
import java.util. * ;
class addfunc {
int a, b, ans, d;
addfunc(int m, int n) {
a = m;
b = n;
}
void calculate() {
ans = a + b;
}
void display1() {
System.out.println("Answer is " + ans);
d = ans;
}
void display2() {
System.out.println("Answer is " + ans);
}
public static void main(String args[] ) {
int f, s, rep = 0, d;
Scanner sc = new Scanner(System.in);
do {
if (rep == 1 || rep == 0) {
System.out.println("enter the first number and second number");
f = sc.nextInt();
s = sc.nextInt();
addfunc ob = new addfunc(f, s);
ob.calculate();
ob.display1();
}
else {
System.out.println("enter the second number");
s = sc.nextInt();
ob.display1();
f = d;
addfunc ob = new addfunc(f, s);
ob.calculate();
ob.display2();
}
System.out.println("press 1 to retry , 2 to continue , 3 to end");
rep = sc.nextInt();
}
while (rep == 1 || rep == 2);
}
}
instead of having a method which calculates variables you have in your class, you should have a parameter which the method gets and need to calculate it and when it finished it should return that value
and do not create method display just for one single line of System.out.print
ok thanks dude
i appreciate it
how do i delete this thread?