Programming First create a class called Person that implemen

Programming....

First, create a class called Person that implements the Comparable interface. Person will contain a member variable to hold a name, a constructor that sets up the name, and three methods (getName, compareTo, toString). getName returns the member variable, compareTo works as defined by the Comparable interface, and toString returns the name neatly formatted. Second, you\'ll compare persons by their names. Include a main method in Person that asks the user to input ten names and generates ten Person objects. A blank name should not be used to create an object. Using the compareTo method, determine and the first and last person among them and print them. Do not simply sort the Person data. Sample Output:

Solution

Please find my implementation.

Please let me know in case of any issue.

############### Person.java ##############

public class Person implements Comparable<Person>{

  

   // instance variable

   private String name;

  

   // constructor

   public Person(String n){

       name = n;

   }

  

   public String getName(){

       return name;

   }

  

   @Override

   public String toString() {

       return \"Person[name = \"+name+\"]\";

   }

   @Override

   public int compareTo(Person o) {

       return name.compareTo(o.getName());

   }

}

############# PersonTest.java ################

public class PersonTest {

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

      

       // creating Array of Person

       Person[] persons = new Person[10];

      

       // creating reader to read name

       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      

       System.out.println(\"Enter 10 names\" );

       int i=0;

       for(i=0; i<10; i++){

           System.out.println(\"Please enter person\'s name or blank to quit\");

           String name = br.readLine();

          

           if(name.equals(\"\"))

               break;

          

           // creating Person Object and strong in array

           persons[i] = new Person(name);

       }

      

       // printing firs and last name

System.out.println(\"First: \"+persons[0]);

System.out.println(\"Last: \"+persons[i-1]);

   }

}

/*

Sample run:

Enter 10 names

Please enter person\'s name or blank to quit

A

Please enter person\'s name or blank to quit

B

Please enter person\'s name or blank to quit

C

Please enter person\'s name or blank to quit

D

Please enter person\'s name or blank to quit

First: Person[name = A]

Last: Person[name = D]

*/

Programming.... First, create a class called Person that implements the Comparable interface. Person will contain a member variable to hold a name, a constructo
Programming.... First, create a class called Person that implements the Comparable interface. Person will contain a member variable to hold a name, a constructo
Programming.... First, create a class called Person that implements the Comparable interface. Person will contain a member variable to hold a name, a constructo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site