سؤال

Im having trouble when it comes to dividing my program into methods (specifically the main method and another method which does all the calculations etc.). Im not sure on the proper way to divide up my existing code to create a new method. My program also writes to a file as well.

When i compile the code i get an error saying

File: F:\COMPSCI 12\java1.java [line: 37] Error: F:\COMPSCI 12\java1.java:37: missing return statement

But i already have a return statement.

Did i use the methods properly? Or what is wrong? Thanks

ORIGINAL CODE WITH NO METHODS

    import java.io.*;

public class java1
{
  public static void main (String [] args) throws IOException
  {
    //int variables are declared
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));

    //arays are declared/initialized
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];

    for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
      lengthscale[a] = length;//stores array
    }//end for loop

    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within it

        fileOut.println( locations[x][y] + ", ");//prints out coordinate

      }//end nested for loop
    }//end for loop

    fileOut.close ();
  }//end main method
}//end cass

SAME CODE BUT USING METHODS

 import java.io.*;

public class J4_2_MultiDimensionalArray7
{
  public static void main (String [] args) throws IOException
  {
    int numpoints = 100, dimension = 2, length = 100;//numpoints is set to 100, dimension is set to 2, length is set to 100

    //arrays are initializewd and declared
    double [] lengthscale = new double [dimension];
    double [][] locations = new double [numpoints][dimension];

    PrintWriter fileOut = new PrintWriter (new FileWriter ("arrayNumPoints.txt"));


    for(int m=0; m <length; m++){//for loop
      fileOut.println(java.util.Arrays.toString(locations[m]) + ", ");
    }
  }//end main

    public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {


    for (int a = 0; a < dimension; a++){//for loop runs while a is less than dimension
      lengthscale[a] = length;//stores array
    }//end for loop

    for (int x=0; x < numpoints; x++){//for loop runs while x is less than numpoints
      for (int y=0; y < dimension; y++){//nested for loop runs while y is less than dimension
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];//creates the range and choses random point within it

      return locations[x][y];//returns the value of locations
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }//end writefile methos
}//end cass
هل كانت مفيدة؟

المحلول

Suppose that numpoints == 0. Would your code ever reach the return statement?

In the other case, if your function does return, will fileOut.close(); ever be called?

Java recognizes that there is a case where the return statement might not be reached, and acts as if you didn't have one. To fix this you should have a "default" return statement at the end of the function, to handle the edge case where your loops aren't entered into.

Im not sure on the proper way to divide up my existing code to create a new method.

It's really up to you and what the code is doing, but a few guidelines:

  • Method getting too long to understand? Break it up into several methods.
  • Are you writing "duplicate code"? Maybe that should go in a method.
  • Stuff like writing to a file is a discrete unit of operation. In other words, separate from the logic of the rest of your program. So it should be separated as its own method.
  • Etc.

نصائح أخرى

The method is wrong. You declared the return value as a Double, however you're trying to return an array of Doubles. Plus the return statement would be called during the first iteration of the cycles, so it would stop there.

public static Double writefile(Double locations[][], Double lengthscale[], int dimension, int numpoints, Double length)throws IOException
    {   

    for (int x=0; x < numpoints; x++){
      for (int y=0; y < dimension; y++){
        locations [x][y]= (2 * Math.random() - 1) * lengthscale[y];

        return locations[x][y];  <------------ this would be called in the first iteration;
      }//end nested for loop

    }//end for loop

    fileOut.close ();//close file
  }

The other guys pointed out several things.

I think the most important general principle here is separation of concerns. In your particular case, calculating something in one place, and persisting data into a file are two distinct, clearly-cut concerns.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top