Results 1 to 7 of 7
- 10-15-2009, 03:51 PM #1
I need some clarifications here..
I was copying a code from a book that I'm reading and studying right now then it came to my attention this code snippet:
Java Code:public int compareTo(Object obj) { Item temp = (Item)obj; if (this.price < temp.price) return 1; else if(this.price < temp.price) return -1; return 0; }
This snippet is from Item.java:
Java Code:package org.cadenhead.ecommerce; import java.util.*; public class Item implements Comparable { private String id; private String name; private double retail; private int quantity; private double price; Item(String idIn, String nameIn, String retailIn,String quanIn) { id = idIn; name = nameIn; retail = Double.parseDouble(retailIn); quantity = Integer.parseInt(quanIn); if (quantity > 400) price = retail * .5D; else if (quantity >200) price = retail * .6D; else price = retail * .7D; price = Math.floor(price * 100 + .5) / 100; } public int compareTo(Object obj) { Item temp = (Item)obj; if (this.price < temp.price) return 1; else if(this.price < temp.price) return -1; return 0; } public String getId () { return id; } public String getName() { return name; } public double getRetail(){ return retail; } public int getQuantity(){ return quantity; } public double getPrice(){ return price; } }
The compareTo() method compares two objects of a class: the current object and another
object passed as an argument to the method. The value returned by the method defines
the natural sorting order for objects of this class:
* If the current object should be sorted above the other object, return -1.
* If the current object should be sorted below the other object, return 1
* If the two objects are equal, return 0.
I would gladly appreciate your help so that I can progress more on Java and gain more coding techniques. Thank You.
- 10-15-2009, 03:56 PM #2
public int compareTo(Object obj) comes from the Comparable Interface. Item temp = (Item)obj; is called a cast the object reference is casted to a Item reference. obj doesn't have a field price, so you could not compare the two. And your book is wrong or you mistyped. It should be:
Java Code:public int compareTo(Object obj) { Item temp = (Item)obj; if (this.price < temp.price) return 1; else if(this.price > temp.price) // NOT < !!!! return -1; return 0; }
- 10-15-2009, 03:58 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 11
The line is a cast. The object came in as class type Object so we can't find out what it's price is when it's an object because the Object class doesn't have that field. Casting to Item means we are now dealing with an Item object and the compiler will allow us to call the Item methods/fields. If they had used generics then the cast would not have been necessary. I assume you are reading a Java 1.4 book?
- 10-16-2009, 04:43 AM #4
Thanks!
Hey thanks r035198x,PhHein. It clear to me now.
By the way r035198x, I'm reading a java 6 book (Sams teach yourself java 6 in 21 days) not a java 1.4, does this mean that this approach is depreciated?
Could you suggest good java books for beginners like me?
Thanks! :)
-
Not deprecated, but if the book were using generics, the class would be:
Java Code:public class Item implements Comparable<Item> {
Java Code:public int compareTo(Item temp) { if (this.price < temp.price) return 1; else if(this.price > temp.price) // NOT < !!!! return -1; return 0; }
Could you suggest good java books for beginners like me?
- 10-16-2009, 02:30 PM #6
Ok thank you. I'll be looking forward to that book that you just said. Thanks for the info.
- 04-14-2011, 04:41 PM #7
Anyway, Thank you Fubarable for suggesting me that book. I learned a lot from it.
Bookmarks