1. Rewrite the Sorting class so that both sorting algorithms put the values in descending order. 2. Modify the Contact class in Chapter 10 so that the sorting methods invoked in PhoneList.java keeps the Contact objects sorted by phone number

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

1. Rewrite the Sorting class so that both sorting algorithms put the values in descending order.

2. Modify the Contact class in Chapter 10 so that the sorting methods invoked in PhoneList.java keeps the Contact objects sorted by phone number.

```java
public class Sorting {
    public static void selectionSort(Comparable[] list) {
        int min;
        Comparable temp;

        for (int index = 0; index < list.length-1; index++) {
            min = index;
            for (int scan = index+1; scan < list.length; scan++) {
                if (list[scan].compareTo(list[min]) < 0)
                    min = scan;
            }

            // Swap the values
            temp = list[min];
            list[min] = list[index];
            list[index] = temp;
        }
    }

    public static void insertionSort(Comparable[] list) {
        for (int index = 1; index < list.length; index++) {
            Comparable key = list[index];
            int position = index;

            // Shift larger values to the right
            while (position > 0 && key.compareTo(list[position-1]) < 0) {
                list[position] = list[position-1];
                position--;
            }

            list[position] = key;
        }
    }
}
```

This Java code defines a `Sorting` class with two static methods for sorting arrays: `selectionSort` and `insertionSort`. Both methods utilize the `Comparable` interface to compare elements within the provided list.

### `selectionSort` Method:
- **Purpose**: Sorts the array using the selection sort algorithm.
- **Process**:
  - Iterates through the array to find the minimum element.
  - Swaps it with the first unsorted element.
  - Continues this process for each position in the array until it's sorted.

### `insertionSort` Method:
- **Purpose**: Sorts the array using the insertion sort algorithm.
- **Process**:
  - Starts from the second element and selects it as the key.
  - Shifts elements of the sorted segment to the right if they are greater than the key.
  - Inserts the key into the correct position.
  - Repeats these steps for each element until the list is sorted.
```
Transcribed Image Text:```java public class Sorting { public static void selectionSort(Comparable[] list) { int min; Comparable temp; for (int index = 0; index < list.length-1; index++) { min = index; for (int scan = index+1; scan < list.length; scan++) { if (list[scan].compareTo(list[min]) < 0) min = scan; } // Swap the values temp = list[min]; list[min] = list[index]; list[index] = temp; } } public static void insertionSort(Comparable[] list) { for (int index = 1; index < list.length; index++) { Comparable key = list[index]; int position = index; // Shift larger values to the right while (position > 0 && key.compareTo(list[position-1]) < 0) { list[position] = list[position-1]; position--; } list[position] = key; } } } ``` This Java code defines a `Sorting` class with two static methods for sorting arrays: `selectionSort` and `insertionSort`. Both methods utilize the `Comparable` interface to compare elements within the provided list. ### `selectionSort` Method: - **Purpose**: Sorts the array using the selection sort algorithm. - **Process**: - Iterates through the array to find the minimum element. - Swaps it with the first unsorted element. - Continues this process for each position in the array until it's sorted. ### `insertionSort` Method: - **Purpose**: Sorts the array using the insertion sort algorithm. - **Process**: - Starts from the second element and selects it as the key. - Shifts elements of the sorted segment to the right if they are greater than the key. - Inserts the key into the correct position. - Repeats these steps for each element until the list is sorted. ```
```java
public class Contact implements Comparable // Has method compareTo
{
    private String firstName, lastName, phone;

    //-----------------------------------------------------------------
    // Constructor: Sets up this contact with the specified data.
    //-----------------------------------------------------------------
    public Contact (String first, String last, String telephone)
    {
        firstName = first;
        lastName = last;
        phone = telephone;
    }

    //-----------------------------------------------------------------
    // Returns a description of this contact as a string.
    //-----------------------------------------------------------------
    public String toString ()
    {
        return lastName + ", " + firstName + "\t" + phone;
    }

    //-----------------------------------------------------------------
    // Returns a description of this contact as a string.
    //-----------------------------------------------------------------
    public boolean equals (Object other)
    {
        return (lastName.equals(((Contact)other).getLastName()) &&
                firstName.equals(((Contact)other).getFirstName()));
    // Use String equals
    }

    //-----------------------------------------------------------------
    // Uses both last and first names to determine ordering.
    //-----------------------------------------------------------------
    public int compareTo (Object other)
    {
        int result;

        String otherFirst = ((Contact)other).getFirstName();
        String otherLast = ((Contact)other).getLastName();

        if (lastName.equals(otherLast))
            result = firstName.compareTo(otherFirst); // String compareTo
        else
            result = lastName.compareTo(otherLast); // String compareTo

        return result;
    }

    //-----------------------------------------------------------------
    // First name accessor.
    //-----------------------------------------------------------------
    public String getFirstName ()
    {
        return firstName;
    }

    //-----------------------------------------------------------------
    // Last name accessor.
    //-----------------------------------------------------------------
    public String getLastName ()
    {
        return lastName;
    }
}
```

### Explanation

This Java class, `Contact`, implements the `Comparable` interface, enabling objects of this class to be compared based on their attributes. 

- **Attributes**: 
  - `firstName`, `lastName`, and `phone` are private instance variables of the class used to store contact details.

- **Constructor**: 
  - `Contact(String first, String last, String telephone)`: This initializes the contact's first name, last name, and phone number.

- **Methods**:
  - `toString()`: Returns a string representation of the contact in the format "LastName, FirstName Phone".
  - `equals(Object other)`: Checks if this contact is equal
Transcribed Image Text:```java public class Contact implements Comparable // Has method compareTo { private String firstName, lastName, phone; //----------------------------------------------------------------- // Constructor: Sets up this contact with the specified data. //----------------------------------------------------------------- public Contact (String first, String last, String telephone) { firstName = first; lastName = last; phone = telephone; } //----------------------------------------------------------------- // Returns a description of this contact as a string. //----------------------------------------------------------------- public String toString () { return lastName + ", " + firstName + "\t" + phone; } //----------------------------------------------------------------- // Returns a description of this contact as a string. //----------------------------------------------------------------- public boolean equals (Object other) { return (lastName.equals(((Contact)other).getLastName()) && firstName.equals(((Contact)other).getFirstName())); // Use String equals } //----------------------------------------------------------------- // Uses both last and first names to determine ordering. //----------------------------------------------------------------- public int compareTo (Object other) { int result; String otherFirst = ((Contact)other).getFirstName(); String otherLast = ((Contact)other).getLastName(); if (lastName.equals(otherLast)) result = firstName.compareTo(otherFirst); // String compareTo else result = lastName.compareTo(otherLast); // String compareTo return result; } //----------------------------------------------------------------- // First name accessor. //----------------------------------------------------------------- public String getFirstName () { return firstName; } //----------------------------------------------------------------- // Last name accessor. //----------------------------------------------------------------- public String getLastName () { return lastName; } } ``` ### Explanation This Java class, `Contact`, implements the `Comparable` interface, enabling objects of this class to be compared based on their attributes. - **Attributes**: - `firstName`, `lastName`, and `phone` are private instance variables of the class used to store contact details. - **Constructor**: - `Contact(String first, String last, String telephone)`: This initializes the contact's first name, last name, and phone number. - **Methods**: - `toString()`: Returns a string representation of the contact in the format "LastName, FirstName Phone". - `equals(Object other)`: Checks if this contact is equal
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education