• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it possible to have an int method, and get double value from it?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, is it possible to have int method and get double value from it?
I have seven judges scores on each dive, random array of scores
To determine the average score, the lowest and highest score are eliminated.

I need to have the output as follows:

Judge 1 - 5.4
Judge 2 - 5.5
Judge 3 - 3.5
Judge 4 - 1.5
Judge 5 - 8.9
Judge 6 - 9.3
Judge 7 - 4.3

Average = 5.52

My average, highest and lowest score method are defined as follows:

private int highest ()
{

}

private int lowest ()
{

}

public double average ()
{

}

Is it possible? My average score end up doesn't tally when I manually calculate it.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi, is it possible to have int method and get double value from it?  


No and Yes.
A method can only return one type of value: int or double

The problem can be changed to return an index into a list of values so the type of the values is not used.
Pass a list of values to the search method and have it return the index into the list of the desired value.
Then the calling code can use that index to access the found value from the list.
 
Junshi Gusza
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

Hi, is it possible to have int method and get double value from it?  


No and Yes.
A method can only return one type of value: int or double

The problem can be changed to return an index into a list of values so the type of the values is not used.
Pass a list of values to the search method and have it return the index into the list of the desired value.
Then the calling code can use that index to access the found value from the list.



Hi, thanks for your reply.
But I don't understand you.
Fyi, I need to use the above 3 methods. I can't change the method name or modify it.
The scores for 7 judges are from the array using random
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can the values be placed in a list or an array?
Can the methods be given access to that list or array?
If so the search methods can return an index into the list/array of the found value.

For example it the array contained:  {11.2, 2.3, 33.7}
The index for the smallest value would be 1
the index for the largest value would be 2
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain why you have methods returning ints when the values are not ints. Why do you need to return ints at all?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:. . . the search methods can return an index into the list/array of the found value. . . .

Was there an average() method? That can't return an int index.
 
Junshi Gusza
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Please explain why you have methods returning ints when the values are not ints. Why do you need to return ints at all?



This is the question I am given.
How will you do the program?

I use math.random to generate 7 random scores for array [i]
 
Junshi Gusza
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can the values be placed in a list or an array?
Can the methods be given access to that list or array?
If so the search methods can return an index into the list/array of the found value.

For example it the array contained:  {11.2, 2.3, 33.7}
The index for the smallest value would be 1
the index for the largest value would be 2




I use a method math.random to generate 7 random scores for the judges for my array [i]

all methods can access to the array
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check carefuly that you have understood the assignment correctly. Can you make the scores into ints? For the average to be an int, you would have to round it.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Was there an average() method? That can't return an int index.


Note: I said "search" methods.

all methods can access to the array


Ok, then the search methods can return an index into the array.  An index is an int value
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But OP does seem to require an average; that can only be expressed as an int by rounding it.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed where the average must be an int.
I saw:
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please check carefuly that you have understood the assignment correctly. Can you make the scores into ints? For the average to be an int, you would have to round it.


I agree with Campbell; a little strange this exercise.

Wouldn't it be easy to work internally with jury scores that are multiplied by 10 (as integer)? So, using 54 in stead of 5.4?
Just when you show the results, don't forget to divide by 10 again.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I disagree.  I see this as a reasonable exercise.
The tricky bit is removing the high and low scores.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't think it is strange to have an array of doubles, and that the highest and lowest values must be returned as ints?
I agree that the removing of the highest and lowest makes this exercise worth while (but that also depends on what OP has been taught so far).
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:I missed where the average must be an int.
I saw: . . .

No, I got that wrong sorry.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

highest and lowest values must be returned as ints?


What about returning the index of the values?   Have we seen the details of the assignment?  The OP posted method signatures.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, it is possible, but it seems very unlikely to me. What if the jury scores have two or three decimals? That would not make for a handy system. But indeed, let's wait for OP to come with some more details.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand: if you have a double array having the values 0.0, 0.1. ..., 9.8, 9,9, then the index i would be equivalent to having the corresponding value multiplied by 10. But in that case, why use the array?
 
Junshi Gusza
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

Campbell Ritchie wrote:Was there an average() method? That can't return an int index.


Note: I said "search" methods.

all methods can access to the array


Ok, then the search methods can return an index into the array.  An index is an int value



how do you return an index?

return array[i];   ?

sorry, i'm new to java
 
Junshi Gusza
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the above, I am stuck at this.
I have 5 divers, 7 judges. how do I get the winner after i compare the average computed?

the output as follows:
                Average
Diver 1         1.22
Diver 2         4.33
Diver 3         6.33
Diver 4         3.33
Diver 5         1.99

winner: Diver 3

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by finding the index of the highest score as you had before. Once you have found that, use exactly the same technique for finding the winner from the averages.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


how do you return an index?

return array[i];   //  return value in array at index i


For the code you show referencing an element in array, the index is i.

So to return the index:
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how do I get the winner after i compare the average computed?  


Often the winner is the one with the highest score.  Given a list of scores, search the list for the highest value.
 
Junshi Gusza
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all for the reply. i solved it. I want to delete this thread. is it possible to delete this thread?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junshi Gusza wrote:thanks . . .

That's a pleasure

i solved it.

Well done: please show us the finalproduect

. . . . is it possible to delete this thread?

No. We only delete threads which have something wrong with them.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic