• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Comapring Shapes

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am trying to compare two shapes. They will only return true when their color is the same. They also must 'have the same vertices in the same order'. This is where I am getting tripped up. How do I 1. Compare the vertices lengths and 2. how to make sure they are in the same order? I have tried a lot of different things, but can't get it.

 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, this code:

should only be used on an integer type. Try
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip! That helps a lot with shortening my code.
 
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sure you didn't mean this. It will always be true.

Probably wanted this.


I'm going to assume from your text that 'verticies' holds a list of points and that you're being asked to compare the contents of the list, not just to compare the list length.
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So i changed it to

But it still isn't passing the test, I think it has to match the order of vertices, but I don't know how to do that.
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, you don't want to compare things with an == unless they are integer types. Read this.
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am looking at the test, and it runs several tests on the same method. The issue is one of the test only compares the color because the thats all the test assigns the square objects. However, the next test tests the color and vertices. So how can I make it so that it will pass both test, because right now to pass the test the squares have to have both color and vertices.
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What type is vertices? Does it have an equals() method? If so, it could be as easy as
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vertices are
 
Knute Snortum
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They have equals() methods, so the above will work.
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so each Square has an array of Points. And to see if one Square is equal to another Square, then among other things you have to decide whether they have the same Points in the same order. I'm not quite sure what that means. And when that happens to me I start writing down actual examples and start looking for patterns. So... could you provide us some examples?
 
Sheriff
Posts: 8523
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jackson Daniels wrote:vertices are

Where did you get Point class? Wrote yourself or took from java.awt.Point?
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Im not really sure what you mean by examples, like from the tester?
 
Jackson Daniels
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its from java.awt.Point
 
Liutauras Vilda
Sheriff
Posts: 8523
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jackson Daniels wrote:Sorry Im not really sure what you mean by examples, like from the tester?

I think it would help if you show us a relevant paragraph or two from your requirements. It seems there are quite few unclear things.
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jackson,

You may need to get the points of the corners and check to see if one set completely contains each individual point of the other.
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Morgan wrote:Jackson,

You may need to get the points of the corners and check to see if one set completely contains each individual point of the other.


The original post also says the points have to be in the same order.
 
Carey Brown
Saloon Keeper
Posts: 9738
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should check out this http://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java.
WARNING:
array1.equals(array2) is the same as array1 == array2, i.e. is it the same array.It's not what most people expect.

Arrays.equals(array1, array2) compares the contents of the arrays.
 
Paul Clapham
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jackson Daniels wrote:Sorry Im not really sure what you mean by examples, like from the tester?



Well, aren't you planning to test this method? You should have some examples of Square pairs which are equal and some pairs which aren't equal. And if that's all the specification you have then you should have your own questions about that specification. I've already asked what "in the same order" means. So let me throw out some examples. Each Square has four points which presumably represent its vertices. So are the Squares represented by A, B, C, D and B, C, D, A equal or not? They look the same when you display them on a screen, so why are they (or aren't they) equal?
 
Les Morgan
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jackson,

Each shape has a toString() method, take a look at converting the Shapes to Strings using the toString() method and then just use the String.equals() to see if the Strings are the same.

Les

Jackson Daniels wrote:Hello, I am trying to compare two shapes. They will only return true when their color is the same. They also must 'have the same vertices in the same order'. This is where I am getting tripped up. How do I 1. Compare the vertices lengths and 2. how to make sure they are in the same order? I have tried a lot of different things, but can't get it.

 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People have forgotten to refer you to three standard references about equals methods:
  • 1: Joshua Bloch, Effective Java™ chapter 3.
  • 2: Angelika Langer
  • 3: Odersky Spoon and Venners: see this FAQ link.
  •  
    Bartender
    Posts: 732
    10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jackson Daniels wrote:public boolean equals(Object shapes){
    if (shapes instance Square){
    Square sQ = (Square) other;



    What is "other" ? I think you you mean
     
    A tiny monkey bit me and I got tiny ads:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic