Program-4
New Class Design, Sorting and File Outputs
Objectives:
1. Designing a new class with multiple methods including a constructor.
2. Sorting one-dimensional array of random integers by the SelectSorting and QuickSorting.
3. Generating random integers using an object of the Class Random.
4. Creating an output file using two output stream classes
FileOutputStream and
PrintWriter.
Helpful Resources:
1.Textbook Display12.8, pages 701-702 for QuickSort Design.
2.Other textbook sections 3.2-3.3, 3.5, 4.1-4.3, 6.1-6.2, 10.2(Text Files).
3.java Platform API website for Classes FileOutputStream, PrintWriter, Math and Random.
4.Unit-1 CSD-3, CSD-4 and Unit-1 EPD-4 and EPD-5.
Submission Requirements:
From this program-4 on, you will submit, unless otherwise indicated,
a. source program file (as before) and
b. output file instead of the screenshot file now, both by the due date given in our Course Syllabus
and also in the Unit-1 Folder of the Blackboard Content page.
Tasks:
The following program does the following tasks:
a.define a brand new class named OneDimArray.
b.declare some instance variables including:
MAX:final static int MAX=100;
original: int[MAX];
sorted: int [MAX];
sum:int;
max:int;
min:int;
c.completely defines one one-parameter constructor and some other methods such as:
public void bubblesort ()
public void display() // Displays sorted array
public void calSum()
public getSum()
Your program is to complete this program by adding some methods and other necessary components as follows:
1. Complete the following six methods:
selectSort()
quickSort()
calMax()
calMin()
getMax()
getMin()
2. Create two objects of this Class, OneDimArray, such that
a. its instance variable original (one dimensional array of 100 integes) will contain
random integers between 1 and 1000 and
b. that of the second will contain random integers between 1 and 10000, respectively.
3. Create an output file that will contain, with a suitable heading each,
a. results of the first object by your selectionSort method()
b. results of the second object by your quickSort method.
c. the sum, max and min of the second object.
************************Program-to-complete******************
Example of a new class OneDimArray.
Instance Variables:
MAX:final static int MAX=100;
original: int[MAX];
sorted: int [MAX];
sum:int;
max:int;
min:int;
public methods:
bubblesort();// I already defined!!!
quicksort()
selectsort();
calSum();// I already defined!!!
calMax();
calMin();
getSum();// I already defined!!!
getMax();
getMin();
display();// I already defined this as it displays sorted result,
// that is, the contents of array sorted, one of those instance variables.
//********************************************************************
*/
import java.util.Random;
public class OneDimArray
{final static int MAX=100;
private int[] original = new int[MAX];
publicint[] sorted = new int[MAX];
private int sum, max, min;
// Constructor will set the original array by generating random
// integers determined by the given parameter
public OneDimArray (int upper)
{ Random generator = new Random();
for (int index=0; index <= MAX-1; index++)original[index] = generator.nextInt(upper)+1;// generated integers are between 1 and upper. } ////////////////////////////////////////////////////////////////////Class methods() follow now!!!////////////////////////////////////////////////////////////////// public void bubblesort () {// first copy original to sorted so as to sort sorted thereby leaving original intactfor (int index=0; index <= MAX-1; index++)sorted [index] = original [index];// now sort the copied sorted array.for (int last=MAX-1; last>=1; last)
for (int first=0; first<=last-1; first++)if (sorted[first]>sorted[first+1]) // Swap them.
{int temp =sorted[first];
sorted[first] = sorted[first+1];
sorted[first+1] = temp;
}
}//end of bubblesort()
/////////////////////////////////////////////////////////////////////////////////////////
public void display() // Displays sorted array
{for(int index=0; index <= MAX-1; index++){ System.out.printf (“%8d”, sorted[index]); if ((index+1)%8 == 0)System.out.println(); }}public void calSum()//calculates the sum of all integers in the array and sets instance variable sum to this calculated total. {int total = 0;for (int index=0; index <= MAX-1; index++)total += original[index];sum=total;}///////////////////////////////////////////////////////////////////////////////////////////////// public int getSum() { return sum; }/*You are supposed to complete the rest of this class and the whole program*/ public static void main (String[] arg) {OneDimArray xint = new OneDimArray(1000);OneDimArray yint = new OneDimArray(10000);xint.bubblesort(); xint.calSum();int total = xint.getSum();xint.display();System.out.println (”
TOTAL OF ALL ARRAY ELEMENTS::: ” + total);}// end of main() }// end of class OneDimArray
Reviews
There are no reviews yet.