Results 1 to 8 of 8
- 01-21-2010, 12:06 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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:
and at this part i dont understand what these lines do: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; } }
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; }
- 01-21-2010, 12:09 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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
-
If you're using Java 1.5 or above, I'd strongly recommend that you use the generic version of this:
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.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; } }
- 01-21-2010, 02:08 AM #4
... 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.
- 01-21-2010, 09:41 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
oh ok it compares the prices of each object and sorts them
thanks
- 01-21-2010, 10:08 PM #6
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
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.
- 02-09-2011, 07:04 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
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;
- 02-09-2011, 07:11 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
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,
JosLast edited by JosAH; 02-09-2011 at 07:14 PM.
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Sychronizing a part of a code in a method
By SarahB in forum Threads and SynchronizationReplies: 1Last Post: 01-03-2010, 10:22 PM -
trouble understanding code help
By yasmin k in forum New To JavaReplies: 4Last Post: 11-16-2009, 09:46 PM -
Help on understanding a program
By newbie225 in forum New To JavaReplies: 1Last Post: 11-10-2009, 12:53 AM -
Trouble implementing part of code into GUI
By Flaresplitz in forum New To JavaReplies: 1Last Post: 12-21-2008, 07:51 AM -
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11-05-2007, 06:56 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks