0

in my class SummerStats I need to create another method that locates the largest element of the array created in my method setSalaries. How can I call the array, "salaries" to another method in the same class?

class SummerStats
 {
   public SummerStats()
   {

   }
   public int[][] setSalaries(int people, int years)
   { 
      int[][] salaries = new int[people][years];

   //rows respresent people and columns represent years
      for (people = 0; people < salaries.length; people++)
      {
         for (years = 0; years < salaries[people].length; years++)
         {
            salaries[people][years] = (int)(1000 + Math.random()*1000);
         }
      }

      return salaries;
   }

Also, my test class is

import java.util.*;
public class testSummerStats
{
   public static void main (String[] args)
   {
   Scanner input = new Scanner(System.in);
   SummerStats one = new SummerStats();
   System.out.println("Enter people, then years: ");
   int x = input.nextInt();
   int y = input.nextInt();
   one.setSalaries(x, y);
   }
 }
1
  • Look at using instance variables in your SummerStats class. Commented Nov 15, 2015 at 17:11

4 Answers 4

1

setSalaries should not return the array. Assign the array to a field of SummerStats instead. Then add another method to SummerStats for locating the largest element.

Sign up to request clarification or add additional context in comments.

Comments

0

Declare your 2D-Array as a member variable. This way it will be accessible to all the methods of the class:

class SummerStats
 {
   private int[][] salaries;

   public SummerStats()
   {

   }

   public void setSalaries(int people, int years)
   { 
      salaries = new int[people][years]; // initialize the array

   //rows respresent people and columns represent years
      for (people = 0; people < salaries.length; people++)
      {
         for (years = 0; years < salaries[people].length; years++)
         {
            salaries[people][years] = (int)(1000 + Math.random()*1000);
         }
      }

   }
    public void locateMax() {
        // Your code goes here. (You can access the salaries array)
    }
}

Then create a new method (locateMax i.e) that will calculate the max number of the 2D-array.

Comments

0

you can assign your array to a field. and access it outside. or second option you can pass pass this array to new method and there you can do operations for finding out largest element.

Comments

0

Your current program:

class SummerStats
 {
   public SummerStats()
   {

   }
   public int[][] setSalaries(int people, int years)
   **{** 
      int[][] salaries = new int[people][years];
      ....
      return salaries;
   **}**
}

Solution:

class SummerStats
 **{**
   private int[][] salaries;

   public SummerStats()
   {

   }
   public int[][] setSalaries(int people, int years)
   { 
      salaries = new int[people][years];
      ....
      return salaries;
   }
**}**

In your current program is the variable salaries declared as a local variable of the method setSalaries and therefore its scope is limited to the scope of the method. In the solution program is the variable salaries declared as a member variable/field of the class SummerStats and therefore its scope is limited to the scope of all member method/variable declarations of the class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.