Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My smallest and largest integers will not change.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

        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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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:

    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)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    toiletsauce (February 1st, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by toiletsauce; February 1st, 2011 at 07:41 PM. Reason: mispelling

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: My smallest and largest integers will not change.

    Quote Originally Posted by toiletsauce View Post
    Thank you very much, the thread you linked for me was exactly what I needed to know.
    No problem. Glad you got it sorted.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Determine the two smallest integers from a set of user input integers
    By bpontin in forum Loops & Control Statements
    Replies: 4
    Last Post: October 17th, 2010, 06:38 PM
  2. finding the largest object help
    By nickypass in forum Object Oriented Programming
    Replies: 4
    Last Post: October 16th, 2010, 05:48 PM
  3. java integers
    By timeline in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2010, 02:54 AM
  4. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  5. Replies: 5
    Last Post: May 21st, 2009, 02:45 AM