Welcome to Dream.In.Code
Become a Java Expert!

Join 150,068 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,812 people online right now. Registration is fast and FREE... Join Now!




Need help with why average returns .00 from my array

 
Reply to this topicStart new topic

Need help with why average returns .00 from my array

brannon
6 Jun, 2008 - 08:06 PM
Post #1

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 5

I need some assistance with why my average only returns .00. I'm sure it's simple, for those who love coding. Please help me with where I went wrong.

Thanks!!

CODE

// DecimalFormat is speific to display numbers formatted to a certain pattern
import java.text.DecimalFormat;        

// Class name
public class GradeAverageW5                
{
    // declare any variables
    private static double myAverage;
    
    // *FOR AVERAGE* this makes the output display show two decimal places
    private static DecimalFormat two = new DecimalFormat("#,###.00#");    
        
    // *FOR AVERAGE* setter
    public static void setmyAverage(double average)
    { myAverage = average; }
    
    // *FOR AVERAGE* getter
    public static float getmyAverage()
    { return (float)myAverage;    }
    
    
    // *FOR GRADEAVERAGE* This method calculate the average
    public static float getmyAverage(float[] floatArray1)
    {        
        float grade = 0;
        float average = 0;
        int i=0;
        
        for (i=0; i<floatArray1.length; i++)
        {
            grade =  grade + floatArray1[i];
            myAverage = grade/floatArray1.length;
        }
        return (float) myAverage;
    }


    // *FOR ARRAY AND GRADEAVERAGE*
    // This method runs through the Java array and passes it along to the printList method to display them
    public void readList(float[] floatArray1)
    {

        for
        (int i = 0; i < floatArray1.length; i++)  // start i out as 0 and increment by one
            {printList(i,floatArray1); } // call the printList method for output
    } // End of readList method
    
    
    // *FOR ARRAY* Java method printList shows output on the screen for this program
    public void printList(int i,float[] floatArray1)
    {
        //this makes the output display show one decimal place but space for three numbers to the left of the decimal
        DecimalFormat oneDecimalPlace = new DecimalFormat("##0.0");
        
        // Display output of each grade in the array along with the correcponding letter grade
        System.out.println("\nAssignment #"+(i+1)+" grade: "+oneDecimalPlace.format(floatArray1[i]));

        // Call the displayLetter switch method to change numerical grade to a letter grade
        displayLetter(i,floatArray1);
    } // End of printList method
    
    // *FOR ARRAY* Number to letter method with the switch statement
     public void displayLetter ( int i, float[] floatArray1 )
     {
         int whichGrade = 0; // initializing the whichGrade variable
         if ( floatArray1[i] >= 95 && floatArray1[i] <= 100 ) whichGrade = 1;   // test 1
         if ( floatArray1[i] >= 90 && floatArray1[i] <=  94 ) whichGrade = 2;   // test 2
         if ( floatArray1[i] >= 87 && floatArray1[i] <=  89 ) whichGrade = 3;   // test 3
         if ( floatArray1[i] >= 84 && floatArray1[i] <=  86 ) whichGrade = 4;   // test 4
         if ( floatArray1[i] < 60 ) whichGrade = 5;   // test 5
        
        // Here is my switch statement for the above options
        switch (whichGrade)
        {
            case 1: System.out.println("You have an A"); break;  // output for case 1
            case 2: System.out.println("You have an A-"); break;  // output for case 2
            case 3: System.out.println("You have a B+"); break;  // output for case 3
            case 4: System.out.println("You have a B"); break;  // output for case 4
            case 5: System.out.println("You have an F"); break;  // output for case 5
            default: System.out.println("You have an invalid grade!");break;  // output for bad whichGrade array variable
        } // End of switch options
        
     } // End of displayLetter method
    
    
// ***  MAIN  ******************************************** //
    public static void main(String[] args)
    {
        GradeAverageW5 AG = new GradeAverageW5();
        
        // This is my instance variable of type array
        float[] floatArray1 = new float[]{95.2f,93.5f,88.0f,84.5f,57.2f};

        // General output
        System.out.println("\nWeek 5 - Daily Question 2 Program...");
        
        // Get the floatArray1 array list values
        AG.readList(floatArray1);
        
        // *FOR AVERAGE* output
        System.out.println("\nThe grade average is " + two.format(getmyAverage()));
        
    } // End of main method
}

User is offlineProfile CardPM
+Quote Post

thenovices
RE: Need Help With Why Average Returns .00 From My Array
6 Jun, 2008 - 08:43 PM
Post #2

D.I.C Head
**

Joined: 18 Jan, 2008
Posts: 73



Thanked: 7 times
My Contributions
you never actually set your average. you read in all the scores, but you never set the average.

Just change the last line from

CODE
System.out.println("\nThe grade average is " + two.format(getmyAverage()));


to

CODE
System.out.println("\nThe grade average is " + two.format(getmyAverage(floatArray1)));



This way will get you the desired result, but it doesn't set the average either.

Another solution is to add a line in the readList method:

CODE

    public void readList(float[] floatArray1)
    {

        for
        (int i = 0; i < floatArray1.length; i++)  // start i out as 0 and increment by one
            {printList(i,floatArray1); } // call the printList method for output
        setmyAverage(getmyAverage(floatArray1));
    } // End of readList method


This post has been edited by thenovices: 6 Jun, 2008 - 08:45 PM
User is offlineProfile CardPM
+Quote Post

brannon
RE: Need Help With Why Average Returns .00 From My Array
6 Jun, 2008 - 08:47 PM
Post #3

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 5

Thank you SOOOO much! I knew it had to be something simple. Don't know how long I looked at it and didn't see that.

KUDOS!!!
User is offlineProfile CardPM
+Quote Post

pbl
RE: Need Help With Why Average Returns .00 From My Array
6 Jun, 2008 - 08:48 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
kind of weird
don't need to calculate average at every loop iteration
just once will do it

from
CODE

       for (i=0; i<floatArray1.length; i++)
        {
            grade =  grade + floatArray1[i];
            myAverage = grade/floatArray1.length;
        }
        return (float) myAverage;


to

CODE

       for (i=0; i<floatArray1.length; i++)
        {
            grade =  grade + floatArray1[i];
        }
        return  grade/floatArray1.length;


User is online!Profile CardPM
+Quote Post

brannon
RE: Need Help With Why Average Returns .00 From My Array
6 Jun, 2008 - 09:03 PM
Post #5

New D.I.C Head
*

Joined: 6 Jun, 2008
Posts: 5

Your absolutely correct pbl. I appreciate your taking the time to look at my code. Excellent catch! Definitely a beginner.

This post has been edited by brannon: 6 Jun, 2008 - 09:10 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:54PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month