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 5 of 5

Thread: [SOLVED][Beginner] Arithmetic Question, Why does product become negative after a certain point?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question [SOLVED][Beginner] Arithmetic Question, Why does product become negative after a certain point?

    Here is the problem:
    Write an application that inputs three integers from the user and displays the sum, average, product, smallest, and largest of the numbers. Note: Calculation of average in this exercise should result in an integer representation of average without repeating decimals.

    Here is my code:
    //Exercise 2.17 from Deitel 
    // Write an application that inputs three integers and displays sum, average, product, smallest, and largest of numbers 
    //Average calculations should not have a repeating decimal
     
    import java.util.Scanner; //Java utilities package import statement
     
    public class Average 
    {
    	//main method for application
    	public static void main (String [] args)
    	{
    		Scanner input = new Scanner(System.in); //Scanner input from user
     
    		int number1, number2, number3, sum, average, product, smallest, largest; 
     
    		System.out.print( "Enter the first integer: " ); //prompt for number1 
    		number1 = input.nextInt(); 
     
    		System.out.print( "Enter the second integer: " ); //prompt for number2
    		number2 = input.nextInt();
     
    		System.out.print( "Enter the third integer: " ); //prompt for number3
    		number3 = input.nextInt();
     
    		sum = number1 + number2 + number3; 
     
    		average = (number1 + number2 + number3) / 3;
     
    		product = number1 * number2 * number3; 
     
    		System.out.printf( "Sum = %d \nAverage = %d \nProduct = %d \n", sum, average, product );
     
    		if (number1 < number2 && number1 < number3) //if number1 is smallest
    			System.out.println( number1 + " is smallest"); 
     
    		if (number2 < number1 && number2 < number3) //if number 2 is smallest
    			System.out.println( number2 + " is smallest");
     
    		if (number3 < number1 && number3 < number2) //if number3 is smallest
    			System.out.println( number3 + " is smallest");
     
    		if (number1 > number2 && number1 > number3) //if number1 is largest
    			System.out.println( number1 + " is largest"); 
     
    		if (number2 > number1 && number2 > number3) //if number 2 is largest
    			System.out.println( number2 + " is largest");
     
    		if (number3 > number1 && number3 > number2) //if number3 is largest
    			System.out.println( number3 + " is largest");
     
    	} //end main method
    } //end Average class

    When I run the program with smaller numbers like 60, 30, 50 then the program runs fine. Once
    I start entering 349284, 409823409, 123980128 as my integers then I get a negative product. I wish I could provide the cut off point for when I start seeing the negative product printed but I don't actually know. I tested 100,000 for all three integers and the product was negative again so I'm assuming anything greater than 100,000.

    This maybe silly question but please let me know if I'm doing something wrong. Thanks!!

    Also! I'm new please let me know if this question should be posted in a different section or on a different forum in general.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

    Time to Google:

    2's compliment
    Overflow/underflow
    Improving the world one idiot at a time!

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

    Quote Originally Posted by beibixx View Post
    I wish I could provide the cut off point for when I start seeing the negative
    An int can only hold a value so big
    System.out.println(Integer.MAX_VALUE);
    System.out.println(Integer.MAX_VALUE + 1);
    Quote Originally Posted by beibixx View Post
    This maybe silly question but please let me know if I'm doing something wrong. Thanks!!
    Nothing wrong with undirected discovery. Hit up your favorite search engine and see what you can find about it

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

    int's are signed 32 bit integers (more specifically, two-compliment signed 32-bit integers). That means the largest value they can hold without overflowing is 2^31-1, or 2147483647.

    Because of the way integer math works on computers results get truncated if the operation overflows. However, the sign bit coincidentally is what would be the most significant bit so you end up with some odd-ball negative numbers.

    There are ways to estimate/check pre-emptively if a given arithmetic operation will overflow or not, but for this assignment I probably wouldn't be worried about it unless your teacher explicitly says you need to handle this.

    You could try switching to use longs. These are 64-bit signed integers, and therefore can hold numbers up and including 2^63-1, or 9223372036854775807, or alternatively use the BigInteger class which has arbitrary precision.

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [Beginner] Arithmetic Question, Why does product become negative after a certain point?

    Thanks guys! I will also Google

Similar Threads

  1. Affecting Point objects with methods - a beginner's failed attempt
    By thatolkevin in forum Object Oriented Programming
    Replies: 10
    Last Post: July 16th, 2013, 08:50 AM
  2. Replies: 9
    Last Post: March 18th, 2013, 05:49 PM
  3. Beginner question about main
    By Samaras in forum Java Theory & Questions
    Replies: 2
    Last Post: July 31st, 2012, 10:10 AM
  4. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  5. beginner's question
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2010, 04:02 PM

Tags for this Thread