# Returning an array

• January 21st, 2013, 11:05 AM
maple1100
Returning an array
So this class take the value of endMiles and startMiles from the main class. Then it would calculate the distance by subtracing the startMiles from endMiles then returning the value of distanceV(This is the variable I use). As you can see I have a method to return the value of distanceV, this work perfectly fine but I'm calculating more than one distance so I want to put all the distanceV into an array. I have two method to put the distanceV to an array but when I return it and try to print it in the main method, it give me some random characters. Thanks

Code :

```public class AnnualFuelUse { //Variables private int endVMiles , startVMiles, distanceV;   private int [] distanceArray = new int[4];   //Constructor AnnualFuelUse(int endMiles,int startMiles) { endVMiles = endMiles; startVMiles= startMiles; distanceV = 0;   }   //Calculation public void calcDistance() { distanceV = endVMiles - startVMiles; }   //Return startDistance public int getStart() { return startVMiles; } //Return endDistance public int getEnd() { return endVMiles; } //Return distance public int getDistance() { return distanceV; }   //Calc distanceArray public void calcArray() { for(int index = 0 ; index < distanceArray.length; index++) { distanceArray[index] = distanceV; } }   //Return distanceArray public int[] getArray() { return distanceArray; } }```
• January 21st, 2013, 11:20 AM
Norm
Re: Returning an array
Quote:

it give me some random characters
Please post the output that you are talking about and a full program that compiles, executes and generates the output.
• January 21st, 2013, 11:31 AM
maple1100
Re: Returning an array
This is the main method class

Code :

```public class AnnualFuelUseTester { public static void main(String[]args) { //Array of object AnnualFuelUse fuelUse []= { new AnnualFuelUse(17283 , 16923 ), new AnnualFuelUse(17483 , 17283 ), new AnnualFuelUse(17733 , 17483 ), new AnnualFuelUse(18033 , 17733 ) };     //Call Methods for(int index = 0 ; index < fuelUse.length; index++) { fuelUse[index].calcDistance();   }   //Print max/min System.out.println("Here is an example:"+fuelUse[1].getArray()); } }```

This print out " This is an example :[I@36c27576"

Is what I did on the other class the way to return an array ? Thanks
• January 21st, 2013, 11:42 AM
Norm
Re: Returning an array
Quote:

This print out " This is an example :[I@36c27576"
That is what is printed out when you print an int array. It is returned by the toString() method

To see the contents of an array, use the Arrays class's toString() method:
Code :

`System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));`
• January 21st, 2013, 12:02 PM
maple1100
Re: Returning an array
So instead of making the array in the outer class, I decided to make the array in the main method class.(I haven't learn the toString yet so can't used it). Here is what I have. Is it possible to put any of this: - in the outer class ( The class in my orignal post) instead of the main? Thanks
Code :

``` int[] distanceA = new int[4]; for(int index = 0 ; index< fuelUse.length; index++) { distanceA[index] = fuelUse[index].getDistance(); } int min = Integer.MAX_VALUE; for(int i = 0 ; i < distanceA.length;i++) { if(distanceA[i] < min) min = distanceA[i];```

Code :

```public class AnnualFuelUseTester { public static void main(String[]args) { //Array of object AnnualFuelUse fuelUse []= { new AnnualFuelUse(17283 , 16923 ), new AnnualFuelUse(17483 , 17283 ), new AnnualFuelUse(17733 , 17483 ), new AnnualFuelUse(18033 , 17733 ) };     //Call Methods for(int index = 0 ; index < fuelUse.length; index++) { fuelUse[index].calcDistance();   }   int[] distanceA = new int[4]; for(int index = 0 ; index< fuelUse.length; index++) { distanceA[index] = fuelUse[index].getDistance(); } int min = Integer.MAX_VALUE; for(int i = 0 ; i < distanceA.length;i++) { if(distanceA[i] < min) min = distanceA[i]; }   } }```
• January 21st, 2013, 12:54 PM
Norm
Re: Returning an array
What does the program output now when it is executed?

Quote:

Is it possible to put any of this: - in the outer class
Are you asking about a method? Who will call the method? What data would the method need to do its computations? Will it need to save any data between calls it receives?
• January 21st, 2013, 12:57 PM
maple1100
Re: Returning an array
With the toString method or my modify code?
• January 21st, 2013, 01:03 PM
Norm
Re: Returning an array
The Arrays class's toString() method is used for debug output and rarely used to generate output for a report. You print out the contents of an array so you can see what the program has put into it.

Post the output from your program that shows what the code is printing out and add some comments to the post describing what you want the output to look like.
• January 21st, 2013, 01:11 PM
maple1100
Re: Returning an array
With this code :

Code :

```public class AnnualFuelUseTester { public static void main(String[]args) { //Array of object AnnualFuelUse fuelUse []= { new AnnualFuelUse(17283 , 16923 ), new AnnualFuelUse(17483 , 17283 ), new AnnualFuelUse(17733 , 17483 ), new AnnualFuelUse(18033 , 17733 ) };     //Call Methods for(int index = 0 ; index < fuelUse.length; index++) { fuelUse[index].calcDistance();   }   int[] distanceA = new int[4]; for(int index = 0 ; index< fuelUse.length; index++) { distanceA[index] = fuelUse[index].getDistance(); } int min = Integer.MAX_VALUE; for(int i = 0 ; i < distanceA.length;i++) { if(distanceA[i] < min) min = distanceA[i]; }   } }```
It print out perfectly what I wanted, but I wanted this part of the code: - To be in the AnnualFuelUse class (My orignal post) instead of the main class. It is appropriate to have this part of the code in the main class instead of the AnnualFuelUse? I thought the main class suppose to be only used for calling on the method on the outside class ( I don't know what it called when one class is associated with another so I'll just call it outside class) Thanks
Code :

``` int[] distanceA = new int[4]; for(int index = 0 ; index< fuelUse.length; index++) { distanceA[index] = fuelUse[index].getDistance(); } int min = Integer.MAX_VALUE; for(int i = 0 ; i < distanceA.length;i++) { if(distanceA[i] < min) min = distanceA[i]; }```
• January 21st, 2013, 01:19 PM
Norm
Re: Returning an array
Have you tried making it a method?

I don't know what rules you've been given about what goes in a "main" class and can't give you any advice.
I put the main() method in the class that has the code to be executed. I don't create a separate, special class that has only a main() method.
• January 21st, 2013, 01:26 PM
maple1100
Re: Returning an array
I prefer coding the way you describe it too.- Yes I try making it a method but I don't know how to get it minimum value (min) back to the main method class so I can display it.

Code :

```public class AnnualFuelUse { //Variables private int endVMiles , startVMiles, distanceV; private int [] distanceArray = new int[4]; private int min = Integer.MAX_VALUE; //Constructor AnnualFuelUse(int endMiles,int startMiles) { endVMiles = endMiles; startVMiles= startMiles; distanceV = 0;   }   //Calculation public void calcDistance() { distanceV = endVMiles - startVMiles; }   //Return startDistance public int getStart() { return startVMiles; } //Return endDistance public int getEnd() { return endVMiles; } //Return distance public int getDistance() { return distanceV; }   //Calc distanceArray public void calcArray() { for(int index = 0 ; index < distanceArray.length; index++) { distanceArray[index] = distanceV; } }   public void calcDDD() { for(int i = 0 ; i < 4;i++) { if(distanceArray[i] < min) min = distanceArray[i]; } }   public int getMin() { return min; } }```
• January 21st, 2013, 01:36 PM
Norm
Re: Returning an array
Quote:

how to get it minimum value (min)
Call the getMin() method.
• January 21st, 2013, 01:45 PM
maple1100
Re: Returning an array
I was thinking of putting this in the main method class : - But since the methods are not static, I can't call the method this way. May I get a hint of how to call it? Thanks
Code :

`System.out.println("Min Value:"+AnnualFuelUse.getMin());`
• January 21st, 2013, 01:56 PM
Norm
Re: Returning an array
To call a non-static method in a class you need to get a reference to an instance of the class and use that to call the method:
Code :

` refToClass.methodInClass(); // call methodInClass using refToClass`

Where in the main() method is there a reference to an instance of the AnnualFuelUse class?
Use that reference to call that instance's getMin() method.
• January 21st, 2013, 01:59 PM
maple1100
Re: Returning an array
Would this be a consider the reference to the instance? Thanks

Code :

``` AnnualFuelUse fuelUse []= { new AnnualFuelUse(17283 , 16923 ), new AnnualFuelUse(17483 , 17283 ), new AnnualFuelUse(17733 , 17483), new AnnualFuelUse(18033 , 17733 ) };```
• January 21st, 2013, 02:03 PM
Norm
Re: Returning an array
That looks like an array full of references to 4 different instances.
• January 21st, 2013, 02:08 PM
maple1100
Re: Returning an array
Hmm yes. This is why I was having the problem of turning the finding the minimum value into a methods. I guess I'll keep it in the main method to make it easier. Thanks for your help!
• January 21st, 2013, 02:29 PM
Norm
Re: Returning an array
Each instance has its own min value.
• January 21st, 2013, 02:31 PM
maple1100
Re: Returning an array
I didn't want to spam the forum with a new thread, so hopefully it find for me to request help in this same one since it related.
I'm getting loss of precision for the following : - mpgA is a double but "i" is an int. So I get the loss of precision here "minMPG = mpgA[i]" How can I fix this? Thanks
Code :

``` double[] mpgA = new double[4]; for(int index = 0 ; index< fuelUse.length; index++) { mpgA[index] = fuelUse[index].getMPG(); }   int minMPG = Integer.MAX_VALUE; for(int i = 0 ; i < mpgA.length;i++) { if(mpgA[i] < minMPG) minMPG = mpgA[i]; }```
• January 21st, 2013, 02:32 PM
Norm
Re: Returning an array
If you're happy with 1.99 becoming 1, then you can cast the double to an int by putting (int) before it.
• January 21st, 2013, 02:35 PM
maple1100
Re: Returning an array
So I cast the array of double to array of int?
• January 21st, 2013, 02:38 PM
Norm
Re: Returning an array
Not the array, the double value.