import java.io.*;
import java.util.Arrays;
public class arrayObjects
{
private int dimension;
private double lengthA;
private int points;
private double [][] dataPoints;
//empty constructor
public arrayObjects(){
}
//constructor
public arrayObjects(double[][] datapoints)
{
lengthA = 0;
points = 0;
dimension = 0;
this.dataPoints = new double [points][dimension +1];
}
//constructor
public arrayObjects(double myLength, int myPoints, int myDimension, double[][] dataPoints){
lengthA = myLength;
points = myPoints;
dimension = myDimension;
this.dataPoints = new double [points][dimension + 1];
}
//array getter
public double[][] getArray(){
//findDistance(dataPoints, points, dimension);
return dataPoints;
}
//array setter
public void setArray(double [][] myDataPoints, int dimension, int points){
dataPoints[points][dimension] = myDataPoints[points][dimension];
}
//dimension getter
public int getDimension(){
return dimension;
}
//dimension setter
public void setDimension(int myDimension){
dimension = myDimension;
}
//points getter
public int getNumPoints(){
return points;
}
//points setter
public void setNumPoints(int myNumPoints){
points = myNumPoints;
}
//range getter
public double getRange(){
return lengthA;
}
//range setter
public void setRange(double myRange){
lengthA = myRange;
}
//fill arrays with values
public void fillArray(double [][] mydataPoints, double mylengthA, int mydimension, int mypoints)throws IOException
{
for(int x = 0; x < mypoints; x++){//runs x times to print x data points
for (int y = 0; y < mydimension; y++){//runs y times to print a coordinate
mydataPoints [x][y]= (dimension *Math.random() - 1) * mylengthA;// finds a random number in the range and assigns it to the coordinate array
}//end for
}//end for
}//end fillArray
//reads data from file
public void readData(String myString)throws IOException
{
BufferedReader readfile = new BufferedReader(new FileReader(myString));//reads data from arrayPoints.txt
try {//try statement
StringBuilder file = new StringBuilder();
String line = readfile.readLine();//assigns line data from file
while (line != null) {//run while line isn't empty
file.append(line);
file.append('\n');
line = readfile.readLine();
}//end while
String finalData = file.toString();//converts to a string
System.out.print(finalData);//prints the coordinate
} //end try
finally {//close file when done
readfile.close();
}//end finally
}//end readFile
//write data to a file
public void writeData(double[][] mydataPoints, int mypoints, String myString)throws IOException
{
PrintWriter fileOut = new PrintWriter (new FileWriter (myString));//writes to myString file
for (int i = 0; i < mypoints; i++){
fileOut.println(Arrays.toString(mydataPoints[i]));//converts array to string and prints to file
}//end for
fileOut.close();//writes to file
}//end writeData
//calculates distance between coordinates
public void findDistance(double[][] mydataPoints, int mypoints, int mydimension){
for(int i = 0; i < mypoints; i++){
for (int j = 0; j < mydimension; j++){
mydataPoints[i][mydimension] = mydataPoints[i][j] * mydataPoints[i][j];//calculaes distance using distane formula
}
mydataPoints[i][mydimension] = Math.sqrt(mydataPoints[i][mydimension]);
}//end for
}//end findDistance
//quick sort for array
public double[][] sortArray(double[][] newArray, int mydimension, int down, int top) {
System.arraycopy(newArray, 0, newArray, 0, newArray.length);
int i = down;//sets int variables
int j = top;
double [] pivot = newArray[(down + top) / 2];//finds centre pivot point
//goes through each value and sorts depending if its greater or less than the pivot point
do {
while (newArray[i][mydimension] < pivot[mydimension]) {
i++;
}
while (newArray[j][mydimension] > pivot[mydimension]) {
j--;
}
if (i <= j) {
double[] temp = new double[newArray[i].length];
for (int y = 0; y < newArray[i].length; y++) {
temp[y] = newArray[i][y];
newArray[i][y] = newArray[j][y];
newArray[j][y] = temp[y];
}
i++;
j--;
}
} while (i <= j);
if (down < j) {
newArray = sortArray(newArray, mydimension, down, j);
}
if (i < top) {
newArray = sortArray(newArray, mydimension, i, top);
}
//returns array
return newArray;
}//end sortArray
}