Question

In: Computer Science

Consider the following incomplete declaration of a Name class for 2a-2c. public class Name something missing...

Consider the following incomplete declaration of a Name class for 2a-2c.

public class Name something missing

{

   private String first;

   private String last;

   public Name(String firstName, String lastName)

   {

      first = firstName;

     last = lastName;

   }

   //additional methods

}

2a) In the first two parts, you will modify the Name class so that it implements the Comparable interface. Show here what should go in place of something missing in the first line of the class declaration.

2b) In this part, implement the compareTo method. The standard ordering of names is to be used: alphabetical order by last name, and if the two last names are the same, alphabetical order by first name. Two names are considered the same if and only if both the first and last names are the same.

2c) Implement a toString method in the Name class, which takes no parameters and returns a String containing the full name: first name then last name. For example, if the first name is “John” and the last name is “Doe”, then toString should return “John Doe”.

Solutions

Expert Solution

1. If wnats to implement Comparable Interface then something missing should be replaced with

Comparable<Name>

and needs to implement method compareTo(Name name) method

Full Program code as below:

Create a java class with name Name.java


import java.util.ArrayList;
import java.util.Collections;


public class Name implements Comparable<Name> {

private String first;
private String last;

public Name(String firstName, String lastName) {
this.first = firstName;
this.last = lastName;
}

/*
In this part, implement the compareTo method. The standard ordering of names is to be used:
alphabetical order by last name, and if the two last names are the same, alphabetical order by first name.
Two names are considered the same if and only if both the first and last names are the same.
*/
@Override
public int compareTo(Name name) {

/*
* Sorting by last name. compareTo should return < 0 if this(keyword)
* is supposed to be less than name, > 0 if this is supposed to be
* greater than object name and 0 if they are supposed to be equal.
*/
int index = this.last.compareTo(name.last);
//Sorting by first name if last name is same d
return index == 0 ? this.first.compareTo(name.first) : index;
}

/*
Implement a toString method in the Name class,
which takes no parameters and returns a String containing
the full name: first name then last name. For example,
if the first name is “John” and the last name is “Doe”,
then toString should return “John Doe”.
  
*/
@Override
public String toString() {

return this.first + " " + this.last;
}

//driver method
public static void main(String[] args) {
//create arraylist to check output of the program
ArrayList<Name> list = new ArrayList<Name>();
//add firstName and lastName
list.add(new Name("Henry", "Miller"));
list.add(new Name("Nalo", "Hopkinson"));
list.add(new Name("Frank", "Miller"));
list.add(new Name("Deborah", "Hopkinson"));
list.add(new Name("George", "Martin"));
//needs to sort arraylist using Collection.sort(Object o)
Collections.sort(list);
//print the arraylist after sorting
for (Name name : list) {
System.out.println(name.toString());
}
}
}
__________________________________________________________________________________________

OUTPUT

Deborah Hopkinson
Nalo Hopkinson
George Martin
Frank Miller
Henry Miller

___________________________________________________________________________________________


Related Solutions

Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default    public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this Book...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default        public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include...
// File name: Person.h // Person class declaration. Person is the base class. #pragma once #include <iostream> using namespace std; class Person { private:         string fName;         string lName;         int birthYear;         int birthMonth;         int birthDay; public:         Person();         void setName(string, string);         void setBirthDate(int, int, int);         string getFullName();         string getBirthDate(); }; // File name: Person.cpp // Person class definition. Person is the base class. #include "Person.h" Person::Person() { fName = ""; lName =...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT