2 Replies - 8016 Views - Last Post: 24 November 2008 - 08:13 PM Rate Topic: -----

#1 parrothead31   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-November 08

Gradebook assignment

Posted 24 November 2008 - 04:37 PM

I have an assignment that has me completely baffled.
This Java program will compute grade averages for students, and we will do
it with objects. You will write a class to represent a student and his/her
information, and instantiate an object of that class for each student that
you need to save information for.

Your program will take one command line parameter, an integer, that
tells how many students you are dealing with. Your program will then
read in from the keyboard a collection of information and grades for
students and then find the class average on each exam. It will also
compute the overall average of all grades across all students.

I have the person.java file and it is fine. I feel like my student.java file is on the right track and the main method file which is gradebook.java is giving me fits:

gradebook.java code:

import java.io.*; // Import all classes in java.io package.  Saves typing.

class GradeBook {

public static void main(String[] args) throws IOException {  
	int nPlay = 2;
	 if (args.length>0) {
	 	nPlay = Integer.parseInt(args[0]);
	 }
	 
	 Student[] students = new Student[nPlay];
	 	 
	 // put student objects into the array slots
	 for (int s=0; s<nPlay; s++) {
		students[s] = new Student();
	 } 	 

	String line;   // to hold user keyboard input
	double num = 0;	  // to hold the integer converted from the the user input
	 int i;
	 int MAX;
	 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

	 try {
	 	 MAX = Integer.parseInt(args[0]);
		 if (MAX <= 0) { System.out.println("command line needs to be positive integer"); return; }
	 } catch (Exception e) {
		System.out.println("command line needs to be a positive integer");
		 return;
	 }

	 double[] grade1; grade1 = new double[MAX];
	 double[] grade2; grade2 = new double[MAX];
	 double[] grade3; grade3 = new double[MAX];
	 String[] name = new String[MAX];
	 	 	 
	for (i=0; i<name.length; i++) {	
	  System.out.print("Student's First name? "); line = in.readLine();
		students[i].setFName(line);
	  if ( line.equals("quit") || line.equals("q") ) { break; }
		name[i]= line;
		
		System.out.print("Student's Last name? "); line = in.readLine();
		students[i].setLName(line);
	  if ( line.equals("quit") || line.equals("q") ) { break; }
		name[i]= line;

		
		System.out.println("grade 1? ");	line = in.readLine();
	  num = Double.parseDouble(line);
		grade1[i] = num;
		System.out.println("grade 2? ");	line = in.readLine();
	  num = Double.parseDouble(line);
		grade2[i] = num;
		System.out.println("grade 3? ");	line = in.readLine();
	  num = Double.parseDouble(line);
		grade3[i] = num;		
	}// end for
	 
	
	 
	 printArray(grade1);	 System.out.println();
	 printArray(grade2);	 System.out.println();
	 printArray(grade3);	 System.out.println();
	 
	 System.out.println("Largest grade on first exam is ");
  }// end main method
  
  public static void printArray (double[] arr) {
	int i;
	 for (i=0; i<arr.length; i++) {
	   System.out.print(arr[i]+", ");
	 }
	 return;
  }
  
   	   public double getAverage()
		   {	  
			  int total = 0; // initialize total
		 
			  // sum grades for one student
			  for ( int grade : grades )   
				 total += grade;		   
		
			  // return average of grades
			  return (double) total / grades.length;
		   } // end method getAverage



	}
	








and my student.java code



public class Student extends Person {

	  public static int SIDcounter = 100; // counter for SID values
										  // this field is common to all
										  // Student objects

	  int SID;

	  // in here you create other fields not already in Person

	  Student () {
		 SID = SIDcounter++; // this loads the SID field for this student
							 // with the next value from the counter
		 // more code as needed
	  }

	  // more methods as needed
		
 }




and for reference sake person.java as well:





class Person {
  // instance variables, local fields
  // each object gets its own copy of these
  protected String lName;
  protected String fName;
  protected int SSN;
  
  Person () {
	 lName = "Last";
	  fName = "First";
	  SSN = 123456789;
  } 
  
  // accessors
  public void setLName ( String name ) { lName = name; }
  public String getLName ()			{ return lName; }
  public void setFName ( String name ) { fName = name; }
  public String getFName ()			{ return fName; }
  public void setSSN ( int num )	   { SSN = num; }
  public int getSSN ()				 { return SSN; }

  // general methods
   
}



Thanks for all the help

Is This A Good Question/Topic? 0
  • +

Replies To: Gradebook assignment

#2 pbl   User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Gradebook assignment

Posted 24 November 2008 - 08:02 PM

As your assignment says:
- you shoul keep the grades into your Student class

And hy not filling all the fields in the constructor ?
class Student {
   String lastName, firstName;
   int SID;
   double grade1, grade2, grade3;

   // constructor
   Student(String last, String first, double g1, double g2, double g3) {
	  lastName = last;
	  firstName = first;
	  grade1 = g1;
	  grade2 = g2;
	  grade3 = g3;
   }
}



Then instead of
students[s] = new Student();

prompt nPlay times for both names and the 3 grades and then build your student:

sudents[s] = new Student(last, first, g1, g2, g3);
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Gradebook assignment

Posted 24 November 2008 - 08:13 PM

public double getAverage()
{	  
   int total = 0; // initialize total
		 
   // sum grades for one student
   for ( int grade : grades )   
      total += grade;		   
		
   // return average of grades
   return (double) total / grades.length;
} // end method getAverage



Where is grades defined?

This post has been edited by n8wxs: 24 November 2008 - 08:14 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1