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

Thread: I need calculation

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need calculation

    /*
    	Chapter 5: Programming Assignment 7
    	Programmer:
       	Date:
    	Program Name: AveragingGradesTemplate
    */
     
    import java.io.*;
     
    public class AveragingGradesTemplate
    {
       public static void main(String[] args) throws IOException
       {
          //Declaring variables
          BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
          	String grade, strSize;
          	float average;
          	int size;
     
          System.out.print("How many grades will you enter? ");
          	strSize = dataIn.readLine();
       	 	size = Integer.parseInt(strSize);
    		float grades[] = new float[size];
     
          for (int i=1; i<size; i++)
          {
     
    	   //Get input from user
    			 System.out.print("Enter grade " + i + ": ");
    			 grade = dataIn.readLine();
    			 grades[i] = Float.parseFloat(grade);
     
          }
     
    	  average = averageGrades(grades, size);
     
     
     
          //Write a for loop to display grades
     
     
     
     
          System.out.println("");
          System.out.println("The average grade was "+average);
       }
     
       public static float averageGrades(float[] g, int s)
       {
    	   float total = 0;
    	   float a;
    	   // write a for loop to calculate the total
                       	??????????????????????????????????
                          ?? ?????????????????????????????????
                           ?????????????????????????????????
     
    	   a = total / s;
    	   return a;
       }
     
    }

    I need to write a for loop for my calculations and I am lost.


  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: I need calculation

    How would you do this by hand, without a computer?

    Pretend you're given a stack of index cards that each show a number (that's your array). How would you go about calculating their total? Their average?

    Pretend you have a friend who has no idea how to do that. Write out specific, simple instructions about what to do. When you have that done, you'll have an algorithm that should be pretty easy to translate to code.

    PS- When posting code, make sure you use the code tags. Nobody likes to look at unformatted code.
    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. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need calculation

    grade [1];
    grade [2];
    and so on

  4. #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: I need calculation

    Quote Originally Posted by Swiss518 View Post
    grade [1];
    grade [2];
    and so on
    Okay. That's not really what I asked, but if you're just looking for a tutorial on for loops (which I found as the first hit after googling "java for loops"), here y'ar: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    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!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need calculation

    It doesnt make sense. Can you give me an example to so what i need to do. this is what i got!!!

    for (int i=1; i<size; i++)

  6. #6
    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: I need calculation

    What doesn't make sense?

    That's a fine start. Maybe try printing out the value of i each time, to give you an idea of what's going on?
    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!

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need calculation

    [i] =80
    [i]= 70

    or less I nees to take a variabe and set it to something.

  8. #8
    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: I need calculation

    Quote Originally Posted by Swiss518 View Post
    [i] =80
    [i]= 70

    or less I nees to take a variabe and set it to something.
    I'm not sure what you're trying to say. Focus on one thing at a time. First, write a for loop that simply prints out a series of numbers (like 1-10, or 0-9).

    Then look at the array tutorial (again, which a quick google search found immediately) and figure out how to access specific indices: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    When you have both of those working separately, then worry about combining them.
    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. Looping help needed for math calculation
    By pcornacchione in forum What's Wrong With My Code?
    Replies: 14
    Last Post: October 1st, 2010, 12:19 AM
  2. RPM Calculation
    By fobos3 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 21st, 2010, 08:53 AM
  3. SQL Time Calculation
    By r12ki in forum JDBC & Databases
    Replies: 3
    Last Post: July 31st, 2009, 03:54 AM