Programming Assignment - part c
Due Wednesday April 11, 2018
Note: parts a) and b) appear below
What
to do: sort an array of class Student. You already have an algorithm to
sort (program 6b) amd have defined a class Student (program 5).
Here is your driver program:
public static void main(String [] args)
{
Student [ ] stuArray=new Student[20]; // array large enough for 20 students
int nextStudent=0; //where to insert next student
stuArray[nextStudent]=new Student(12543,"BILL","SMITH",3.56, new Date(9,1,2014));
nextStudent++;
stuArray[nextStudent]=new Student(35643,"SUSAN","JONES",3.76, new Date(9,1,2013));
nextStudent++;
stuArray[nextStudent]=new Student(87564,"HARRY","DAVIS",3.26, new Date(9,1,2015));
nextStudent++;
stuArray[nextStudent]=new Student(34265,"MARY","ADAMS",3.86, new Date(9,1,2016));
nextStudent++;
stuArray[nextStudent]=new Student(88993,"DAVE","FREDERICKS",3.16, new Date(1,1,2013));
nextStudent++;
stuArray[nextStudent]=new Student(66557,"KAREN","MORRIS",3.02, new Date(1,1,2009));
nextStudent++;
stuArray[nextStudent]=new Student(33445,"ED","PETERS",3.56, new Date(9,1,2017));
nextStudent++;
for( k=0; k< YOU_NEED_TO FIGURE_OUT_THIS_VALUE; k++)
{
lowestPosition=findLowestPosition( stuArray, k , nextStudent);
swap the element at position k with the element at position lowestPosition
}
System.out.println("Sorted array follows :");
for (k=0; k< nextStudent; k++)
System.out.println(stuArray[k]);
}
----------------------------------------------------------------------------------------------------------------------------------------
Programming Assignment 6 - part b
Due Monday April 2, 2018
NOTE: part a) in its entirety appears below
i) Selection sort! We are going to sort an array, you have already done half the work! Recall from part a), you have written:
lowestPosition=findLowestPosition( theArray , 0 , placeForNextNumber);
This will find the
address(index) of the smallest element in theArray[0],
theArray[1], theArray[2],.........theArray[placeForNextNumber-1].
If lowestPosition does not
equal 0, then swapping the element at position 0 with the element at
position at lowestPosition will place the
smallest
element at the beginning of the array (Question: suppose lowestPosition
does equal zero ?)
ii) After completing i), let's take this a step further:
lowestPosition=findLowestPosition( theArray , 1 , placeForNextNumber)
will find the address (index) of the smallest
element in theArray[1],
theArray[2],............theArray[placeForNextNumber-1]. If
lowestPosition
does not equal 1, then swapping the element at
position 1 with the element at position lowestPosition will place the
second smallest
element at theArray[1] (with the smallest at theArray[0]).
iii) After completing ii), tet's get even fancier. If k is declared as an integer, then
k=2;
lowestPosition=findLowestPosition( theArray , k, placeForNextNumber)
will find the address(index) of the smallest element
in theArray[k], theArray[k+1],......,theArray[placeForNextNumber-1].
Again, we swap
(I leave it to you to see the pattern)
What if we put this into a loop:
for( k=0; k< YOU_NEED_TO FIGURE_OUT_THIS_VALUE; k++)
{
lowestPosition=findLowestPosition( theArray, k , placeForNextNumber);
swap the element at position k with the element at position lowestPosition
}
This will sort the entire array. So your main program should look like this:
import java.util.*;
import java.io.*;
public class ArrayStuff
{
public static void main(String [] args)
{
Scanner
kbd=new Scanner(System.in);
int []
theArray=new int[100];
int
nextNumber=0;
int
placeForNextNumber=0;
int
lowestPosition;
String
input;
do
{
System.out.println("More numbers to input ? ");
input=kbd.nextLine();
if (input.equals("Y"))
{
System.out.println("Enter number ");
nextNumber=kbd.nextInt();
kbd.nextLine();
theArray[placeForNextNumber]=nextNumber;
placeForNextNumber++;
}
}while (input.equals("Y"));
//Sanity check. Make sure numbers made it to the array
//for(int i=0;i<placeForNextNumber;i++)
// System.out.println(theArray[i]);
//
Assignment #5 Part a
// You
need to write the method findLowestPosition to make this work
//
//
Consider the following:
// if the
array contained 3,2,6 then theArray[0] would be 3, theArray[1] would be
2
// and theArray[2] would be 6.
placeForNextNumber would be 3 and findLowestPosition
// would return 1 since the smallest
element in the array (2) resided at position 1
//
// if the
array contained -3,2,-6,17 then theArray[0] would be -3, theArray[1]
would be 2,
// theArray[2] would be -6 and
theArray[3] would be 17. placeForNextNumber would be 4
// and findLowestPosition
// would return 2 since the smallest
element in the array (-6) resided at position 2
//
//lowestPosition=findLowestPosition( theArray , 0 , placeForNextNumber);
//System.out.println("The lowest position occurred at "+lowestPosition);
for( k=0; k< YOU_NEED_TO FIGURE_OUT_THIS_VALUE; k++)
{
lowestPosition=findLowestPosition( theArray, k , placeForNextNumber);
swap the element at position k with the element at position lowestPosition
}
System.out.println("Sorted array follows :");
for (k=0; k< placeForNextNumber; k++)
System.out.println(theArray[k]);
}
}
--------------------------------------------------------------------------------------------------------
Programming Assignment 6 - part a
Due Monday March 26, 2018
Copy the following into a class called ArrayStuff. You will need to
implement one
method. See below
import java.util.*;
import java.io.*;
public class ArrayStuff
{
public static void main(String [] args)
{
Scanner
kbd=new Scanner(System.in);
int []
theArray=new int[100];
int
nextNumber=0;
int
placeForNextNumber=0;
int
lowestPosition;
String
input;
do
{
System.out.println("More numbers to input ? ");
input=kbd.nextLine();
if (input.equals("Y"))
{
System.out.println("Enter number ");
nextNumber=kbd.nextInt();
kbd.nextLine();
theArray[placeForNextNumber]=nextNumber;
placeForNextNumber++;
}
}while (input.equals("Y"));
//Sanity check. Make sure numbers made it to the array
//for(int i=0;i<placeForNextNumber;i++)
// System.out.println(theArray[i]);
//
Assignment #5 Part a
// You
need to write the method findLowestPosition to make this work
//
//
Consider the following:
// if the
array contained 3,2,6 then theArray[0] would be 3, theArray[1] would be
2
// and theArray[2] would be 6.
placeForNextNumber would be 3 and findLowestPosition
// would return 1 since the smallest
element in the array (2) resided at position 1
//
// if the
array contained -3,2,-6,17 then theArray[0] would be -3, theArray[1]
would be 2,
// theArray[2] would be -6 and
theArray[3] would be 17. placeForNextNumber would be 4
// and findLowestPosition
// would return 2 since the smallest
element in the array (-6) resided at position 2
//
lowestPosition=findLowestPosition( theArray , 0 , placeForNextNumber);
System.out.println("The lowest position occurred at "+lowestPosition);
}
}