public String toString()
{
String str;
str = "The test scores in descending order are \n";
for(int i = 0; i < data.length; i++)
{
str = str + data[i] + " ";
}
str = str + "\nThe average is " + mean;
return str;
}
This code for java returns scores in descending order for my code. But what throws me off is the way the 'str' is being returned. How I understand this is the return value of 'str' is (str + "\nThe average is " + mean). Because this is my last updated value of 'str', it will update the 'str' to "The test scores in descending order are" First, Second the loop, then last "str + "\nThe average is " + mean". So in the end even though we updated str a few times the actually printed 'str' will only be "str + "\nThe average is " + mean". Please explain why the program actually ends up printing
The test scores in descending order are 70 80.......... (element values)
(And then returns the average mean value info)
Because the command was
str = str + "\nThe average is " + mean;
Which means, append to everything already in str and store the results in str. If it was
str = "\nThe average is " + mean;
it would instead replace everything already in str.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With