DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005
    Posts
    1

    2 questions, please help!

    hello all, i just have a couple of questions (im also posting some code)

    1. if you would, please take a look at my code and let me know if my compareTo method is the best way to compare what i am trying to compare. i am basically trying to make a program that will read in 3 text files, and then build 3 lists based on the info in the text files. well the class i am posting (Order) is just one of the three main classes that will be used to sort through and determine the alphabetical order the information will be in. so please take a look and tell me if it looks okay.

    2. also, i am using jGrasp as my editor and i am trying to create a workbench so i can test my compareTo method. i tried to create 2 instances so i could compare them, but it keeps throwing an error when i try to enter a mock Date. the error says something along the lines of: int cannot be converted to type java.util.Date. i was just wondering why it is throwing this error?

    well please take a look and please help! thanks in advance!

    Code:
       import java.util.Comparator;
       import java.util.Date;
    
        public class Order implements Comparable <Order>
       {
          private String lastName;
          private String firstName;
          private String title;
          private Date dateOfPurchase;
       
           public Order(String firstName, String lastName, String title, Date dateOfPurchase)
          {
             this.lastName = lastName;
             this.firstName = firstName;
             this.title = title;
             this.dateOfPurchase = dateOfPurchase;
          }
       
           public void setLastName(String lastName)
          {
             this.lastName = lastName;
          }
       
           public String getLastName()
          {
             return lastName;
          }
       
           public void setFirstName(String firstName)
          {
             this.firstName = firstName;
          }
       
           public String getFirstName()
          {
             return firstName;
          }
       
           public void setTitle(String title)
          {
             this.title = title;
          }
       
           public String getTitle()
          {
             return title;
          }
       
           public void setDateOfPurchase(Date dateOfPurchase)
          {
             this.dateOfPurchase = dateOfPurchase;
          }
       
           public Date getDateofPurchase()
          {
             return dateOfPurchase;
          }
       
           public int compareTo(Order anOrder)
          {
             int result;
          	
             result = this.dateOfPurchase.compareTo(anOrder.dateOfPurchase);
             
             if (result == 0)
             {
                result = this.lastName.compareTo(anOrder.lastName);
             }
             else
             {	
                if(result ==0)
                {
                   result = this.firstName.compareTo(anOrder.firstName);
                }
                else	
                {	
                   result = this.title.compareTo(anOrder.title);
                }
             }		
          	
             return result;
          		
          }
       
           public boolean equals(Order anOrder)
          {
             
             boolean result;
             
             result = (this.dateOfPurchase.equals(anOrder.dateOfPurchase) && this.lastName.equals(anOrder.lastName)
                && this.firstName.equals(anOrder.firstName) && this.title.equals(anOrder.title));
          	
             return result;
         }

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    One thing to note with the code:
    Code:
           public int compareTo(Order anOrder)
          {
             int result;
          	
             result = this.dateOfPurchase.compareTo(anOrder.dateOfPurcha  se);
             
             if (result == 0)
             {
                result = this.lastName.compareTo(anOrder.lastName);
             }
             else
             {	
                if(result ==0) // This line is always false and so is redundent
                {
                   result = this.firstName.compareTo(anOrder.firstName);
                }
                else	
                {	
                   result = this.title.compareTo(anOrder.title);
                }
             }		
          	
             return result;
          		
          }
    Hope this helps.
    ~evlich

Similar Threads

  1. Replies: 13
    Last Post: 05-23-2005, 05:22 PM
  2. Replies: 0
    Last Post: 02-18-2005, 06:40 AM
  3. Stream object, encryption questions
    By Murray Foxcroft in forum VB Classic
    Replies: 0
    Last Post: 04-16-2002, 01:44 AM
  4. Multiple-choice questions in Access
    By Paul Gardner in forum VB Classic
    Replies: 1
    Last Post: 06-15-2001, 09:10 AM
  5. Questions
    By Kevin Gray in forum authorevents.kurata
    Replies: 1
    Last Post: 04-17-2000, 01:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links