Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By harry

Thread: Java GradeBook - Arrays Help

  1. #1
    harry is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Question Java GradeBook - Arrays Help

    What needs to be modified in StudentManager class so that it uses constructor instead of the following lines.

    gradeBook[i].setFirstName(fName);
    gradeBook[i].setLastName(lName);
    gradeBook[i].setTestScores(tstScores);


    StudentManager.java


    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class StudentManager
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            Tests[] gradeBook = new Tests[10];
    
            String fName, lName;
            double[] tstScores = new double[5];
    
            double classAverage = 0.0;
    
            Scanner inFile =
    		              new Scanner(new FileReader("StudentData.txt"));
    
    		for (int i = 0; i < 10; i++)
    		    gradeBook[i] = new Tests();
    
            for (int i = 0; i < 10; i++)
            {
    			fName = inFile.next();
    			lName = inFile.next();
    
                for (int j = 0; j < 5; j++)
                    tstScores[j] = inFile.nextDouble();
    
                gradeBook[i].setFirstName(fName);
                gradeBook[i].setLastName(lName);
                gradeBook[i].setTestScores(tstScores);
    		}
    
            System.out.println("First_Name  Last_Name     Test1  Test2  "
                              + "Test3  Test4  Test5   Average Grade");
    
    		for (int i = 0; i < 10; i++)
    		{
    		    System.out.println(gradeBook[i]);
    
    		    classAverage = classAverage + gradeBook[i].getAverage();
    		}
    
    		classAverage = classAverage / 10;
    
    		System.out.println();
    
    		System.out.printf("Class average = %.2f%n", classAverage);
        }//end main
    }
    Tests.java

    Java Code:
    public class Tests
    {
    	private final int numberOfTests = 5;
    
        private String firstName;
        private String lastName;
        private double[] testScores;
        private double average;
        private char grade;
    
        Tests()
        {
            firstName = "";
            lastName = "";
            testScores = new double[numberOfTests];
            average = 0.0;
            grade = '*';
        }
    
        Tests(String fName, String lName, double[] ts)
        {
            firstName = fName;
            lastName = lName;
            testScores = new double[numberOfTests];
            for (int i = 0; i < numberOfTests; i++)
                testScores[i] = ts[i];
            average = getAverage();
            grade = getGrade();
        }
    
        public void setFirstName(String fName)
        {
            firstName = fName;
        }
    
        public void setLastName(String lName)
        {
            lastName = lName;
        }
    
        public String getFirstName()
        {
            return firstName;
        }
    
        public String getLastName()
        {
            return lastName;
        }
    
        public void setTestScores(double[] ts)
        {
            for (int i = 0; i < numberOfTests; i++)
                testScores[i] = ts[i];
        }
    
        public void setTestScr(double scr, int index)
        {
            if (0 <= index && index < numberOfTests)
                testScores[index] = scr;
            else
                System.out.println("Invalid array index.");
        }
    
        public double getTestScr(int index)
        {
            if (0 <= index && index < numberOfTests)
                return testScores[index];
            else
            {
                System.out.println("Invalid array index.");
    
                return -1.0;
    		}
        }
    
        private void calculateAverage()
        {
    		for (int i = 0; i < numberOfTests; i++)
                average = average + testScores[i];
    
            average = average / 5;
    	}
    
        public double getAverage()
        {
    		average = 0.0;
    
    		calculateAverage();
    
            return average;
        }
    
        private void calculateGrade()
        {
    		double avg = getAverage();
    
            if (avg >= 90.00)
                grade =  'A';
            else if (avg >= 80.00)
                grade =  'B';
            else if (avg >= 70.00)
                grade =  'C';
            else if (avg > 60.00)
                grade =  'D';
            else
                grade =  'F';
        }
    
        public char getGrade()
        {
    		calculateGrade();
    		return grade;
    	}
    
        public String toString()
        {
    		average = 0.0;
    
    		calculateAverage();
    		char grade = getGrade();
    
    		String scoreString = "";
    
    		for (int i = 0; i < numberOfTests; i++)
    		    scoreString = scoreString + String.format("%7.2f", testScores[i]);
    
            return String.format("%-11s %-11s %35s %7.2f %5c", firstName,
                                 lastName, scoreString, average, grade);
        }
    }
    Last edited by harry; 08-23-2013 at 08:48 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Java GradeBook - Arrays Help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    harry is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Java GradeBook - Arrays Help

    DarrylBurke and gimbal2 like this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Java GradeBook - Arrays Help

    Good show!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 11-28-2012, 02:53 AM
  2. Replies: 0
    Last Post: 05-03-2012, 12:55 AM
  3. Gradebook Program... Newbie
    By java_beaner in forum New To Java
    Replies: 2
    Last Post: 09-24-2011, 07:55 PM
  4. GradeBook Program
    By just_in_deed in forum New To Java
    Replies: 0
    Last Post: 09-10-2011, 06:19 AM
  5. GradeBook Class Modifications
    By awaken38 in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 09:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •