4 Replies - 1006 Views - Last Post: 03 May 2013 - 07:20 PM Rate Topic: -----

#1 mjanth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-May 13

Promblem with Gradebook

Posted 02 May 2013 - 08:25 PM

Description
A teacher has ve students who have taken four tests. The teacher uses the following grading scale
to assign a letter grade to a student, based on the average of his or her four test scores.

Test Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F

The student and score data are stored in the given le StudentInfo.txt. Create a class named
GradeBook in a le named GradeBook.java. The class GradeBook is required to use a String
array to hold the ve students' names, an array of ve characters to hold the ve students' letter
grades, and ve arrays of four doubles each to hold each student's set of test scores. The class
should have methods that return a speci c student's name, average test score, and a letter grade
based on the average.

The partial program that demonstrates the class GradeBook is given in the le Grade-BookDemo.java. You are required to read and understand the given program, and then ll in the
missing code in GradeBookDemo.java to make the program complete and work properly with
the class GradeBook. The sample output of the program is given in the le SampleOutput.txt.
Do NOT make any change in the given program except the location where you are
required to insert code.

Here is the text in the StudentInfo.txt file:
Joanne Smith
98
89
100
76
Will Jones
67
89
91
88
Kerry McDonald
78
79
88
91
Sam Young
88
98
76
56
Jill Barnes
94
93
91
98

Here is my code thus far:
GradeBookTest.Java
import java.util.Scanner;
import java.io.*;

/**
 * GradeBookDemo program
 * This program tests the GradeBook class by
 * using the data stored in the StudentInfo.txt
 * file.
 */

public class GradeBookTest
{
	public static void main(String[] args)
	                    throws IOException
	{
		// Create a GradeBook object.
		GradeBook gb = new GradeBook();

		// Read the data from the file and put the data into grade book gb
		readFromFile(gb);
		
		// Display the student data.
		for (int i = 1; i <= 5; i++)
		{
			System.out.println("Name : " + gb.getName(i) +
			                   "\tAverage score: " + 
									 gb.getAverage(i) +
									 "\tGrade: " + 
									 gb.getLetterGrade(i));
		}
	}


	/**
	 * readFromFile method
	 */
	
	public static void readFromFile(GradeBook gb)
	                    throws IOException
	{
		/* Missing code start here */
		int i = 0;
		final int NUM_STUDENTS = 5;
		String[] names = new String[ NUM_STUDENTS];
	
		//Open the file
		File file = new File ("StudentInfo.txt");
		Scanner inputFile = new Scanner (file);
		
		//Read the file
		while (inputFile.hasNext())
		{
			names[i] = inputFile.nextLine();
			i++;
		}
		
		//Close the file
		inputFile.close();



                /* Missing code end here */
	}
}



GradeBook.Java
/**
 Author: Michael J. Anthony & Jake Mueller
 Class: CIS 110 Sec:2
 Instructor: Dr. He
 Assignment: MP2
 Date Last Updated: 5/1/2013
 */

public class GradeBook
{
   // Constant for the number of students
   private final int NUM_STUDENTS = 5;

   // Constant for the number of tests
   private final int NUM_TESTS = 3;

   // Array to hold student names
   private String[] names = new String[NUM_STUDENTS];

   // Array to hold student grades
   private char[] grades = new char[NUM_STUDENTS];

   // Create arrays of scores, one for each student.
   private double[] scores1 = new double[NUM_TESTS];
   private double[] scores2 = new double[NUM_TESTS];
   private double[] scores3 = new double[NUM_TESTS];
   private double[] scores4 = new double[NUM_TESTS];
   private double[] scores5 = new double[NUM_TESTS];

   /**
      The setName method assigns a student's name.
   */

   public void setName(int studentNumber, String name)
   {
      names[studentNumber-1] = name;
   }

   /**
      The setScores method copies an array of test scores
      to a student's array of scores.
   */

   public void setScores(int studentNumber, double[] scores)
   {
          switch(studentNumber)
          {
              case 1:copyArray(scores1,scores); break;
              case 2:copyArray(scores2,scores); break;
              case 3:copyArray(scores3,scores); break;
              case 4:copyArray(scores4,scores); break;
              case 5:copyArray(scores5,scores); break;
              default:break;
          }
   }

   /**
      The getName method returns a student's name.
   */

   public String getName(int studentNumber)
   {
       return names[studentNumber-1];
   }

   /**
      The getAverage method returns a student's average 
      test score.
   */

   public double getAverage(int studentNumber)
   {
       double avg=0.0;
       switch(studentNumber)
          {
              case 1:avg = calcAverage(scores1); break;
              case 2:avg = calcAverage(scores2); break;
              case 3:avg = calcAverage(scores3); break;
              case 4:avg = calcAverage(scores4); break;
              case 5:avg = calcAverage(scores5); break;
              default:break;
          }
       return avg;
   }

   /**
      The getLetterGrade method returns a student's 
      letter grade.
   */

   public char getLetterGrade(int studentNumber)
   {
           char lettergrade; //your code
       if(getAverage(studentNumber)>=90 && getAverage(studentNumber)<=100)
           lettergrade = 'A';
           else if(getAverage(studentNumber)>=80 && getAverage(studentNumber)<=89)
           lettergrade = 'B';
           else if(getAverage(studentNumber)>=70 && getAverage(studentNumber)<=79)
           lettergrade = 'C';
           else if(getAverage(studentNumber)>=60 && getAverage(studentNumber)<=69)
           lettergrade = 'D';
           else
               lettergrade = 'E';
           return lettergrade;


   }

   /**
      The copyArray method copies the contents of 
      one array to another.
   */

   private void copyArray(double[] to, double[] from)
   {
        System.arraycopy(from, 0, to, 0, from.length);
   }

   /**
      The calcAverage method calculates the average
      of the values in an array of test scores.
   */

   private double calcAverage(double[] scores)
   {
       double sum=0;
       for(int i=0; i<scores.length; i++)
           sum+=scores[i];
       return sum/scores.length;

   }

   /**
      The determineGrade method determines the letter 
      grade for a test average.
   */

   public char LetterGrade(double average)
   {
       char lettergrade; 
       if(average>=90 && average<=100)
    	   		lettergrade = 'A';
           else if(average>=80 && average<=89)
        	   	lettergrade = 'B';
           else if(average>=70 && average<=79)
        	   	lettergrade = 'C';
           else if(average>=60 && average<=69)
        	   	lettergrade = 'D';
           else
        	   	lettergrade = 'E';
           return lettergrade;
   }
}





I am completely lost at this point. I am frustrated. I am mostly having trouble with reading the data in from the file.

Is This A Good Question/Topic? 0
  • +

Replies To: Promblem with Gradebook

#2 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Re: Promblem with Gradebook

Posted 03 May 2013 - 03:02 AM

well the first thing is you need to make a seperate array for scores after that when reading the file you must get the next double four times to get the score

        while (inputFile.hasNext())
        {
			names[i] = inputFile.nextLine();
			i++;
			grades[newi] =inputFile.nextDouble();
			newi++;
			grades[newi] =inputFile.nextDouble();
			newi++;
			grades[newi] =inputFile.nextDouble();
			newi++;
			grades[newi] =inputFile.nextDouble();
			newi++;
			//since the items we are looking for switches from double to string 
			//we must return the remaining white space
			if(inputFile.hasNext()){
			    //this will get the remaining whitespace before the next item we want
			    inputFile.nextLine();
			 }
		}
		
		//Close the file
		inputFile.close();



now you just need to call the appropriate methods to create a gradebook
Was This Post Helpful? 0
  • +
  • -

#3 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Promblem with Gradebook

Posted 03 May 2013 - 03:24 AM

I don't know how your teacher wants you to do this, but parallel arrays are a bad practice. So bad, in fact, that one of our resident administrators -- macosxnerd101 -- wrote a featured tutorial on the subject

Why not split the string and parse the last four elements to double? (Then create an object from that)
//A class to store each student
class Student
{
	private String name;
	private double[] grades;
	
	public Student(String name, double[] grades)
	{
		this.name = name;
		this.grades = new double[grades.length]; //4, in this case
		for(int i = 0; i < grades.length; i++)
		{
			this.grades[i] = grades[i];
		}
	}
	//And then write your Student member methods
}



//Declare your new variables:
String name, temp;
double[] grades = new double[4];
List<Student> students = new ArrayList<Student>(); //possible structure for storing students

//Then, in your loop:
while (inputFile.hasNext())
{
	//Collect 5 lines into one string
	temp = "";
	for(int i = 0; i < 5; i++)
		temp += inputFile.nextLine();

	//Split the string using newline as a delimiter
	names = temp.split("\\r?\\n");

	//The first index will be the name
	name = names[0];

	//Parse to double for the grades
	for(int i = 0; i < grades.length; i++)
		grades[i] = Double.parseDouble(names[i + 1]); //will return an error if the data isn't there
	
	//Create a new Student object
	students.add(new Student(name, grades));
}


This post has been edited by Gungnir: 03 May 2013 - 03:38 AM

Was This Post Helpful? 1
  • +
  • -

#4 mjanth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-May 13

Re: Promblem with Gradebook

Posted 03 May 2013 - 12:11 PM

Thank you everyone for your suggestions. They were very helpful.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Promblem with Gradebook

Posted 03 May 2013 - 07:20 PM

and put the newi++ on the same line
from
	grades[newi] =inputFile.nextDouble();
	newi++;
	grades[newi] =inputFile.nextDouble();
	newi++;


to
	grades[newi++] =inputFile.nextDouble();
	grades[newi++] =inputFile.nextDouble();


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1