Results 1 to 8 of 8
  1. #1
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default need help understanding part of code

    im learning java from a book and i came across an example and i need help understanding what a part of it does since the book doesn't always explain new parts:
    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 quantityIn)
    		{
    			id = idIn;
    			name = nameIn;
    			retail = Double.parseDouble(retailIn);
    			quantity = Integer.parseInt(quantityIn);
    			
    			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;
    	}
    	
    	}
    and at this part i dont understand what these lines do:
    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;
    		}

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    it is used by saying
    int i = item1.compareTo(item2);
    if the price variable in item1 is less than the price value in item2 then it returns 1 if it is greater it returns -1 and if it is = then it returns 0

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

    Default

    If you're using Java 1.5 or above, I'd strongly recommend that you use the generic version of this:
    Java Code:
    public class Item implements Comparable<Item> {
      private String id;
      private String name;
      private double retail;
      private int quantity;
      private double price;
    
      Item(String idIn, String nameIn, String retailIn,
          String quantityIn) {
        id = idIn;
        name = nameIn;
        retail = Double.parseDouble(retailIn);
        quantity = Integer.parseInt(quantityIn);
    
        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(Item temp) {
        // 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;
      }
    
    }
    This is a little safer to use as the compiler will prevent you from comparing an Item object to a completely different object such as a String.

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    ... and that this compareTo() method and its convention of returning -1, 0, +1 is defined in the [built in to the Java standard runtime] "Comparable" interface, which this Item class implements. This provides a feature similar to the facility that would compare two strings (if the two strings are the same, or one is greater than or less than the other, such as longer length, or lexographically), except to any object that you make implement the Comparable interface.

    Testing something like an abstract data type to see how it compares to another instance of this type of object, is useful for when the object would be placed in a container that naturally sorts them, such as a sorted list, or if you want to test to see if the object's internal fields match another instance of this type of object. Where the rules for how to have an object compared (such as for sorting order) is defined by what you put in the body of the compareTo method. In this example, even though this item type has several fields, it will use the price as the thing that will compare it to another instance of the Item type, so if a bunch of these objects were to be placed in a sorted list they would be displayed in order of their price.
    Last edited by travishein; 01-21-2010 at 02:10 AM.

  5. #5
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    oh ok it compares the prices of each object and sorts them
    thanks

  6. #6
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    It doesn't sort them. It just returns whether or not one is > = or < the other. It is useful for sorting, but doesn't really 'sort' per se.

  7. #7
    cobra is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default Question along the same line

    I'm working on the exact same example, and I'm having hard time understanding a few expressions. I'd really appreciate any help.

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

    What is 'D' or what does it stands for in the above expressions?
    for eg: price = retail * .5D;

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by cobra View Post
    I'm working on the exact same example, and I'm having hard time understanding a few expressions. I'd really appreciate any help.

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

    What is 'D' or what does it stands for in the above expressions?
    for eg: price = retail * .5D;
    There are two types of floating point numbers: floats (4 bytes) and doubles (8 bytes). A double can store more fractional digits than a float can but it takes more memory; a 'D' or 'd' suffix indicates to the compiler that you're talking about a double type constant. A 'F' or 'f' suffix indicates a float type constant. The default floating point constant is double so a trailing 'D' isn't really needed, i.e. 05.D is the same as 0.5.

    kind regards,

    Jos
    Last edited by JosAH; 02-09-2011 at 07:14 PM.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Sychronizing a part of a code in a method
    By SarahB in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-03-2010, 10:22 PM
  2. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 PM
  3. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 AM
  4. Trouble implementing part of code into GUI
    By Flaresplitz in forum New To Java
    Replies: 1
    Last Post: 12-21-2008, 07:51 AM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 PM

Posting Permissions

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