Java I have to create two classes (DataSet & DataSample) for wine collection cla
ID: 3701637 • Letter: J
Question
Java
I have to create two classes (DataSet & DataSample) for wine collection classification. DataSet takes a filename as an input (wine.txt) and stores all the variables into array DataSample[] dataArray (as said on screenshot). I am loading file correctly and get correct return of the lines from the file but I do not understand how to store all the data from the file into DataSample[] dataArray and pass them later to that class or use it with split() function. I have written all classes and methods and put them on github (link below) where you can see the code i wrote so far. not allowed to use lists or collections.
Full requirements are in the screenshots (do not need them fully solved, just explain how to retrieve and store data from text file).
https://github.com/denistepp1/denistepp1.github.io/tree/master/javacw LINK TO FILES
.Inside this class, you should have the following methods: public Datasample(int lb_doublel1atr_(Constructor for this class You should set 1abel, numofAttributes and atrributes for this sample nside this method public void setLabel (int 1b); (Set the label of this sample). public int getLabel); (Get the label of this sample). public int getnumofAttributes); (Get the number of attributes of this sample) public double[] getattributes; (Get the attributes of this sample). public double distance (Datasample dat); (Get the distance of this sample to another sample dat. We calculate the Euclidean distance between two samples. E.g. x =[x1 , X2, , Xd] and y =[Yvy,, yd] are two samples. The distance between them is_d(x, y)- (x-y,)' + (x2-y2)2 + + (x,-Y2)2 .) Hiut: You mav call Math.pow(x.0.5) to getExplanation / Answer
function:
public DataSet(String filename)throws FileNotFoundException{
System.out.println("Calculating number of lines...");
getDataSetSize(filename);
DataSample[] dataArray = new DataSample[samples];
double[] data=new double[samples]; //to convert string array into doubles array
for(int i=0;i<samples;i++){
String[] token=sampleArr[i].split(","); //this is to split string by ','
label=Integer.parseInt(token[0]); //split label is token[0]
for(int j=1;j<token.length;j++)
data[j-1]=Double.parseDouble(token[j]); //token[1] to token[13] are attributes, this will convert string into //double
dataArray[i]=new DataSample(label,data);
}
}
public int getDataSetSize(String filename) throws FileNotFoundException {
samples=0;
File file = new File(filename);
try (Scanner input = new Scanner(file)) {
while (input.hasNextLine()){
sampleArr[samples]=input.nextLine(); //sampleArr , has each line read from file
samples++;
}
input.close();
System.out.println("done ");
}
return samples;
}
//i have not given the full code, as you want , how to read file and split., i have given that statement alone in the function.
if you need any more help, please write me back
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.