|
-
April 15th, 2009, 07:28 PM
#1
sorting using comparable
I am unable to execute these codes. This code is a replica from a website.
I used Testing.java and Person.java. Should the files be named comparable.ex01.Testing.java and comparable.ex01.Person.java?
As I am pretty new to java I have this doubt.
Code:
package comparable.ex01;
import java.util.Arrays;
import java.util.ArrayList;
public class Testing {
public static void main(String[] args) {
Person[] persons = new Person[4];
persons[0] = new Person();
persons[0].setFirstName("Elvis");
persons[0].setLastName("Goodyear");
persons[0].setAge(56);
persons[1] = new Person();
persons[1].setFirstName("Stanley");
persons[1].setLastName("Clark");
persons[1].setAge(8);
persons[2] = new Person();
persons[2].setFirstName("Jane");
persons[2].setLastName("Graff");
persons[2].setAge(16);
persons[3] = new Person();
persons[3].setFirstName("Nancy");
persons[3].setLastName("Goodyear");
persons[3].setAge(69);
System.out.println("Natural Order");
for (int i=0; i<4; i++) {
Person person = persons[i];
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
Arrays.sort(persons);
System.out.println();
System.out.println("Sorted by age");
for (int i=0; i<4; i++) {
Person person = persons[i];
String lastName = person.getLastName();
String firstName = person.getFirstName();
int age = person.getAge();
System.out.println(lastName + ", " + firstName + ". Age:" + age);
}
}
}
Code:
package comparable.ex01;
class Person implements Comparable {
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int compareTo(Object anotherPerson) throws ClassCastException {
if (!(anotherPerson instanceof Person))
throw new ClassCastException("A Person object expected.");
int anotherPersonAge = ((Person) anotherPerson).getAge();
return this.age - anotherPersonAge;
}
}
-
April 15th, 2009, 09:02 PM
#2
Re: sorting using comparable
I tried executing your code. Works for me.
I think that you are not putting the files in the right package folders.
Try executing them by putting them under comparable/ex01/Testing.java and comparable/ex01/Person.java
should work.
Other thing.. as you said you are new to java I hope that you have set the classpath correctly.
-
April 15th, 2009, 11:36 PM
#3
Re: sorting using comparable
hi.... I created package/ex01/Person.java and it compiled.
however when I try package/ex01/Testing.java it is not compiling
C:\Users\comparable\ex01>javac Te
Testing.java:9: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
Person[] persons = new Person[4];
^
Testing.java:9: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
Person[] persons = new Person[4];
^
Testing.java:10: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
persons[0] = new Person();
^
Testing.java:15: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
persons[1] = new Person();
^
Testing.java:20: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
persons[2] = new Person();
^
Testing.java:25: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
persons[3] = new Person();
^
Testing.java:33: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
Person person = persons[i];
^
Testing.java:46: cannot find symbol
symbol : class Person
location: class comparable.ex01.Testing
Person person = persons[i];
^
8 errors
please let me know where I am going wrong
-
April 16th, 2009, 12:55 AM
#4
Re: sorting using comparable
It works fine on my machine. Here is what I get as output:
Code:
E:\java-work\comparable\ex01>javac Person.java
E:\java-work\comparable\ex01>javac Testing.java
E:\java-work\comparable\ex01>java Testing
Natural Order
Goodyear, Elvis. Age:56
Clark, Stanley. Age:8
Graff, Jane. Age:16
Goodyear, Nancy. Age:69
Sorted by age
Clark, Stanley. Age:8
Graff, Jane. Age:16
Goodyear, Elvis. Age:56
Goodyear, Nancy. Age:69
E:\java-work\comparable\ex01>
I had the same problem previously. Everything worked fine after setting the classpath correctly.
Here are some links which might help you.
http://forums.sun.com/thread.jspa?threadID=670019
http://forums.sun.com/thread.jspa?threadID=753450
http://www.sharkyforums.com/showthread.php?t=281641
Try including the current directory in your classpath.
-
April 16th, 2009, 02:43 AM
#5
Re: sorting using comparable
Thanks for the help! The first thread showed me how to fix the problem. I used
javac -cp . comparable/ex01/Testing.java to compile
I dint change the path because it worked fine for many other java programs.
-
April 16th, 2009, 02:46 AM
#6
Re: sorting using comparable
You need your classpath set correctly. You may find this tutorial helpful: Path & Classpath.
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time...
T. Cargill
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|