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.

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);
    }
  }
}
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;    
  }
}

Recommended Answers

All 4 Replies

Stating why you are unable to execute that code might prove useful to someone inclined to offer advice on it.

Your file names are just fine, but I would remove the package declaration from "Testing", unless you have re-created the directory structure of those files.

I created package/ex01/Person.java and it compiled.
however when I try package/ex01/Testing.java it is not compiling

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
Edit/Delete Message

For start you missing package declaration on start of Person class which should be same as the one on start of testing class...

Peter is correct that you would need the same package declaration for the Person class - or you can remove the package statement from Testing. You can read more about using pakages to organize code here: http://java.sun.com/docs/books/tutorial/java/package/index.html

The easiest thing for you to do at this point is to remove the package statement from the top of your Testing class (and from Person if you have added one).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.