User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 12 Feb 2003, 11:10   #1
xtothez
ŻŻŻŻŻŻŻŻŻ
 
xtothez's Avatar
 
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
xtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud of
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.
xtothez is offline   Reply With Quote
Unread 12 Feb 2003, 11:31   #2
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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?
pablissimo is offline   Reply With Quote
Unread 12 Feb 2003, 12:18   #3
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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.
Nodrog is offline   Reply With Quote
Unread 12 Feb 2003, 13:44   #4
Atamur
Ngisne
 
Join Date: Jul 2001
Location: right here
Posts: 79
Atamur is an unknown quantity at this point
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
Atamur is offline   Reply With Quote
Unread 12 Feb 2003, 20:11   #5
xtothez
ŻŻŻŻŻŻŻŻŻ
 
xtothez's Avatar
 
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
xtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud of
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.
xtothez is offline   Reply With Quote
Unread 12 Feb 2003, 20:18   #6
xtothez
ŻŻŻŻŻŻŻŻŻ
 
xtothez's Avatar
 
Join Date: May 2001
Location: Sept 2057
Posts: 1,813
xtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud ofxtothez has much to be proud of
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.
xtothez is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 22:40.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright İ2002 - 2018