Lab 6h Sorting Objects-2
Lab 6h Sorting Objects-2
1. Make Sorting6h.insertionSort() run in descending order - Modify the insertionSort() algorithm so that it
sorts in descending order rather than ascending order. Change RunNumbers6h.java to call insertionSort()rather
than selectionSort(). Run RunNumbers6h.java to make sure the sorting is correct.
The file Salesperson6h.java partially defines a class that represents a sales person. This is very similar to the Contact class in Listing
6.15 on page 324 of the book. However, a sales person has a first name, last name, and a total number of sales (an int) rather than
a first name, last name, and phone number.
3. Run RunWeeklySales6h class - The file RunWeeklySales6h.java contains a driver for testing the compareTo() method
and the sorting . Compile and run it. Make sure your compareTo() method is correct. The sales staff should be listed in
order of sales from most to least with the four people having the same number of sales in reverse alphabetical order.
Sorting6h class
//********************************************************************
// Sorting6h.java [Lab 6h]
// Student Name: <name>
// Author: Lewis/Loftus
//
// Demonstrates the selection sort and insertion sort algorithms.
//********************************************************************
list[position] = key;
}
}
// -----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
// -----------------------------------------------------------------
public static void selectionSort( Comparable[] list )
{
int min;
Comparable temp;
RunNumbers6h class
// ******************************************************
// RunNumbers6h.java [Lab 6h]
// Student Name: <name>
//
// Demonstrates selectionSort on an array of integers.
// ******************************************************
import java.util.Scanner;
Sorting.selectionSort( intList );
Salesperson6h class
// *******************************************************
// Salesperson6h.java [Lab 6h]
// Student Name: <name>
//
// Represents a sales person who has a first name, last
// name, and total number of sales.
// *******************************************************
// ------------------------------------------------------
// Constructor: Sets up the sales person object with
// the given data.
// ------------------------------------------------------
public Salesperson6h( String first, String last, int sales )
{
firstName = first;
lastName = last;
totalSales = sales;
}
// --------------------------------------------------
// Order is based on total sales with the name
// (last, then first) breaking a tie.
// --------------------------------------------------
@Override
public int compareTo( Object other )
{
int result = 0;
if (this.getSales() == (________________________________)
{if (this.getLastName().equals(((Salesperson6h)other).getLastName()))
result = ((Salesperson6h)other).getFirstName().compareTo(this.getFirstName());
else
result = ((Salesperson6h)other).______________.compareTo(this._______________);}
return result;
}
// -------------------------------------------
// Returns true if the sales people have
// the same name.
// -------------------------------------------
@Override
public boolean equals( Object other )
{
return lastName.equals( ( (Salesperson6h) other ).getLastName() ) &&
firstName.equals( ( (Salesperson6h) other ).getFirstName() );
}
// -------------------------
// First name accessor.
// -------------------------
public String getFirstName()
{
return firstName;
}
// -------------------------
// Last name accessor.
// -------------------------
public String getLastName()
{
return lastName;
}
// -------------------------
// Total sales accessor.
// -------------------------
public int getSales()
{
return totalSales;
}
// -------------------------------------------
// Returns the sales person as a string.
// -------------------------------------------
@Override
Page 3 of 4 Searching & Sorting
Lab 6h: Sorting Objects
RunWeeklySales6h class
// *************************************************************
// RunWeeklySales6h.java [Lab 6h]
// Student Name: <name>
//
// Sorts the sales staff in descending order by sales.
// ************************************************************
Sorting6h.insertionSort( salesStaff );
RunWeeklySales6h Output
Ranking of Sales for the Week