|
12 Feb 2003, 11:10
|
#1
|
ŻŻŻŻŻŻŻŻŻ
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
|
Java help
Here's one for you java users:
Quote:
Using the Student class provided write a program to create and array of students populated with data (minimum of 5). Print this array, then sort it on lastName and print the sorted array
|
Code:
Student.java
public class Student
{
private String firstName, lastName, id;
//-----------------------------------------------------------------
// Sets up this Student object with the specified initial values.
//-----------------------------------------------------------------
public Student (String first, String last, String number)
{
firstName = first;
lastName = last;
id = number;
}
// Returns this Student object as a string.
public String toString()
{
String result;
result = firstName + " " + lastName + " " + id + "\n";
return result;
}
}
Code:
import avi.*;
class ex1 {
public static void main(String[] args) {
Window screen = new Window("ex1,java");
Student[] list = new Student[5];
list[0] = new Student("Bob","Thomas","1");
list[1] = new Student("Tim","Armstrong","2");
list[2] = new Student("Liz","Hope","3");
list[3] = new Student("John","Smith","4");
list[4] = new Student("Billy","Nomates","5");
screen.showWindow();
for (int index=0; index != list.length; index++) {
screen.write(list[index].toString()+"\n");
}
}
}
As you can see, I can fill the array no problem and print it in order. But I have no idea to sort it on lastName. The Student class returns a String with the firstName, lastName and id all concatenated, and I see no way to read the first char of lastName.
Any help would be much appreciated.
__________________
in my sig i write down all my previous co-ords and alliance positions as if they matter because I'm not important enough to be remembered by nickname alone.
|
|
|
12 Feb 2003, 11:31
|
#2
|
Henry Kelly
Join Date: Apr 2000
Posts: 7,374
|
You could write a custom sorting routine that looks at each LastName in turn and puts them into a temporary array called Sorted or something, then copy the sorted array back...
You covered sorting algorithms yet?
|
|
|
12 Feb 2003, 12:18
|
#3
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
add a getLastName() method to your student class which returns the last name. Parsing the toString() data to get it would be insanely bad OO design.
|
|
|
12 Feb 2003, 13:44
|
#4
|
Ngisne
Join Date: Jul 2001
Location: right here
Posts: 79
|
are you permitted to use Collection framework or are you supposed to write your own sorting routine?
Code:
class LastNameCmp implements Comparator {
public int compare(Object o1, Object o2) {
return ((Student) o1).getLastName().compareTo(((Student) o2).getLastName());
}
}
....
Arrays.sort(list, new LastNameCmp());
or, if you are allowed to modify Student class, you can make it implement Comparable and define compareTo method instead of defining a separate comparator class.
__________________
down with signatures
|
|
|
12 Feb 2003, 20:11
|
#5
|
ŻŻŻŻŻŻŻŻŻ
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
|
For those interested in the solution, I found a new way to do it:
Code:
import java.lang.*;
public class Student implements Comparable
{
private String firstName, lastName, id;
//-----------------------------------------------------------------
// Sets up this Student object with the specified initial values.
//-----------------------------------------------------------------
public Student (String first, String last, String number)
{
firstName = first;
lastName = last;
id = number;
}
// Returns this Student object as a string.
public String toString()
{
String result;
result = firstName + " " + lastName + " " + id + "\n";
return result;
}
public int compareTo(Object object) {
return (((Student)this).lastName).compareTo(((Student)object).lastName);
}
}
Code:
import avi.*;
import java.util.*;
class ex1 {
public static void main(String[] args) {
Window screen = new Window("ex1.java");
Student[] list = new Student[5];
int index;
list[0] = new Student("Bob","Thomas","1");
list[1] = new Student("Tim","Armstrong","2");
list[2] = new Student("Liz","Hope","3");
list[3] = new Student("John","Smith","4");
list[4] = new Student("Billy","Nomates","5");
screen.showWindow();
screen.write("Array before sorting:\n\n");
for (index=0; index != list.length; index++) {
screen.write(list[index].toString());
}
Arrays.sort(list);
screen.write("\n\nArray after sorting:\n\n");
for (index=0; index != list.length; index++) {
screen.write(list[index].toString());
}
}
}
__________________
in my sig i write down all my previous co-ords and alliance positions as if they matter because I'm not important enough to be remembered by nickname alone.
|
|
|
12 Feb 2003, 20:18
|
#6
|
ŻŻŻŻŻŻŻŻŻ
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
|
Quote:
Originally posted by Atamur
|
Oops, just noticed your post there now. I ended up doing it that way, it would have been done a bit sooner if I was more awake though...
thx
__________________
in my sig i write down all my previous co-ords and alliance positions as if they matter because I'm not important enough to be remembered by nickname alone.
|
|
|
|
All times are GMT +1. The time now is 22:40.
| |