• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extracting maximum and minimum values from an array  RSS feed

 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm currently working on a school assignement, but i'm stuck at writing the methods that give me the max and min values of the array from the first method. I'm getting numbers that are close to the actual numbers, but they're missing by small margines. I'm having a hard time noticing the mistake i've made, so any help would be much appriciated!

 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You are passing in "array" but nowhere are you using it. Instead of having arrayMax just use array. Ditto for min.
 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I can just have this? But i'm still not hitting the values from the cooldownSamples method, and that's the point that's confusing me the most. Is it because the methods are running another "iteration" of the array as the numbers vary for each time i run the program? Say the array is created once in the first method, then again for the seconds and another time for the third?

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On lines 7-9 in your main method, you are creating new arrays each time. The array you print on line 7 is a different array from the array that you pass to the max method on line 8. Likewise, the array you pass to the min method on line 9 is a different array from the ones used on the previous lines.

The solution is to have only one call to the coolDownSamples() method and assign the result to an array variable. Then use that array variable consistently when you call the print, max, and min methods.
 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help! Is this a reasonable solution?

 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Eirik Lauritsen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers1
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a better name though. Prefer a name like sampleTemperatures over arrayFinal. The former tells the reader that the array represents a set of sample temperatures. The arrayFinal name doesn't really do anything for the reader except to redundantly remind them that they're looking at an array and that somehow it's "final," whatever that means.

Edit: looking over your code again, maybe sampleCoolDownTimes is really what you should name that array. Or just sampleTimes. I hope you get the point.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Who needs to mess about with loops when you can use an IntStream? I think the easiest way to get an IntStream out of that array is with Arrays#stream(int[]) (note: heavily overloaded method). All streams of primitives can produce a summary statistics object and you can get various details out of that:-Be sure to read about the summary object and the IntStream#summaryStatistics() method.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice one, Campbell, and I learned something new, too.

However, since OP is using double[], java.util.DoubleSummaryStatistics might be more appropriate.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also have a next method:
Look how much ceremony it contains. You're dealing with such a low level details beforehand as creating an array, specifying its size in advance, then iterating over an empty array, also you need to take care of indices in order to get them right. Then populating array and finally returning. That is a very imperative way, as you are telling your program how exactly to do things, and that HOW is quite easy to get wrong when the program's logic becomes more complex.

On the other hand, you have a tool to tell program WHAT to do, so you leave underlying mechanics to accomplish for the tool and for yourself leave to worry about the business logic.

Look how simplified version might look using streams:
Explanation might not even needed what this nice method is doing, as it is pretty expressive in its own language. Apparently it does the same, just in much more concise and less error prone way.

Consider learning streams in parallel with old habitual style of writing code, as that might be beneficial for you.
Staff note (Liutauras Vilda):

I have avoided using temperature parameter as it isn't used in original OP's version.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Nice one, Campbell, and I learned something new,
Thank you
However, since OP is using double[], java.util.DoubleSummaryStatistics might be more appropriate.
But all you have to do is change three letters I‑n‑t to six letters D‑o‑u‑b‑l‑e  in the name of the class (and the same in the import) and it will work
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minuste ago, I wrote:. . . all you have to do is . . .
That is the nice thing about good object programming (or in this case good functional programming): it is possible to make small changes to accommodate new requirements.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
A few minuste ago, I wrote:. . . all you have to do is . . .
That is the nice thing about good object programming (or in this case good functional programming): it is possible to make small changes to accommodate new requirements.

In this case, it wasn't object-oriented programming per se that made that possible, it was consistency in choosing names for related classes, i.e. a sensible and intuitive API design.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean good any sort of programming?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!