• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Getting grades into a gradebook

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create two classes, 1 called Student record and 1 called grade book. In student record I am collecting tha name and grades of the student . In gradebook I'm computing the average and sorting names alphabetically.I have computed the average in Student record because I cannot get gradebook to work. I will cut and paste the code when I get gradebook to work. I am trying to compile grade book but I get this error
C:\javas>c:\j2sdk1.4.1_02\bin\javac GradeBook.java
GradeBook.java:35: cannot resolve symbol
symbol : constructor StudentRecord (java.lang.String,int[])
location: class StudentRecord
table[currentSize] = new StudentRecord(name, grades);
^
1 error
his is my gradebook code:

This is studentrecord
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are trying to create a new StudentRecord object with the call to:

The only constructor you have in the StudentRecord class is:

So what's a compiler to do? The fix is to provide a constructor that takes a String and int[] thus:
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I di exactly what you recommended but when I used my test class i recieved this error.
C:\javas>java StudentTest
Exception in thread "main" java.lang.NoSuchMethodError: StudentRecord.<init>(Lja
va/lang/String V
at StudentTest.main(StudentTest.java:7)
Here are my changes!


This is my test code:

It worked before and know I don't understand why it is not working.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lisa, I wasn't suggesting that you remove the other constructor. It's OK to have more than one. You still have a problem with the new constructor. Your code completely ignores the int[] array of grades. Let me make a couple of suggestions:

[ June 26, 2003: Message edited by: Michael Morris ]
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So should I put the same constructor in gradebook?
 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have Student record working again and gradebook compiled. Can you look at grade book and let me know if the Studentrecord is going there. If so how do I sort it alphabetically and print it out?
I tried using util-array sort but I get an error. Should I use a compare method before call array sort.
This is grade book:
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Arrays.sort() to work, StudentRecord will need to implement the Comparable interface. It has one method:

It returns a negative int if this is less than obj, a positive int if greater and 0 if equal. Since you want to sort it alphabetically by student name and since string is a Comparable then the method would look something like this:


Then your sort() method in GradeBook would look like:

 
Lisa Smith
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I get what you are saying about that. Now my problem is implementing it in StudentTest. Here are my errors
StudentTest.java:42: cannot resolve symbol
symbol : method insertStudent ()
location: class StudentRecord
stud.insertStudent();
^
StudentTest.java:43: cannot resolve symbol
symbol : method insertStudent ()
location: class StudentRecord
stud1.insertStudent();
^
StudentTest.java:44: cannot resolve symbol
symbol : method insertStudent ()
location: class StudentRecord
stud2.insertStudent();
^
StudentTest.java:47: cannot resolve symbol
symbol : variable sort
location: class StudentTest
sort.table();
^
StudentTest.java:49: cannot resolve symbol
symbol : variable table
location: class StudentTest
table.print();
^
I know it has something to do with the methods that I have created, I just don't know what.
My code again
Gradebook

/**
*
*
*
*
*/
public class StudentRecord
{

final int arraySize = 25;
// Private instance varibles
private final String name; // student name
private final int[] grades; // grades
int Count; // number of grades per student
// Constructor
StudentRecord(String studentName, int[] grades1) {
if (grades1.length > arraySize)
{
throw new IllegalArgumentException("Grade array exceeds fixed size");
}
name = studentName ;
grades = new int[arraySize];
System.arraycopy(grades1, 0, grades, 0, grades1.length);
Count = grades1.length ;
} StudentRecord(String studentName)
{
//Call the two arg constructor with an empty grade array
this(studentName, new int[0]); }
public int compareTo(Object obj)
{
return this.name.compareTo(((StudentRecord) obj).name);
}
public void print ()
{
System.out.println ( "Name: " + name );
System.out.println( "Grades:");
for ( int j=0; j < Count ; j++ )
System.out.println ( " grade " + j + ": " + grades[ j ] );
}
public void addGrade ( int grade )
{
if ( Count < arraySize )
grades[Count] = grade ;
Count ++ ;
}
public int average ( )
{
int sum = 0 ;
for ( int j=0; j < Count; j++ )
sum += grades[ j ] ;
return sum / Count ;
}

}
Student Test

[CODE]
class StudentTest
{
public static void main ( String[] args )
{
// create a student object
StudentRecord stud = new StudentRecord( "Nate" ) ;
// add a few grades
stud.addGrade( 90 ) ;
stud.addGrade( 95 ) ;
stud.addGrade( 88 ) ;
stud.addGrade( 78 ) ;
stud.addGrade( 82 ) ;
stud.addGrade( 97 ) ;
StudentRecord stud1 = new StudentRecord( "Yvette" ) ;
// add a few grades
stud1.addGrade( 90 ) ;
stud1.addGrade( 95 ) ;
stud1.addGrade( 88 ) ;
stud1.addGrade( 78 ) ;
stud1.addGrade( 82 ) ;
stud1.addGrade( 97 ) ;
StudentRecord stud2 = new StudentRecord( "Maria" ) ;
// add a few grades
stud2.addGrade( 90 ) ;
stud2.addGrade( 95 ) ;
stud2.addGrade( 88 ) ;
stud2.addGrade( 78 ) ;
stud2.addGrade( 82 ) ;
stud2.addGrade( 97 ) ;

stud.print() ;
stud1.print() ;
stud2.print() ;

stud.insertStudent();
stud1.insertStudent();
stud2.insertStudent();

sort.table();
table.print();
System.out.println( "Average grade: " + stud.average() );
}
}
How can I print this alphabetically after they are averaged?
[CODE]
reply
    Bookmark Topic Watch Topic
  • New Topic