0
import java.util.*;
public class quadrado {
    public static void main (String args[]) {
        int xp1,xp2,xp3,xp4,yp1,yp2,yp3,yp4;
        double c1,c2,c3;
        Scanner aa = new Scanner (System.in);
            System.out.print("Insira a abcissa do P1\n");
            xp1 = aa.nextInt();
            System.out.print("Insira a ordenada do P1\n");
            yp1 = aa.nextInt();

            System.out.print("Insira a abcissa do P2\n");
            xp2 = aa.nextInt();
            System.out.print("Insira a ordenada do P2\n");
            yp2 = aa.nextInt();

            System.out.print("Insira a abcissa do P3\n");
            xp3 = aa.nextInt();
            System.out.print("Insira a ordenada do P3\n");
            yp3 = aa.nextInt();

            System.out.print("Insira a abcissa do P4\n");
            xp4 = aa.nextInt();
            System.out.print("Insira a ordenada do P4\n");
            yp4 = aa.nextInt();

            c1= Math.sqrt((xp2-xp1)^2 + (yp2-yp1)^2);
            c2= Math.sqrt((xp3-xp2)^2 + (yp3-yp2)^2);
            c3= Math.sqrt((xp4-xp3)^2 + (yp4-yp3)^2);

            System.out.print("A figura é um quadrado: "+c1);

            if (c1==c2==c3) {                                           // sendo os lados consecutivos
                System.out.print("A figura é um quadrado"); }
            else {
                System.out.print("A figura não é um quadrado"); }


        }
    }   

I'm trying to write something that given the coordinates of 4 points decides if it forms a square. I decided to get the lenght of 3 vectors and, iff they are all the same size, it says it is a square. I'm a beginner so i really need help :/

5 Answers5

4
if (c1==c2==c3)

Lets break this down:

c1==c2

This is either true or false, lets call this b1.

b1 == c3

b1 is a boolean, c3 is still a double - error

Also, == can cause problems with double. Although it's use should be fine here, and in the majority of places, it's something to bear in mind.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
2

You can't write c1==c2==c3 as this means (c1==c2)==c3 so if c1 == c2 you are saying true == c3 which doesn't make sense.

Most likely you intended

if (c1 == c2 && c2 == c3)

Note: while double can have representation error, in this case that error would be consistent and you don't need to check for a small calculation error.

Conversely if you have non integers which have been calculated via a different means, you need to allow for some error (or use rounding)

 double a = input();
 double b = input();
 double c = input();

 if (a == b && b == c) // ok
 if (a + b == c) // not ok, needs rounding or tolerance.

You should also note that ^ mean bitwise XOR, not power of. You can instead do

 double c1 = Math.sqrt(Math.pow(xp2-xp1, 2) + Math.pow(yp2-yp1, 2));
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

When comparing doubles, you should always allow a small tolerance because you usually get roundoff errors:

static final double TOL = 1.0e-8;
if (Math.abs(c1-c2) < TOL && Math.abs(c1-c3) < TOL) ...

By the way, it is not sufficient to check if all sides have equal lengths because this is also the case for a diamond shape. You also need to check for right angles.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
0

You need to write if c1==c2 && c2 ==c3 instead.

Your compiler is complaining since c1==c2==c3 is evaluated as (c1==c2)==c3 and this is not allowed in java since the c1==c2 is a boolean type.

Note that due to imprecision in floating point, it would be better if you compared for equality before taking the square roots as those types are integers. That said, it doesn't actually matter in this case since the computed lengths are not combined in any way.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

You cannot compare more than two things like that. Java is comparing c1 & c2, then it is comparing the result of c1 & c2 with c3. If you want to compare multiple variables you need to do it with more if statements.

if (c1 == c2 && c1 == c3 && c2 == c3) {...}

You can also do it with a method like that:

public boolean allSame(Object... os) {
     Object first = os[0];

     for (Object o : os) 
         if (!first.equals(os)) return false;

     return true;
}
  • Comparing `c2 == c3` should always be redundant. for example see your second method which doesn't do this (though it compares the first element to itself) – Peter Lawrey Oct 09 '16 at 12:44
  • I'm interested in what you say here Peter. If you have time, might you be able to expand on this? My instinct is not to compute the unnecessary square roots. – Bathsheba Oct 09 '16 at 12:47
  • @PeterLawrey: Except that in his second method it actually is necessary to compare `c2` and `c3`, because while `==` is guaranteed to be transitive, `.Equals` is not. – Eric Oct 09 '16 at 12:47
  • @Bathsheba You don't need to compute any `sqrt` here. If `a == b` then `sqrt(a) == sqrt(b)` In this case, the converse happens to be true as the inputs are integers. – Peter Lawrey Oct 09 '16 at 12:51