0% found this document useful (0 votes)
7 views

Lab 6h Sorting Objects-2

Uploaded by

mertdene10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 6h Sorting Objects-2

Uploaded by

mertdene10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 6h: Sorting Objects

Lab 6h: Sorting Objects


The file Sorting6h.java contains a class that implements both the selection sort and the insertion sort algorithms for sorting any
array of Comparable objects in ascending order. In this exercise, you will use the Sorting6h class to sort several different types of
objects.

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.

2. Complete Salesperson6h.compareTo() - Complete the compareTo() method in the Salesperson6h class by


filling in the blanks. The comparison should be based on total sales; that is, return a negative number if the executing
object has total sales less than the other object and return a positive number if the sales are greater. Use the name of the
sales person to break a tie (alphabetical order ). See Listing 6.16 on page 326 in the text.

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.
//********************************************************************

public class Sorting6h


{
// -----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
// -----------------------------------------------------------------
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;
}
}

// -----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
// -----------------------------------------------------------------
public static void selectionSort( Comparable[] list )
{
int min;
Comparable temp;

for ( int index = 0; index < list.length - 1; index++ )


{
min = index;

Page 1 of 4 Searching & Sorting


Lab 6h: Sorting Objects

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;
}
}
}

RunNumbers6h class
// ******************************************************
// RunNumbers6h.java [Lab 6h]
// Student Name: <name>
//
// Demonstrates selectionSort on an array of integers.
// ******************************************************

import java.util.Scanner;

public class RunNumbers6h


{
// --------------------------------------------
// Reads in an array of integers, sorts them,
// then prints them in sorted order.
// --------------------------------------------
public static void main( String[] args )
{
Integer[] intList;
int size;

Scanner scan = new Scanner( System.in );

System.out.print( "\nHow many integers do you want to sort? " );


size = scan.nextInt();
intList = new Integer[size];

System.out.println( "\nEnter the numbers..." );


for ( int i = 0; i < size; i++ )
{
intList[i] = scan.nextInt();
}

Sorting.selectionSort( intList );

System.out.println( "\nYour numbers in sorted order..." );


for ( int i = 0; i < size; i++ )
{
System.out.print( intList[i] + " " );
}
System.out.println();
}

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.
// *******************************************************

public class Salesperson6h implements Comparable


{
private String firstName, lastName;
Page 2 of 4 Searching & Sorting
Lab 6h: Sorting Objects

private int totalSales;

// ------------------------------------------------------
// 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() > ((Salesperson6h)other)._______________)


{___________;}
else if (_____________________ < ___________________________)
{return 1;}

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

public String toString()


{
return lastName + ", " + firstName + ": \t" + totalSales;
}
}

RunWeeklySales6h class
// *************************************************************
// RunWeeklySales6h.java [Lab 6h]
// Student Name: <name>
//
// Sorts the sales staff in descending order by sales.
// ************************************************************

public class RunWeeklySales6h


{
public static void main( String[] args )
{
Salesperson6h[] salesStaff = new Salesperson6h[10];

salesStaff[0] = new Salesperson6h( "Jane", "Jones", 3000 );


salesStaff[1] = new Salesperson6h( "Daffy", "Duck", 4935 );
salesStaff[2] = new Salesperson6h( "James", "Jones", 3000 );
salesStaff[3] = new Salesperson6h( "Dick", "Walter", 2800 );
salesStaff[4] = new Salesperson6h( "Don", "Trump", 1570 );
salesStaff[5] = new Salesperson6h( "Jane", "Black", 3000 );
salesStaff[6] = new Salesperson6h( "Harry", "Taylor", 7300 );
salesStaff[7] = new Salesperson6h( "Andy", "Adams", 5000 );
salesStaff[8] = new Salesperson6h( "Jim", "Doe", 2850 );
salesStaff[9] = new Salesperson6h( "Walt", "Smith", 3000 );

Sorting6h.insertionSort( salesStaff );

System.out.println( "\nRanking of Sales for the Week\n" );

for ( Salesperson6h s : salesStaff )


{
System.out.println( s );
}
}
}

RunNumbers6h (after insertionSort() ) Output


How many integers do you want to sort? 7

Enter the numbers...


5 6 7 10 8 2 9

Your numbers in sorted order...


2 5 6 7 8 9 10

RunWeeklySales6h Output
Ranking of Sales for the Week

Taylor, Harry: 7300


Adams, Andy: 5000
Duck, Daffy: 4935
Smith, Walt: 3000
Jones, Jane: 3000
Jones, James: 3000
Black, Jane: 3000
Doe, Jim: 2850
Walter, Dick: 2800
Trump, Don: 1570

Page 4 of 4 Searching & Sorting

You might also like