Need help tweaking my program

Reply

Join Date: May 2006
Posts: 3
Reputation: Knightofdurham is an unknown quantity at this point 
Solved Threads: 0
Knightofdurham Knightofdurham is offline Offline
Newbie Poster

Need help tweaking my program

 
0
  #1
May 5th, 2006
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

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Need help tweaking my program

 
0
  #2
May 5th, 2006
[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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: Knightofdurham is an unknown quantity at this point 
Solved Threads: 0
Knightofdurham Knightofdurham is offline Offline
Newbie Poster

Re: Need help tweaking my program

 
0
  #3
May 5th, 2006
here is an example of my main
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Need help tweaking my program

 
0
  #4
May 5th, 2006
[CODE][/CODE]

Can you read? Heh heh?

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


  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:

  1. public int getMonth()
  2. {
  3. return month;
  4.  
  5. }

Have a go see how far you get?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: Knightofdurham is an unknown quantity at this point 
Solved Threads: 0
Knightofdurham Knightofdurham is offline Offline
Newbie Poster

Re: Need help tweaking my program

 
0
  #5
May 5th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: Psyqwix is an unknown quantity at this point 
Solved Threads: 0
Psyqwix Psyqwix is offline Offline
Newbie Poster

Re: Need help tweaking my program

 
0
  #6
May 5th, 2006
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 ;).
Psyqwix ):0) "Gluttony is an emotional escape, a signal that something is eating us" - Peter de Vries
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC