• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to get the credit cards with implements and comparable  RSS feed

 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags (<- link) when you post code. I have added them for you this time.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code returns card.balance regardless of which is larger. That's not right.

A concise way of comparing numbers when sorting is:


This follows the contract:
0 if the numbers are the same
negative or positive based on which is bigger. You have a double and not an int though. Can you see how to follow the contract but still preserve this behavior?

Hint: Can you make your code return 1, -1 or 0 depending on the scenario?
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:This code returns card.balance regardless of which is larger. That's not right.

A concise way of comparing numbers when sorting is:


This follows the contract:
0 if the numbers are the same
negative or positive based on which is bigger. You have a double and not an int though. Can you see how to follow the contract but still preserve this behavior?

Hint: Can you make your code return 1, -1 or 0 depending on the scenario?


I tried this but i'm think i am confusing myself on how to write it out

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the instanceof check because that's guaranteed to be true by the parameter type.
 
aaron butcher
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it

 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need line 4 and the ”otherCreditCard" variable? This is just as unnecessary as the instance of check.

The parameter is already declared as a CreditCard and there's absolutely no way it's going to be anything but a CreditCard. You are guaranteed to have a CreditCard object. If you want to use the name "otherCard" then rename your parameter. I would just use the name ”other".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!