Your task is to write a class called Data, stored in a file named Data.h. Your c
ID: 3682836 • Letter: Y
Question
Your task is to write a class called Data, stored in a file named Data.h. Your class should be able to store a collection (vector) of integers. In addition to the appropriate constructors, your class should also have the following methods. void add (int number); Adds a number to the data set. void print (); Prints out the entire data set on a single line, separated by space. void sort (); Sorts the data set in ascending order. You may implement any sorting algorithm here, for example, max sort, bubble sort, insertion sort, merge sort, quick sort, etc... There is no need to try to implement the most efficient one, any one will do.
Explanation / Answer
package com.chegg.java;
import java.util.Vector;
public class Data
{
static Vector<Integer> dataVector = new Vector<Integer>(5,2);
public Data() {
}
public static void add (int number) {
Vector<Integer> vector = new Vector<Integer>(5,2);
vector.add(number);
dataVector.addAll(vector);
}
public static void print (Vector<Integer> dataSet) {
System.out.print("Data : ");
for(Integer number: dataSet) {
System.out.print(number+" ");
}
System.out.println("");
}
public static void sort(Vector<Integer> vector) {
Object[] array = vector.toArray();
int[] array2 = new int[10];
for(int l=0;l<array.length;l++) {
array2[l] = (Integer) array[l];
}
for(int i=0;i<array.length;i++) {
for(int j=1;j<(array.length-i) ;j++) {
if(array2[j-1] > array2[j] ) {
int temp = array2[j-1];
array2[j-1] = array2[j];
array2[j] = temp;
}
}
}
System.out.print("After sorting : ");
for(int k=0;k<array.length;k++) {
System.out.print(array2[k] + " ");
}
}
public static void main(String args[]) {
Data data = new Data();
data.add(75);
data.add(26);
data.add(47);
data.add(18);
data.add(89);
data.print(dataVector);
sort(dataVector);
}
}
Expected output :
Data : 75 26 47 18 89
After sorting : 18 26 47 75 89
Here i am wirtting .java file.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.