Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Project Programming Help- The file Numbers.java reads in an array of intege

ID: 3796084 • Letter: J

Question

Java Project Programming Help-

The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to your directory.

Numbers.java won’t compile in its current form, because int type is primitive but not object in java. Change the array type from int to Integer (autoboxing). The numbers should be sorted now.

Write a program Strings.java, similar to Numbers.java, that reads in an array of String objects and sorts them. You may just copy and edit Numbers.java.

Now the selectionSort method can sort both Integer and String types. This polymorphism is implemented through interface.

Modify the selectionSort algorithm so that it sorts in descending order rather than ascending order. Run Numbers.java and Strings.java again to check the descending output.

Explanation / Answer

Here are the three complied classes, Please check and run the program.

1. Sorting.java


public class Sorting {

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

2. Numbers.java

import java.util.Scanner;


public class Numbers {

   //---------------------------------------------
// Reads in an array of integers, sorts them,
// then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
   Sorting sorting = new Sorting();
Comparable[] intList;
int size;
Scanner scan = new Scanner(System.in);
System.out.print (" How many integers do you want to sort? ");
size = scan.nextInt();
intList = new Comparable[size];
System.out.println (" Enter the numbers...");
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt();
sorting.selectionSort(intList) ;
System.out.println (" Your numbers in sorted order...");
for (int i = 0; i < size; i++)
System.out.print(intList[i] + " ");
System.out.println ();
}
}

3. Strings.java

import java.util.Scanner;


public class Strings {

   //---------------------------------------------
// Reads in an array of strings, sorts them,
// then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
   Sorting sorting = new Sorting();
Comparable[] stringList;
int size;
Scanner scan = new Scanner(System.in);
System.out.print (" How many strings do you want to sort? ");
size = scan.nextInt();
stringList = new Comparable[size];
System.out.println (" Enter the strings...");
for (int i = 0; i < size; i++)
   stringList[i] = scan.next();
sorting.selectionSort(stringList) ;
System.out.println (" Your numbers in sorted order...");
for (int i = 0; i < size; i++)
System.out.print(stringList[i] + " ");
System.out.println ();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote