| | |
Need help tweaking my program
![]() |
•
•
Join Date: May 2006
Posts: 3
Reputation:
Solved Threads: 0
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
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.
Java Syntax (Toggle Plain Text)
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.
Last edited by cscgal; May 5th, 2006 at 9:58 pm. Reason: Code tags fixed
[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?
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?
•
•
Join Date: May 2006
Posts: 3
Reputation:
Solved Threads: 0
here is an example of my main
Java Syntax (Toggle Plain Text)
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
Last edited by cscgal; May 5th, 2006 at 9:58 pm. Reason: Code tags fixed
[CODE][/CODE]
Can you read? Heh heh?
Yeah it looks ok. Instead of using the compareto method just compare ints like such...
You also need another method:
Have a go see how far you get?
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)
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:
Java Syntax (Toggle Plain Text)
public int getMonth() { return month; }
Have a go see how far you get?
•
•
Join Date: May 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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?
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
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Running a program before loading Windows Explorer during booting (Visual Basic 4 / 5 / 6)
- 3d Program (Game Development)
Other Threads in the Java Forum
- Previous Thread: help with setting background color!!!
- Next Thread: Downloading files from websites
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






