944,014 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1438
  • Java RSS
May 5th, 2006
0

Need help tweaking my program

Expand Post »
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

Java Syntax (Toggle Plain Text)
  1.  
  2. class Person
  3. {
  4. private String lastName;
  5. private String firstName;
  6. private int day;
  7. private int month;
  8. //----------------------------------------------------------
  9.  
  10. public Person(String last, String first, int d, int m)
  11. { // constructor
  12. lastName = last;
  13. firstName = first;
  14. day = d;
  15. month = m;
  16. }
  17. //----------------------------------------------------------
  18.  
  19. public void displayPerson()
  20. {
  21. System.out.print(" Last name: " + lastName);
  22. System.out.print(", First name: " + firstName);
  23. System.out.println(", Day: " + day);
  24. System.out.println(", Month:" + month);
  25. }
  26. //----------------------------------------------------------
  27.  
  28. public String getLast() // get last name
  29. { return lastName; }
  30. } // end class Person
  31. ////////////////////////////////////////////////////////////////
  32.  
  33. class ArrayInOb
  34. {
  35. private Person[] a; // ref to array a
  36. private int nElems; // number of data items
  37.  
  38. //-------------------------------------------------------------
  39.  
  40. public ArrayInOb(int max) // constructor
  41. {
  42. a = new Person[max]; // create the array
  43. nElems = 0; // no items yet
  44. }
  45. //-------------------------------------------------------------
  46.  
  47. // put person into array
  48. public void insert(String last, String first, int day, int month)
  49. {
  50. a[nElems] = new Person(last, first, day, month);
  51. nElems++; // increment size
  52. }
  53. //-------------------------------------------------------------
  54.  
  55. public void display() // displays array contents
  56. {
  57. for(int j=0; j<nElems; j++) // for each element,
  58. a[j].displayPerson(); // display it
  59. System.out.println("");
  60. }
  61. //-------------------------------------------------------------
  62.  
  63. public void insertionSort()
  64. {
  65. int in, out;
  66. for(out=1; out<nElems; out++) // out is dividing line
  67. {
  68. Person temp = a[out]; // remove marked person
  69. in = out; // start shifting at out
  70. while(in>0 && // until smaller one found,
  71.  
  72. a[in-1].getLast().compareTo(temp.getLast())>0)
  73. {
  74. a[in] = a[in-1]; // shift item to the right
  75. --in; // go left one position
  76. }
  77. a[in] = temp; // insert marked item
  78. } // end for
  79. } // end insertionSort()
  80. //-------------------------------------------------------------
  81.  
  82. } // end class ArrayInOb
  83. ////////////////////////////////////////////////////////////////

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.
Last edited by cscgal; May 5th, 2006 at 9:58 pm. Reason: Code tags fixed
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knightofdurham is offline Offline
3 posts
since May 2006
May 5th, 2006
0

Re: Need help tweaking my program

[CODE][/CODE]



http://bj.middlebury.edu/~briggs/CS1...nSort_java.txt

http://www.cs.princeton.edu/introcs/...Sort.java.html

comparing ints is different to comparing Strings isn't it?

Where's your main, let me see an example of your main?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 5th, 2006
0

Re: Need help tweaking my program

here is an example of my main
Java Syntax (Toggle Plain Text)
  1. class ObjectSortApp
  2. {
  3. public static void main(String[] args)
  4. {
  5. int maxSize = 100; // array size
  6. ArrayInOb arr; // reference to array
  7. arr = new ArrayInOb(maxSize); // create the array
  8. arr.insert("Evans", "Patty", 24, 9);
  9. arr.insert("Smith", "Doc", 19, 4);
  10. arr.insert("Smith", "Lorraine", 37, 6);
  11. arr.insert("Smith", "Paul", 17, 11);
  12. arr.insert("Yee", "Tom", 13, 7);
  13. arr.insert("Hashimoto", "Sato", 21, 12);
  14. arr.insert("Stimson", "Henry", 29, 2);
  15. arr.insert("Velasquez", "Jose", 12, 1);
  16. arr.insert("Vang", "Minh", 22, 3);
  17. arr.insert("Creswell", "Lucinda", 18, 10);
  18. System.out.println("Before sorting:");
  19. arr.display(); // display items
  20. arr.insertionSort(); // insertion-sort them
  21. System.out.println("After sorting:");
  22. arr.display(); // display them again
  23. } // end main()
  24. } // end class ObjectSortApp
Last edited by cscgal; May 5th, 2006 at 9:58 pm. Reason: Code tags fixed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knightofdurham is offline Offline
3 posts
since May 2006
May 5th, 2006
0

Re: Need help tweaking my program

[CODE][/CODE]

Can you read? Heh heh?

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


Java Syntax (Toggle Plain Text)
  1. int a =12;
  2. int b = 7;
  3.  
  4. if (a>b)
  5. 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:

Java Syntax (Toggle Plain Text)
  1. public int getMonth()
  2. {
  3. return month;
  4.  
  5. }

Have a go see how far you get?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
May 5th, 2006
0

Re: Need help tweaking my program

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Knightofdurham is offline Offline
3 posts
since May 2006
May 5th, 2006
0

Re: Need help tweaking my program

Quote originally posted by Knightofdurham ...
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 ;).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Psyqwix is offline Offline
3 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: help with setting background color!!!
Next Thread in Java Forum Timeline: Downloading files from websites





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC