-5
import java.util.Scanner;
import java.lang.Math;

public class Assignment1{
         //instance variables
         double a;
     double b;
     double c;
    // method
    public double hypot(double x, double y){
        x = x*x;
        y = y*y;
        double z = x+y;
        z = Math.sqrt(z);
        return z;

    }
    // constructor
    public Assignment1(double a , double b, double c){
            this.a = a;
        this.b = b;
        this.c = c;
    }

    public static void main(String [] args){ // execution starts from here
            Assignment1 obj = new Assignment1(a,b,c);
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the values to compute the pythagoras    theorem");

        obj.a = in.nextDouble();
        obj.b = in.nextDouble();
        obj.c = hypot(a,b);
        System.out.println("The hypot is"+ c);


    }


}

What is the problem with this code... I m creating the objects... then it should worl fine !! Aint it???

Marco Bolis
  • 1,222
  • 2
  • 15
  • 31

4 Answers4

1

You should make a new class for Assignment. Having the main method here is a bit confusing. That said, the problem is that the first line of your main method:

Assignment1 obj = new Assignment1(a,b,c);

Accesses variables a, b, and c, which are non-static variables. This means they do not exist in the context of the static main function.

An alternative way to write this would be:

// Place this in a file called Assignment.java
import java.util.Scanner;
import java.lang.Math;

public class Assignment {

  double a;
  double b;
  double c;

  public double hypot(double x, double y) {
    x = x*x;
    y = y*y;
    double z = x+y;
    z = Math.sqrt(z);
    return z;
  }

  public Assignment(double a , double b, double c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }

} // end class Assignment

// Place this in a file called Main.java
public class Main {
  public static void main(String [] args){ // execution starts from here

    // Give some values to a, b, and c!
    double a = 1;
    double b = 1;
    double c = 1;

    Assignment obj = new Assignment(a,b,c);
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the values to compute the pythagoras    theorem");

    obj.a = in.nextDouble();
    obj.b = in.nextDouble();
    obj.c = hypot(a,b);
    System.out.println("The hypot is"+ c);
  } // end method main
} // end class Main

As a general rule of thumb, try leave your main class empty of everything except the main method. It's even clearer if you call the class that contains the main method public class Main. Hope this helps!

gdiazc
  • 2,108
  • 4
  • 19
  • 30
1

In your main method (which is static) you are trying to access a b and c fields which are non-static without any reference to Assignment1 instance.

You need to understand that non-static members of class belongs to instance of class and they can be access only via such instance.

In non-static methods like

void someMethod(){
    field = 42;
}

you can use field without reference to instance only because compiler will add this. to variable for you which will represent current instance. In static methods there is no this because static methods/fields belong to entire class, not to instances.

To solve your problem consider either first creating "unset" instance of Assignment1 class (lets say you set its values to 0) like

Assignment1 obj = new Assignment1(0, 0, 0);

then you can set its field to data from user like

obj.a = in.nextDouble();
obj.b = in.nextDouble();

and lastly you can calculate c value using

obj.c = obj.hypot(obj.a, obj.b);

BTW direct manipulation of object field is consider bad design. Instead you should create getters and setters (methods like getA() setA(int newA)) which will return or manipulate their values.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

a, b and c are instance members. You main method is a class member.

You're trying to mix the two, which won't work.

Also, since you don't really need a custom constructor for your Assignment1 class, you can delete it.

Instead, try something like this:

public static void main(String [] args){ // execution starts from here
        Assignment1 obj = new Assignment1();
        Scanner in = new Scanner(System.in);
        System.out.println("Enter the values to compute the pythagoras    theorem");

        obj.a = in.nextDouble();
        obj.b = in.nextDouble();
        obj.c = hypot(a,b);
        System.out.println("The hypot is"+ c);
 }
victorantunes
  • 1,141
  • 9
  • 20
1

Of course its wrong, you create a instance of Assignment1 with these Codes first: Assignment1 obj = new Assignment1(a,b,c);

where are a and b and c??

maybe you should use null first: Assignment1 obj = new Assignment1(null,null,null);

then use the Scanner.in to initialize a ,b and c

Qing
  • 1,401
  • 2
  • 21
  • 41