-2

The program I wrote

public class Angle{
int deg1,deg2, min1,min2;
    void accept()throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the angle parameters degree fb minute fb next degree");
    deg1= Integer.parseInt(br.readLine());
    min1= Integer.parseInt(br.readLine());
    deg2= Integer.parseInt(br.readLine());
    min2= Integer.parseInt(br.readLine());
}
int anglesum(int a,int b,int x, int y)
{
    int m= (int)(b+y)/60;

    int s=a+x+m;
    return(s);
}
void main()throws IOException
{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    Angle abc=new Angle();
    abc.accept();
    int e=abc.anglesum(deg1,min1,deg2,min2);
    System.out.println("The sum is ="+e+" degrees "+(min1+min2-60)+" minutes");
}

It accepts values when calling functions separately via object but not when running main function. What could be the problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 4
    It's not at all clear what you mean by "but no when running main function" - what happens, and what did you expect? It would help if you'd provide a [mcve], and ideally follow Java naming conventions too... (The fact that you're opening a `BufferedReader` around `System.in` twice seems suspicious, btw. Ideally, hard-code the data in your example code.) – Jon Skeet Sep 15 '16 at 07:38
  • 4
    Your class is full with problems: A) you violate java naming convents; class names go UpperCase **always**; whereas methods and variables should be using camelCase. B) really bad naming - use names that tell something, avoid single char names C) normally, **main** should be static and taking a string array. You should step back; and rethink what you actually want to achieve; and then add those things one by one; and after each step you run the compiler! – GhostCat Sep 15 '16 at 07:42
  • Since i am relatively new to programming I couldn't decide what parts to put in. Sorry for inconvenience. On creating `new angle()` and calling classes separately in the bottom bar the program works perfectly. But when calling the main function `s` returns as null. That's all, I think.I am adding `deg1` and `deg2` normally and adding `min1` and `min2` until the sum reaches 60 wherein the spillover gets printed separately while an entire 60 cycle adds as 1 to the sum of `deg1` and `deg2`. Hope that clears stuff. – PrometheusWithoutMind Sep 15 '16 at 07:48
  • Your main method should be like this `public static void main(String[] args){`. Read about it here http://stackoverflow.com/questions/11952804/explanation-of-string-args-and-static-in-public-static-void-mainstring-a – Ravikumar Sep 15 '16 at 08:30

1 Answers1

0

You problem is that you are calling method main that is method of object Angle and inside you are creating instance of another object of type Angle

You may change int e=abc.anglesum(deg1,min1,deg2,min2); to int e=abc.anglesum(abc.deg1, abc.min1, abc.deg2, abc.min2); then you probably get the result you want.

Same for System.out.println("The sum is ="+e+" degrees "+(abc.min1 + abc.min2-60)+" minutes");

1) You should move method main so that it is not part of object Angle

2) You may create new method in Angle

int anglesum() { return anglesum(deg1,min1,deg2,min2); }

then you may call in your main method (that will no longer be part of Angle):

abc.accept();
int e=abc.anglesum();
rudolf_franek
  • 1,795
  • 3
  • 28
  • 41