#simple low level program question using methods , objects

1 messages · Page 1 of 1 (latest)

karmic lintel
#

also i have to use methods to do that , the program i made is in the thread , i would love some corrections and shot tricks .
i have to make a simple program which features continuing , repeating and ending it.
it must be very easy for programmers who have/are studied/studying java/c++

vernal lakeBOT
#

<@&987246399047479336> please have a look, thanks.

vernal lakeBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

karmic lintel
#
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);
    }
}    
vernal lakeBOT
# karmic lintel ```java import java.util.*; class addfunc { int a,b,ans,d; addfunc(int m...

Detected code, here are some useful tools:

Formatted code
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);
  }
}
shut zenith
#

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

karmic lintel
#

ok thanks dude

karmic lintel
#

how do i delete this thread?