Homework Help Question & Answers

in java Define a class named ComparableNames thats extends Person and implements comparable Define a class...

in java
Define a class named ComparableNames thats extends Person and implements comparable
Define a class named ComparableNames that extends Person (defined below) and implements Comparable. Override the compare To m
Define a class named ComparableNames that extends Person (defined below) and implements Comparable. Override the compare To method to compare the person on the basis of name. Write a test class to find the order of two ComparableNames. class Person String name; Person00 Person(String n) name=n: Hint: in class ComparableName, you only need a constructor with arg to initialize the data filed defined in the super class a method that override method compare to defined in interface Comparable. In Test class, create two objects of class ComparableName with two names and use method compare To: if both names are the same, print "both names are the same'. otherwise, print them in order.
0 0
Add a comment
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

Person.java
-----
class Person
{
   String name;
  
   Person(){}
   Person(String n)
   {
       name = n;
   }
}


ComparableNames.java
---------
class ComparableNames extends Person implements Comparable<Person>
{
   public ComparableNames(String n){
       super(n);
   }
  
   public int compareTo(Person p){
       return name.compareTo(p.name);
   }
}

TestComparableNames.java
-----------
public class TestComparableNames
{
   public static void main(String[] args){
       ComparableNames n1 = new ComparableNames("John");
       ComparableNames n2 = new ComparableNames("Bob");
       if(n1.compareTo(n2) == 0)
           System.out.println("both names are same");
       if(n1.compareTo(n2) < 0){
           System.out.println(n1.name);
           System.out.println(n2.name);
       }
       else if(n1.compareTo(n2) > 0){
           System.out.println(n2.name);
           System.out.println(n1.name);

       }
   }
}

Add a comment
Know the answer?
Add Answer to:
in java Define a class named ComparableNames thats extends Person and implements comparable Define a class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coin

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Design a class named Person and its two subclasses named Student and Employee. A...

    Java Programming Design a class named Person and its two subclasses named Student and Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, date hired. Define a class named MyDate that contains the fields year, month, and day. Override the toString method in each class to display the class name and the person's name....

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • In Java (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and...

    In Java (The Person, Student, Employee, Faculty, and Staff classes) Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other);...

    JAVA The Comparable interface is defined as follows: public interface Comparable<T> {         int compareTo(T other); } A Film class is defined as public class Film {      private String title;      private int yearOfRelease;           public Film(String title, int yearOfRelease) {           super();           this.title = title;           this.yearOfRelease = yearOfRelease;      }           public void display()      { System.out.println("Title " + title +". Release" + yearOfRelease);      } } Rewrite the Film class so that it...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • Create a class named GlobalWarming. In that class, define the following instance variables and methods:] Instance...

    Create a class named GlobalWarming. In that class, define the following instance variables and methods:] Instance Variables name: A String that is the name of the person taking the quiz. Methods GlobalWarming () - A default or no argument constructor that takes no parameters and initializes the instance variable to ‘null’. GlobalWarming (String name) - A constructor that takes one parameter that will be the name of the person taking the quiz, and that will be used to initialize the...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • Define a class named Employee . Derive this class from the class Person. An employee record...

    Define a class named Employee . Derive this class from the class Person. An employee record inherits an employee's name from the class Person. In addition, an employee record contains an annual salary (represented by a single value of type double), a hire date that gives the year hired (as a single value of type int), an identification number of type int, and a department of type String. Give your class a reasonable complement of constructors, accessors, mutators, an equals...

Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.