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

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




Help with getting the right Average

 
Reply to this topicStart new topic

Help with getting the right Average

drew_z_2
12 Sep, 2008 - 07:29 AM
Post #1

New D.I.C Head
*

Joined: 24 Apr, 2008
Posts: 47


My Contributions
CODE

//StudentRecord.java
//Andrew Z Sept 11 2008

import java.util.StringTokenizer;

//A StudentRecord contains a student's first name and their gpa
public class StudentRecord
{
    private String name;
    private double gpa;

    //initialize to the given values
    public StudentRecord(String newName, double newGpa)
    {
        setName(newName);
        setGpa(newGpa);
    }
    //initialize to the empty record
    public StudentRecord()
    {
        this("" , 0);
    }

    //initialize record to values in given string
    public StudentRecord(String studentInfo)
    {
        StringTokenizer theTokenizer = new StringTokenizer(studentInfo);
            setName(theTokenizer.nextToken());
            setGpa(Double.parseDouble(theTokenizer.nextToken()));
    }

    //name is set to newName
    public void setName(String newName)
    {
        name= newName;

    }

    //gpa is set to newGpa
    public void setGpa(double newGpa)
    {
        gpa= newGpa;
    }

    //returns name
    public String getName()
    {
        return name;
    }

    //returns gpa
    public double getGpa()
    {
        return gpa;
    }

    //returns string representation of record
    public String toString()
    {
        return name+" "+ gpa;
    }
}


CODE

//AverageGpa
//Andrew Z Sept 11 2008

import java.io.*;

public class AverageGpa

{
    public static void main(String []args) throws IOException
    {
        
        StudentRecord[] student = new StudentRecord[10];
        System.out.println("Type in a list of a students name seperated by a space, then their gpa, one per line."
                        + "\nPress enter at blank line to stop" );
    //    int size = readDataIntoArray(data);
//    }
    
//    public static int readDataIntoArray(Integer[]data) throws IOException
//    {
        InputStreamReader reader= new InputStreamReader(System.in);
        BufferedReader keyReader = new BufferedReader(reader);
        
        String line=null;
        int count = 0;
        double average=0;
        while(true)
        {
            
            {
                line= keyReader.readLine();
                if(line.equals(""))
                    break;
                student[count] = new StudentRecord(line);
                
                count++;
            }
        }
    
        int track = 0;
        for(int x =0; x < count; x++)
        {
            average = average + student[x].getGpa();
            average = average/count;
            track++;
        }
        System.out.println("The average Gpa is " + average);    
        
        for(int y=0; y< track; y++)
        {
            if (student[y].getGpa() > average)
                System.out.println(student[y].getName()+ "'s gpa = "+ student[y].getGpa()+ " and is greater than average");
            else
                System.out.println(student[y].getName()+ "'s gpa= "+ student[y].getGpa()+ " and is less than the average");        
        }
    }
}


The problem I'm having is mymy for loop where i try to get the average I keep getting the wrong average for example when i enter 4 and 2 as my gpa's the average comes out as 2 ?? and the first set of code is my constructor



User is offlineProfile CardPM
+Quote Post

BigAnt
RE: Help With Getting The Right Average
12 Sep, 2008 - 02:34 PM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Aug, 2008
Posts: 361



Thanked: 31 times
Dream Kudos: 25
My Contributions

In the for loop for calculating the Gpa:
CODE
  
       int track = 0;
        for(int x =0; x < count; x++)
        {
            average = average + student[x].getGpa();
            average = average/count;
            track++;
        }



You should divide the total gpas by the count after the for loop or else the average will change each time you add an additional gpa, also if you want to get the total you should initialize track to 1 outside the for loop and then divide by track after the for loop

CODE

       int track = 1;
        for(int x =0; x < count; x++)
        {
            average = average + student[x].getGpa();
            track++;
        }
        average = average/track;

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:09AM

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