Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use what the user inputed for an array size from the main method?  RSS feed

 
Shawn Defranco
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a program that measures the time it takes for each sorting algorithm to sort random integers in an array. The problem is in my main class I ask the user to input what the size of the array should be. However, I'm trying to pass this number "arraySize" into another class.

For now, I set the size of the array as 50.


So, the program should work by taking in array with a size of input.next() and then populates the array with random integers. It then sorts the array and measures the time.

This is my main:

This is in the same class as my main:

Why can't I replace SIZE with arraySize? Is there a way where I can declare SIZE as my input.nextInt() in my main method? If I do that, it still populates the array with 50 random integers.

 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The vast majority of times, the way you pass things between two classes is by using a method parameter. In your example I would use the initValues() method to handle a value indicating the size of the array.
 
Shawn Defranco
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The vast majority of times, the way you pass things between two classes is by using a method parameter. In your example I would use the initValues() method to handle a value indicating the size of the array.


could I use something like arraySize.nextInt() in that method? The problem is I can't declare a new array in my main and pass that into my method like selectionSort;
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn Defranco wrote:could I use something like arraySize.nextInt() in that method? The problem is I can't declare a new array in my main and pass that into my method like selectionSort;


I suppose you could, but why not keep it simple? Now that you know the desired size, just create an array of that size.
 
Shawn Defranco
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could, but why not keep it simple? Now that you know the desired size, just create an array of that size.


Well, that is my issue. I do not know the size of the array as the user will determine that. My main method asks the user the size and the user replies with the size. How do I carry this info outside the main method? If I initialize a new array in my main and use input.nextInput() in my main it still uses the 50 integer size array that was declared outside the main. I also can't pass the new array declared in my main in my selectionSort() method as it doesn't accept it as one of the parameters.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem I'd see as a wrong decision in designing your classes.

Why class Sorts has the array in the first place?

I'd expect to have those static sorting methods which accepts array as an argument. Beware of possible side effect, need to think if you want to have an effect after sorting on original array, or you want to return new array but this time just sorted.

So, in this matter, in your other class, where the main method is, you'd ask user for an array size and would create an array; Initialize with some values; Later in order to sort it you'd pass it on to Sorting class method and return it either new sorted array, or without returning just sort it (that involves you knowing, that you're passing reference copy, and while you're not re-assigning reference to a new object, that would refer to the same original object, so sorting effect would be reflected in original array).

Your Sorting class also could have a method to check the algorithm running time. But that is for later.
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shawn Defranco wrote:I do not know the size of the array as the user will determine that. My main method asks the user the size and the user replies with the size. How do I carry this info outside the main method?



Paul Clapham wrote:In your example I would use the initValues() method to handle a value indicating the size of the array.


Like this:




 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!