user2884009
user2884009

Reputation: 35

getting class expected error (nooby)

letme first say, im new both to programming and this website. Its my first visit here and this is my second programming project! :)

Im getting some error at the bottom of this while compiling, getting red box over the - x2 and will probably be over - y2 as well. The error is unexpected type.

Basically doing a project to get the difference of points and this is not finished but Im not sure how to get around the compile error. At first I tried to add fields for storing x2 and y2 and that did get around this error only to give me another (I erased as im not suppose to add more fields). in the end I think I need return math.sqrt(double x3) + math.sqrt(double y3)

any advice is appreciated, im using bluejay. Im sure this may look easy to some of you Guru's keep in mind im new! :)

public class Point {
    //fields holding info for x,y
    private double x;
    private double y;

    //default constructor, set to 0.0
    public Point()  {
        x = 0.0;
        y = 0.0;
    }

    //constructor that user can set field values
    public Point (double x, double y) {
            this.x = x;
            this.y = y;
    }

    //This is optional. return value of x
    public double getx () {
        return x;
    }

    //This is optional. return value of y
    public double gety () {
        return y;
    }

    //take new parameter find distance between field parameter and new parameter 
    public double distanceTo (double x2, double y2) {
        x - x2 = x3;
        y - y2 = y3;
    }
}

Upvotes: 2

Views: 61

Answers (3)

Richard Tingle
Richard Tingle

Reputation: 17236

In Java (and most programming languages) you should not consider yourself to be writing equations but instructions:

= basically means make the left "thing" what the right "thing" currently is, or in the words of StormeHawke

the = operator in Java and most other programming languages should not mentally be read as "equals" but rather "is defined as"

so

x3= x - x2;

is valid because you can change x3 to be x - x2, but

x - x2 = x3;

is not valid because which thing on the left do you change: x, x2, or both?

This means that the following is a perfectly reasonable thing to write;

x=x+1;

As an equation this makes no sense, but as an instruction it makes perfect sense, its "make x equal to what x is now plus 1"

Other issues

  • After solving this you will also find that distanceTo() is defined as returning a double but doesn't, you need to return some "answer" or change its return to void
  • x3 is also not defined as "being" anything, it needs to be declared before/when its used, presumable as a double

Upvotes: 5

MGorgon
MGorgon

Reputation: 2607

You're getting class expected error because compiler doesn't know what x3 and y3 are. You must first declare them: double x3; in order to use them. Hoever, your implementation of distanceTo method should be changed also.

Upvotes: 0

Jay Nebhwani
Jay Nebhwani

Reputation: 976

x - x2 = x3; is incorrect syntax in Java and your distanceTo() method is supposed to return a double but you don't return anything.

Upvotes: 0

Related Questions