Results 1 to 7 of 7
  1. #1
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default 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;
    
        }
    the Item temp = (Item)obj is the part that I don't understand quite much. What does this do? How did it came to that?

    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 book says it this way:

    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.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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;
    
    }

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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?

  4. #4
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default 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! :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by arnelcolar View Post
    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?
    Not deprecated, but if the book were using generics, the class would be:
    Java Code:
    public class Item implements Comparable<Item> {
    and the compareTo method would look a little cleaner, again because of generics:
    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?
    I've heard positive reports from the Head First book.

  6. #6
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default

    Ok thank you. I'll be looking forward to that book that you just said. Thanks for the info.

  7. #7
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default

    Anyway, Thank you Fubarable for suggesting me that book. I learned a lot from it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •