Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2006, 05:04 PM
Authorized User
 
Join Date: Dec 2005
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Flow of the Program ?

explain the Flow of the Program ?

how the items compared by the compare(Object a, Object b) method

while added to the TreeSet ?

please give me some idea...


class MyComp implements Comparator
{


    public int compare(Object a, Object b)
    {
        int i,j,k;System.out.println(" hai from Comparator");

        String sa=(String )a;
        String sb=(String )b;;

        i=sa.lastIndexOf(' ');
        j=sb.lastIndexOf(' ');

        k=sa.substring(i).compareTo(sb.substring(j));

        if(k==0)

            return sa.compareTo(sb);
        else
            return k;

    }
}


public class Array {




    public static void main(String[] args) {
                TreeSet a =new TreeSet(new MyComp());


a.add("Apple juice"); System.out.println(" hai from main for Apple " );

a.add("Grapes juice"); System.out.println(" hai from main for Grapes" );

a.add("Mango juice");System.out.println(" hai from main for Mango " );

a.add("Lemon juice");System.out.println(" hai from main for Lemon" );




            System.out.println("\n \n LAst \n \n ");
            Iterator i=a.iterator();

            while(i.hasNext())
            {

                System.out.println(i.next());



            }

            System.out.println("\n \n LAst \n \n ");

            System.out.println(" hai from main for Banana");
            a.add("Banana juice");
            System.out.println(" hai from main for caret");
            a.add("Caret juice");




            Iterator i1=a.iterator();
            while(i1.hasNext())
            {

                System.out.println(i1.next());



            }




    }

}
 
Old January 23rd, 2006, 01:42 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Is this your homework?

Here's your clues:
- Start at public static void main(String[] args) {

- Comparator looks like an interface, which MyComp implements.

- TreeSet seems to let you specify a class to be used for comparisons between Objects when you're instantiating it, that's what MyComp is.

I think you can figure it out from there

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
 
Old March 20th, 2006, 11:07 PM
Authorized User
 
Join Date: Mar 2006
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Have a look on this code. It may useful to u. :-

// Sorting ArrayList of Objects(Emp) based on Date of Birth (String)

import java.util.*;
import java.text.SimpleDateFormat;

public class SortObjects
{
        public void checkData()
        {

                ArrayList ar = new ArrayList();

ar.add(new Emp(5, "ram", "13-Sep-2005 03:00:39 PM"));

ar.add(new Emp(7, "shiva", "27-Oct-2005 09:08:28 AM"));

ar.add(new Emp(1, "raj", "29-Aug-2005 11:15:07 AM"));

ar.add(new Emp(4, "amar", "13-Mar-2005 10:29:18 AM"));

ar.add(new Emp(6, "bhanu", "08-Oct-2005 03:51:34 PM"));

ar.add(new Emp(11, "ramesh", ""));

ar.add(new Emp(2, "gopi", null));

           System.out.println("\nBefore Sorting :");

                for(int i=0; i<ar.size(); i++)
                {
                        Emp ob = (Emp) ar.get(i);
                    System.out.println(ob.toString());
                }

                Collections.sort(ar);

          System.out.println("\nAfter Sorting :");

                for(int i=0; i<ar.size(); i++)
                {
                        Emp ob = (Emp) ar.get(i);
                   System.out.println(ob.toString());
                }

        }

        public static void main(String args[])
        {

                SortObjects ob = new SortObjects();

                ob.checkData();

        }
}

class Emp implements Comparable
{
    private int eno;
    private String ename;
    private String dob;

    public Emp(int eno, String ename, String dob)
    {
        this.eno = eno;
        this.ename = ename;
        this.dob = dob;
    }

    public int getEno()
    {
        return eno;
    }

    public String getEname()
    {
        return ename;
    }

    public String getDob()
    {
        return dob;
    }

    public String toString()
    {
          return eno + "\t" + ename + "\t" + dob;
    }

public int compareTo(Object ob) throws ClassCastException
    {
Emp temp = (Emp)ob; // If this doesn't work, ClassCastException is thrown

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a");

                Date date1 = null, date2 = null;

        try {
    if ((dob!=null)&&(!dob.equalsIgnoreCase(""))) {

            date1 = sdf.parse(dob);}
if ((temp.dob!=null)&&(!temp.dob.equalsIgnoreCase("") )) {

            date2 = sdf.parse(temp.dob);}
        }
        catch(Exception e) {
            e.printStackTrace();
        }

              if ((date1!=null)&&(date2!=null) ){
        return date2.compareTo(date1);}else{return 0;}
    }

}








Similar Threads
Thread Thread Starter Forum Replies Last Post
Flow chart of Cobol program in dotNet SOANS General .NET 0 November 20th, 2007 11:29 PM
Program Flow Interrupted By IF...THEN Statement squeege321 Pro VB 6 10 March 23rd, 2007 11:38 AM
weird program flow with nested functions zayasv General .NET 1 January 6th, 2006 11:06 AM
weird program flow with nested loops zayasv Intro Programming 2 November 17th, 2005 06:19 AM
weird program flow with nested functions zayasv VB.NET 0 October 26th, 2005 11:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.