public class Person private String name private int age pub

public class Person
{
private String name;
private int age;


public Person()
{
name=\"\";
age=0;
}
/**
Create a person with a given name and age.
@param name the name
@param age the age
*/
public Person(String name, int age)
{
this.name = name;
this.age = age;
}

/**
Get the name.
@return the name
*/
public String getName()
{   
return name;
}

/**
Change the name.
@param name the name
*/
public void setName(String name)
{   
this.name = name;
}

/**
Get the age.
@return the age
*/
public int getAge()
{   
return age;
}

/**
Change the age.
@param age the age
*/
public void setAge()
{   
this.age = age;
}


/**
Convert person to string form.
*/
  
public String toString()
{
return \"Name: \" + name + \"\\t\" + \"Age: \" + age;
}

}

OUTPUT should look like this!

Solution

solution

package com.prt.test;
public class Person
{
protected String name;
protected int age;

public Person()
{
name=\"\";
age=0;
}
/**
Create a person with a given name and age.
@param name the name
@param age the age
*/
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
/**
Get the name.
@return the name
*/
public String getName()
{   
return name;
}

/**
Change the name.
@param name the name
*/
public void setName(String name)
{   
this.name = name;
}

/**
Get the age.
@return the age
*/
public int getAge()
{   
return age;
}

/**
Change the age.
@param age the age
*/
public void setAge()
{   
this.age = age;
}


/**
Convert person to string form.
*/
  
public String toString()
{
return \"Name: \" + name + \"\\t\" + \"Age: \" + age;
}


}

package com.prt.test;

public class Student extends Person implements Comparable<Student>{
private int studentId;
  
   private String major;

  
   /**
   * @return the studentId
   */
   public int getStudentId() {
       return studentId;
   }


   /**
   * @param studentId the studentId to set
   */
   public void setStudentId(int studentId) {
       this.studentId = studentId;
   }


   /**
   * @return the major
   */
   public String getMajor() {
       return major;
   }


   /**
   * @param major the major to set
   */
   public void setMajor(String major) {
       this.major = major;
   }


   public Student(String name, int age, int studentId, String major) {
       super(name, age);
       this.studentId = studentId;
       this.major = major;
   }


   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"name:\" + name+ \"\\t\"+\"\\t\"+\"age:\"+ age+\"\\t\"+ \"studentId :\"+ studentId + \"\\t\" +\" major:\"+ major;
   }

  


   public int compareTo(Student st){
       if(studentId==st.studentId)
       return 0;
       else if(studentId>st.studentId)
       return 1;
       else
       return -1;
       }

}

package com.prt.test;

public class Instructor extends Person implements Comparable<Instructor> {

   private double salary;

   public Instructor(String name, int age, double salary) {
       super(name, age);
       this.salary = salary;
   }

   /**
   * @return the salary
   */
   public double getSalary() {
       return salary;
   }

   /**
   * @param salary
   * the salary to set
   */
   public void setSalary(double salary) {
       this.salary = salary;
   }

   public int compareTo(Instructor is) {
       if (salary == is.salary)
           return 0;
       else if (salary > is.salary)
           return 1;
       else
           return -1;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"name:\" + name + \"\\t\" + \"age:\" + age + \"\\t\" + \"salary=\" + salary;
   }

}

package com.prt.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
   static List<Student> list = null;

   public static List<Student> read(File file) throws IOException {

       BufferedReader br = new BufferedReader(new FileReader(file));
       list = new ArrayList<Student>();

       String line = null;
       int count = 0;
       while ((line = br.readLine()) != null) {
           ++count;
           if (count == 1) {
               continue;
           }

           String[] words = line.split(\"\\\\s+\");
           // System.out.println(words.length);

           // int num = Integer.parseInt(words[0]);
           String name = words[0];
           int age = Integer.parseInt(words[1]);
           int studentId = Integer.parseInt(words[2]);
           String major = words[3];

           Student student = new Student(name, age, studentId, major);
           list.add(student);
       }
       // System.out.println(count);
       return list;

   }
}

package com.prt.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class FileUtil1 {
   static List<Instructor> list = null;

   public static List<Instructor> read(File file) throws IOException {

       BufferedReader br = new BufferedReader(new FileReader(file));
       list = new ArrayList<Instructor>();

       String line = null;
       int count = 0;
       while ((line = br.readLine()) != null) {
           ++count;
           if (count == 1) {
               continue;
           }

           String[] words = line.split(\"\\\\s+\");
           String name = words[0];
           int age = Integer.parseInt(words[1]);
           double salary = Double.parseDouble(words[2]);
           Instructor instructor = new Instructor(name, age, salary);
           list.add(instructor);
       }
       return list;

   }

}

package com.prt.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test1 {
   static File file = new File(\"D:\\\\data1.txt\");
   static File file1 = new File(\"D:\\\\data2.txt\");

   public static void main(String[] args) throws IOException {

       List<Student> list = FileUtil.read(file);
       List<Instructor> list1 = FileUtil1.read(file1);
       Collections.sort(list);
       Collections.sort(list1);

System.out.println(\"the list of students sorted by studentid\");

       for (Student student : list) {
           System.out.println(student);
       }

       System.out.println(\"===============================================\");

System.out.println(\"the list of instructors sorted by their salaries:\");

       for (Instructor instructor : list1) {
           System.out.println(instructor);
       }

   }

}

output

the list of students sorted by studentid

name:Eric age:19 studentId :11111 major:Engineering
name:Larry age:20 studentId :12345 major:CPSC
name:John age:17 studentId :23434 major:CPSE
name:Jennifer age:20 studentId :33333 major:Biology
name:Amy age:23 studentId :34343 major:IT
name:Christina age:19 studentId :55555 major:French
name:Mike age:21 studentId :77777 major:Chemistry
name:Ashley age:18 studentId :99923 major:ENGL


===============================================

the list of instructors sorted by their salaries
name:Jenny age:29 salary=35343.0
name:Kathy age:45 salary=39888.43
name:Chris age:35 salary=46233.0
name:Alex age:42 salary=55599.21
name:steve age:50 salary=98000.5
name:Shala age:55 salary=1500010.23

public class Person { private String name; private int age; public Person() { name=\
public class Person { private String name; private int age; public Person() { name=\
public class Person { private String name; private int age; public Person() { name=\
public class Person { private String name; private int age; public Person() { name=\
public class Person { private String name; private int age; public Person() { name=\
public class Person { private String name; private int age; public Person() { name=\
public class Person { private String name; private int age; public Person() { name=\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site