Results 1 to 4 of 4
  1. #1
    juliab is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Object and Client Class help

    I have an object and client class created which prints coordinates in order of their distance from the origin. The client class asks the user how many dimensions they want the array to have (x,y or x,y,z) how many points they want generated, and a range from which each coordinate will run from (ex. -x to x, -y to y etc.). When I run the program it prints out the correct number of points, but there is always one extra element in the array (etc. when user enters dimension of array as '4', it always prints out one extra element -> [4, 5, 9, 6, 1]). Why is this happening?

    Client Class
    Java Code:
    import java.io.*;   
    
    public class MA {  
      
      public MA() { }   
      
      public static void main(String args[]) throws IOException { 
        
        String myString = "arrayNumPoints.txt";
        int numpoints = 0;
        int dimension = 0;
        double lengthscale = 0;
        double [][] points = new double[numpoints][dimension + 1]; 
        
        BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
        
        MB pointsB = new MB(); 
        
        System.out.println("Enter number of points:"); 
        String numpointsA = myInput.readLine(); 
        numpoints = Integer.parseInt(numpointsA); 
        pointsB.setnumpoints(numpoints); 
        
        System.out.println("Enter the dimension:"); 
        String dimensionA = myInput.readLine(); 
        dimension = Integer.parseInt(dimensionA); 
        pointsB.setdim(dimension); 
        
        System.out.println("Enter length(range):"); 
        String lengthscaleA = myInput.readLine(); 
        lengthscale = Double.parseDouble(lengthscaleA); 
        pointsB.setlengthscale(lengthscale); 
        
        
        pointsB = new MB(numpoints, lengthscale, dimension, points);
        
        pointsB.fillarray(pointsB.getarray(), pointsB.getlengthscale(), pointsB.getdim(), pointsB.getnumpoints());
        pointsB.caldistance(pointsB.getarray(), pointsB.getnumpoints(), pointsB.getdim());
        pointsB.sort(pointsB.getarray(), 0, pointsB.getnumpoints()-1, pointsB.getdim());
        pointsB.writefile(pointsB.getarray(), pointsB.getnumpoints(), myString);
        pointsB.readfile(myString);
      }
    }
    Object Class
    Java Code:
    import java.io.*; 
    import java.util.Arrays; 
    
    public class MB { 
      
      //variables and arrays are declared
      private double lengthscale; 
      private int numpoints; 
      private int dimension; 
      private double [][] points; 
      
      //constructor
      public MB() { }   
      
      //constructor
      public MB(double [][] points) { 
        numpoints = 0; 
        lengthscale = 0; 
        dimension = 0; 
        this.points = new double[numpoints][dimension + 1]; 
      }   
      
      //constructor
      public MB(int mynumpoints, double mylengthscale, int mydimension, double [][] mypoints) { 
        numpoints = mynumpoints; 
        lengthscale = mylengthscale; 
        dimension = mydimension; 
        this.points = new double[numpoints][dimension + 1]; 
      }   
      
      //numpoints getter
      public int getnumpoints() 
      { 
        return numpoints; 
      }   
      
      //numpoints setter
      public void setnumpoints(int mynumpoints) { 
        numpoints = mynumpoints; 
      }   
      
      //lengthscale getter
      public double getlengthscale() { 
        return lengthscale; 
      }   
      
      //lengthscale setter
      public void setlengthscale(double mylengthscale) { 
        lengthscale = mylengthscale; 
      }   
      
      //dimension getter
      public int getdim() { 
        return dimension; 
      }  
      
      //dimension setter
      public void setdim(int mydimension) { 
        dimension = mydimension; 
      }   
      
      //array getter
      public double[][] getarray() { 
        return points; 
      }   
      
      //array setter
      public void setarray(double [][]mypoints, int numpoints, int dimension) { 
        points[numpoints][dimension] = mypoints[numpoints][dimension];
      }   
    
      //fill array method
      public void fillarray(double [][]mypoints, double mylengthscale, int mydimension, int mynumpoints) throws IOException { 
        
        for(int x = 0; x < mynumpoints; x++) 
        { 
          for(int y = 0; y < mydimension; y++) {
            //fills array with random points within the specified range
            mypoints[x][y] = (dimension * Math.random() - 1) * mylengthscale;   
          }//end for   
        }//end for
      }
    
      //writefile method
      public void writefile(double [][]mypoints, int mynumpoints, String myString) throws IOException { 
        
        //writes to myString
        PrintWriter fileOut = new PrintWriter(new FileWriter(myString));
        
        //for loop runs for length of mylengthscale
        for(int m = 0; m < mynumpoints; m++) {
          //prints points to file
          fileOut.println(Arrays.toString(mypoints[m]));   
        }
          //close file
          fileOut.close(); 
        //end for
      }
      
      //readfile metod
      public void readfile(String myString) throws IOException 
      { 
        String filePath = myString; 
        //reads data from mString
        BufferedReader in = new BufferedReader(new FileReader(new File(myString))); 
        String line = null; 
        
        while(( (line = in.readLine()) != null)) 
       
        System.out.println(line);   
        in.close(); 
      }  
    
      
      //caldistance method
      public void caldistance(double [][]mypoints, int mynumpoints, int mydimension) { 
        
        //for loop; calculates distance for specified number of points
        for(int i = 0; i < mynumpoints; i++) { 
          for(int k = 0; k < mydimension; k++) { 
            mypoints[i][mydimension] = mypoints[i][k] * mypoints[i][k];
          }//end for loop
          mypoints[i][mydimension] = Math.sqrt(mypoints[i][mydimension]); 
        }//end for loop                              
      }
      
      //sort method
      public double[][] sort(double[][] mynewpoints, int down, int top, int mydimension) {
        
        System.arraycopy(mynewpoints, 0, mynewpoints, 0, mynewpoints.length);
        
        //variables are declared
        int d = down;
        int u = top;
        
        //determines pivot point
    
        double [] pivot = mynewpoints[(down + top)/2];
     
        
        //sorts the values of the array, comparing it to the pivot
       do {
          while (mynewpoints[d][mydimension] < pivot[mydimension]) {
            d++;
          }
          
          while (mynewpoints[u][mydimension] > pivot[mydimension]) {
            u--;
          }
          
          if (d <= u) {
            double[] temporary = new double[mynewpoints[d].length];
            
            //compres values in array and switches them     
            for (int y = 0; y < mynewpoints[d].length; y++) {
              temporary[y] = mynewpoints[d][y];
              mynewpoints[d][y] = mynewpoints[u][y];
              mynewpoints[u][y] = temporary[y];
            }
            d++;
            u--;
          }
        } while (d <= u);
        
        if (down < u) {
          mynewpoints = sort(mynewpoints, mydimension, down, u);
        }
        
        if (d < top) {
          mynewpoints = sort(mynewpoints, mydimension, d, top);
        }
        
        return mynewpoints;
        
      }
    }
    Last edited by juliab; 10-31-2013 at 01:57 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Object and Client Class help

    it always prints out one extra element
    What is this for: dimension + 1?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    juliab is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: Object and Client Class help

    dimention + 1 is so that i can add the 'distance' found, in the array points[numpoints[dimention +1]

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Object and Client Class help

    Why the +1? why not just dimension?

    Where is the print statement that prints out the extra element?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 05:37 AM
  2. Replies: 1
    Last Post: 02-02-2011, 07:11 PM
  3. Replies: 2
    Last Post: 06-22-2010, 05:29 AM
  4. Replies: 3
    Last Post: 03-15-2010, 09:44 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •