My smallest and largest integers will not change.
Hello, I've very recently the forums and am in need of some help. I've been given a simple program to write, to get some hands on experience with "getters" and "setters". This program will take the values entered by the user and output the the number in the list, the sum, the average, and the smallest and largest numbers in the list. The last part is where I'm having problems. No matter what I do I can't see to find a way to correctly get the largest and smallest value from the user input.
Code Java:
private int getLargest()
{
return largest;
}
private void setLargest( int largest )
{
largest = largest;
}
private int getSmallest()
{
return smallest;
}
private void setSmallest( int smallest )
{
smallest = smallest;
}
private void clear()
{
for( int cycle = 0; cycle > count; cycle++ )
{
list[ cycle ] = 0;
}
sum = 0;
average = 0;
largest = 0;
smallest = 0;
count = 0;
}
private void processElement( int element )
{
list[ getCount() ] = element;
count = getCount() + 1;
setCount( count );
sum = getSum() + element;
setSum( sum );
average = ((double)getSum() / (double)( getCount() + 1));
setAverage( average );
if ( element >= getLargest() )
{
setLargest( element );
}
else if ( element <= getSmallest() )
{
setSmallest( element );
}
}
Sorry if I break any forum rules in posting this question. Any help would be much appreciated, also hows my use of getters and setters?
Re: My smallest and largest integers will not change.
Without actually seeing all of your code in SSCCE (that's a link) form, I'm only guessing, but what do you think this line does:
Code :
private void setLargest( int largest )
{
largest = largest;
}
Do you have a class variable named largest? How does Java know which largest you're talking about? The answer is in this article: Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: My smallest and largest integers will not change.
Thank you very much, the thread you linked for me was exactly what I needed to know.
Re: My smallest and largest integers will not change.
Quote:
Originally Posted by
toiletsauce
Thank you very much, the thread you linked for me was exactly what I needed to know.
No problem. Glad you got it sorted.