2

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;
 }
}
DT7
1,60515 silver badges26 bronze badges
asked Oct 15, 2013 at 20:13
3
  • 2
    Why did you write x - x2 = x3;, but in a different method this.x = x;? What is being assigned to what? Commented Oct 15, 2013 at 20:14
  • What's the distance formula in math? It is the sqrt((x1-x2)^2+(y1-y2)^2). Make use of that in your distanceTo method. Commented Oct 15, 2013 at 20:16
  • Yea the this.x was so the user could set x value, the last method was to do some calculation to find the distance of point A from point B. There may have been a better way to assign the field but thats how we have been told but thanks for all the help guys ^_^ Commented Oct 15, 2013 at 23:44

3 Answers 3

5

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
answered Oct 15, 2013 at 20:16
Sign up to request clarification or add additional context in comments.

1 Comment

To put it another way, I find it useful to say that the = operator in Java and most other programming languages should not mentally be read as "equals" but rather "is defined as".
0

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

answered Oct 15, 2013 at 20:15

Comments

0

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.

answered Oct 15, 2013 at 20:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.