9 Replies - 7305 Views - Last Post: 22 September 2011 - 11:03 AM Rate Topic: -----

#1 nathan_williamson94  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-March 11

Sum and Average Program

Posted 22 September 2011 - 09:16 AM

I have to make a simple program for my class and the program must give the sum and average of four numbers. The average should be "556.75" but is showing up as "556.0". I will be very grateful for any help.



public class SumAndAverage
{
    // Variables for the four numbers used for the sum and average
    int firstNumber  = 475;
    int secondNumber = 821;
    int thirdNumber  = 369;
    int fourthNumber = 562;
    
    //Variables for the sum and average
    int    sumOfNumbers;
    double averageOfNumbers;
    
    public SumAndAverage()
    {
        // Sum calculations
        sumOfNumbers = (firstNumber + secondNumber + thirdNumber
          + fourthNumber);
          
        // Average calculations
        averageOfNumbers = (sumOfNumbers / 4); 
        /**  Used the variable "sumOfNumbers" in calculation above so there
             would be less room fo errors int the calculations for the average.
         */
        
        // Print out of sum and average
        System.out.println("The sum of the for numbers is " + sumOfNumbers);
        System.out.println();
        System.out.println("The average of the four numbers is "
           + averageOfNumbers);
        
    }
}



Is This A Good Question/Topic? 1
  • +

Replies To: Sum and Average Program

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Sum and Average Program

Posted 22 September 2011 - 09:18 AM

averageOfNumbers = (sumOfNumbers / 4);
You are dividing an int by 4, this will give you only a solid number. You need to cast it to a double when you assign it to the double.
averageOfNumbers = <cast goes here>(sumOfNumbers / 4);
Was This Post Helpful? 1
  • +
  • -

#3 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Sum and Average Program

Posted 22 September 2011 - 09:20 AM

I believe if you change
averageOfNumbers = (sumOfNumbers / 4);
to
averageOfNumbers = (sumOfNumbers / 4.0);
, it will work for ya. Gotta divide by two doubles to keep the decimals.

Edit - well you are dividing an int by 4, but it'll still work as 4.0. int / double will yield double.

This post has been edited by creative code monkey: 22 September 2011 - 09:32 AM

Was This Post Helpful? 1
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Sum and Average Program

Posted 22 September 2011 - 09:59 AM

The only problem that I have with changing 4 to 4.0 which would work; is that the fact remains that you only have 4 tests, you can't have part of a test. You only use double when you can have part of something. By adding on that .0 you are insinuating that you can have a part of it. If you come across a picky teacher they will say that that is incorrect and you cannot do that. Which is why they should use a cast to double to fix it. Also it is good practice to using a cast, they come in handy. You can't always add a .0 to solve things.
Was This Post Helpful? 1
  • +
  • -

#5 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Sum and Average Program

Posted 22 September 2011 - 10:02 AM

I'm not really following what you mean by 'tests'. Could you elaborate?
Was This Post Helpful? 0
  • +
  • -

#6 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Sum and Average Program

Posted 22 September 2011 - 10:08 AM

And by the way, casting this won't work either, because it first divides the two int variables, resulting in an int. Casting to double at this point just adds a 0 to the end of it.
public static void main(String[] args){
    	int value = 234848;
    	double average = (double) (value / 4);
    	System.out.println(average);// outputs 58712.0
    }


The better way would either do what I said, or change your 'sum' variable to a double and divide by 4. Then if you cast the 'sum' variable back to an int, it'll just drop everything after the decimal.

This post has been edited by creative code monkey: 22 September 2011 - 10:12 AM

Was This Post Helpful? 2
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Sum and Average Program

Posted 22 September 2011 - 10:09 AM

Gah, I apologize.. went into beginner help mode and forgot this isn't a "find student test average" program haha. Well my statement still stands just change tests to the numbers. You have a total of 4 numbers, and dividing by 4.0 is making it a double which is insinuating that you could divide it by say 3.3? Well, where is the .3 of the number? Which number should we take 30% from? Just doesn't work like that. When getting the average you don't divide by part of the number of scores. Have to divide by the total number of scores. Which means it cannot be an double since it needs to be a whole (int). Which is why you should cast it rather then make it 4.0

make sense?

Edit- Oops I casted outside of the parenthesis on accident.
averageOfNumbers = ((double)sumOfNumbers / 4); Mis-Clicked my apologies. Thank you for catching that!

This post has been edited by Fuzzyness: 22 September 2011 - 10:11 AM

Was This Post Helpful? 0
  • +
  • -

#8 creative code monkey  Icon User is offline

  • New D.I.C Head

Reputation: 14
  • View blog
  • Posts: 45
  • Joined: 14-September 11

Re: Sum and Average Program

Posted 22 September 2011 - 10:14 AM

Aah I see. I was wondering what you were thinking :)
Was This Post Helpful? 0
  • +
  • -

#9 nathan_williamson94  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 08-March 11

Re: Sum and Average Program

Posted 22 September 2011 - 10:57 AM

Thank you very much for all of your help. I changed my code to...

 averageOfNumbers = ((double)sumOfNumbers / 4); 



and everything works great. Also I learned I little extra by the conversation on how I should change my line of code.
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Sum and Average Program

Posted 22 September 2011 - 11:03 AM

Glad creative code monkeycreative code monkey and I could help. have any questions on things just let me know!

Happy Coding!
Fuzzy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1