CS3401 Assignment 2
Homework Assignment #2Exercise #1
Write an implementation of a
generic version of the Sorting.java
class and its methods. Modify the SortingDriver.java
program so that it also test comparisons of Integers and
Dates, along with comparisons of Strings. Your
Sorting methods should have the following headers:
public static <E extends Comparable<E>> void selectionSort(E[] list) public static <E extends Comparable<E>> void insertionSort(E[] list) Exercise #2
Implement a program called
MaxRandomNumber.java that creates a 5x5 two dimensional
array of Integers, and then fills it with random numbers
from 0 to 1000, and then sends this array to the generic max()
method which returns the maximum value in the array. Have your
main() method print out the numbers in the 5x5 array on 5
separate lines with the 5 Integers from each row on their
own line. And then print the maximum number returned by your max()
method below the 5 lines that show the array contents. Your max()
method should have the following header:
public static <E extends Comparable<E>> E max(E[][] list)
Be sure your programs compile and
run without error.
Review the Submission
Requirements document and make sure your submission meets
those requirements. Please also review the Grading
Guidelines for an indication of general guidelines used in
evaluating your assignments.
Deliverables
Turn in the files:
Do this by uploading the file as
an attachment to Homework #02 in the Homework Assignments section
of the class website.
/*Name:
Dalibor
Labudovic
*
Course: CS3401
*
Professor: Adam
Shaw
*
Assignment: Assignment 2 Part 1
*
School: Kennesaw
State University
*/
package
assignment2;
//********************************************************************
//
Demonstrates the selection sort and insertion sort algorithms
with strings.
//********************************************************************
public
class
Sorting
{
//-----------------------------------------------------------------
//
Sorts the specified array of objects using the selection
//
sort algorithm.
//-----------------------------------------------------------------
public
static
<E extends
Comparable<E>>void
selectionSort (E[] list)
{
int
max;
E temp;
for
(int
index = 0; index < list.length-1;
index++)
{
max = index;
for
(int
scan = index+1; scan < list.length;
scan++)
if
(list[scan].compareTo(list[max]) < 0)
max = scan;
//
Swap the values
temp = list[max];
list[max] = list[index];
list[index] = temp;
}
}
//-----------------------------------------------------------------
//
Sorts the specified array of objects using the insertion
//
sort algorithm.
//-----------------------------------------------------------------
public
static
<E extends
Comparable<E>>void
insertionSort(E[] list)
{
for
(int
index = 1; index < list.length;
index++)
{
E 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;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*Name:
Dalibor
Labudovic
*
Course: CS3401
*
Professor: Adam
Shaw
*
Assignment: Assignment 2 Part 1
*
School: Kennesaw
State University
*/
package
assignment2;
import
java.util.Date;
public
class
SortingDriver
{
public
static
void
main (String[] args)
{
Integer[] nums = { 1, 2, 5, 6};
Sorting.insertionSort(nums);
for(int
j = 0; j < nums.length;
j++)
System.out.println(nums[j]);
Date[] date = { new
Date(), new
Date()};
Sorting.insertionSort(date);
for(int
f = 0; f < date.length;
f++)
System.out.println(date[f]);
String[] friends = new
String[4];
friends[0] = "Fred
Williams";
friends[1] = "Sarah
Barnes";
friends[2] = "Mark
Riley";
friends[3] = "Laura
Getz";
Sorting.insertionSort(friends);
for
(int
i = 0; i < friends.length;
++i)
System.out.println
(friends[i]);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*Name:
Dalibor
Labudovic
*
Course: CS3401
*
Professor: Adam
Shaw
*
Assignment: Assignment 2 Part 2
*
School: Kennesaw
State University
*/
package
assignment2;
public
class
MaxRandomNumber {
public
static
void
main (String[] args){
//create
an double array with random numbers from 0 - 1000
Integer[][]
randomNumbers = new
Integer[5][5];
for(int
i = 0; i < randomNumbers.length;
i++)
{
for(int
j = 0; j <randomNumbers[i].length;
j++)
{
randomNumbers[i][j]
= (int)(Math.random()*1000);
}
}
//display
double array
System.out.println("Here
is random number 5x5 array: ");
for(int
i = 0; i < randomNumbers.length;
i++)
{
for(int
j = 0; j < randomNumbers[i].length;
j++)
{
System.out.print(randomNumbers[i][j]
+ "
");
}
System.out.println();
}
//call
method max and pass double array and print results
System.out.println("Maximum
value in the array is "
+ MaxRandomNumber.max(randomNumbers));
}
//will
return the maximum value from double array
public
static
<E extends
Comparable<E>>E max(E[][] list)
{
E
max = list[0][0];
//find
max
for(E[]
row : list)
{
for(E
elt : row)
{
if(elt.compareTo(max)
> 0)
{
max
= elt;
}
}
}
return
max;
}
}
|
Comments
Post a Comment