Hi my name is Knightofdurham but my friends call me Knight anyways i am a student in the Uk doing information systems and management. ANyways the reason I have registered to this website is because i need help with one of my java programs. Basically at the moment it sorts people in my array list by last name however i want to know how to sort by birthday to be more specific by their month. Its just that when i try to use the comparable on an int it just doenst work. To be honest my java is a bit weak but i am working on it. I am not asking you guys to do my homework cause i have done it but I am stumped and need some help please. here is my code

class Person
{
private String lastName;
private String firstName;
private int day;
private int month;
//----------------------------------------------------------
 
public Person(String last, String first, int d, int m)
{ // constructor
lastName = last;
firstName = first;
day = d;
month = m;
}
//----------------------------------------------------------
 
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Day: " + day);
System.out.println(", Month:" + month);
}
//----------------------------------------------------------
 
public String getLast() // get last name
{ return lastName; }
} // end class Person
////////////////////////////////////////////////////////////////
 
class ArrayInOb
{
private Person[] a; // ref to array a
private int nElems; // number of data items
 
//-------------------------------------------------------------
 
public ArrayInOb(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
//-------------------------------------------------------------
 
// put person into array
public void insert(String last, String first, int day, int month)
{
a[nElems] = new Person(last, first, day, month);
nElems++; // increment size
}
//-------------------------------------------------------------
 
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
System.out.println("");
}
//-------------------------------------------------------------
 
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
Person temp = a[out]; // remove marked person
in = out; // start shifting at out
while(in>0 && // until smaller one found,
 
a[in-1].getLast().compareTo(temp.getLast())>0)
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//-------------------------------------------------------------
 
} // end class ArrayInOb
////////////////////////////////////////////////////////////////

need help i think with my insdertion sort.

thanks

guys i apologize for the lack of code wrapping have no idea how to do it.

here is an example of my main

class ObjectSortApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ArrayInOb arr; // reference to array
arr = new ArrayInOb(maxSize); // create the array
arr.insert("Evans", "Patty", 24, 9);
arr.insert("Smith", "Doc", 19, 4);
arr.insert("Smith", "Lorraine", 37, 6);
arr.insert("Smith", "Paul", 17, 11);
arr.insert("Yee", "Tom", 13, 7);
arr.insert("Hashimoto", "Sato", 21, 12);
arr.insert("Stimson", "Henry", 29, 2);
arr.insert("Velasquez", "Jose", 12, 1);
arr.insert("Vang", "Minh", 22, 3);
arr.insert("Creswell", "Lucinda", 18, 10);
System.out.println("Before sorting:");
arr.display(); // display items
arr.insertionSort(); // insertion-sort them
System.out.println("After sorting:");
arr.display(); // display them again
} // end main()
} // end class ObjectSortApp
Member Avatar for iamthwee

[CODE]

Can you read? Heh heh? ;)

Yeah it looks ok. Instead of using the compareto method just compare ints like such...

int a =12; 
int b = 7;

if (a>b)
 then //do stuff

public void insertionSortMonth()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
Person temp = a[out]; // remove marked person
in = out; // start shifting at out
while(in>0 && // until smaller one found,

a[in-1].getMonth()>temp.getMonth())
{
a[in] = a[in-1]; // shift item to the right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()

You also need another method:

public int getMonth()
{
    return month;

}

Have a go see how far you get?

guys this is going to sound stupid but can some onbe describe how i would be able to create a method which would show whether a given set of data contains two people with the same birthday?

guys this is going to sound stupid but can some onbe describe how i would be able to create a method which would show whether a given set of data contains two people with the same birthday?

Assuming the sort part of your program is working ok now...

Sort the arrays by birthdate, then you'll have a list of accending/decending birthdates. If there are two items with the same birthdate then they will be next to each other. Instead having to compare each item in the array with every other item, simply go down the list comparing each birthday with the one after it.

Heres an abstract example. Have two variables that store date, e.g. CurrentDate, NextDate. Get the first item in the array's date and put into the CurrentDate variable then put the date of the next item into the NextDate variable.

Check to see if they are equal, if so do whatever it is you want to do in this situation. Otherwise set the CurrentDate to NextDate, and if another item exists set NextDate to that item's date value. Do this for the entire list until there are no more list items.

Sorry I can't provide the code as i'm fairly new at Java, but I think this should help. There may be a better way but this is just my suggestion ;).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.