Can someone work with me on this program. I will put the instructions first, then my code, then the errors

INSTRUCTIONS:
Create a program to enter grades and calculate averages and letter grades.
1. Need a class which will contain:
a. Student Name
b. Student Grades (an array of 3 grades)
c. A constructor that clears the student data (use -1 for unset grades)
d. Accessors (get functions) for each of the above, average, and letter grade
e. Mutators (set functions) for items a, b, c
f. Note that the accessor and mutator for Student grades has to have an argument for the grade index.
2. Need another class which will contain:
a. An Array of Students (1 above)
b. A count of number of students in use
c. Constructor that reads data from a text file and sets up the students
3. You need to create a graphical user interface that allows you to:
a. Read data from file
b. Add new students
c. Process existing students
d. Add test grades
e. Based on a radio button setting display either the average or the letter grade
f. Save modified data to file
4. A possible graphical look is as follows:

5. Add comments and use proper indentation.
NOTE: You need actionCommand to handle saving a file and you cannot modify actionCommand with a “throws IOException” clause. The way to solve this is as follows:
Assuming that you have a method called saveData that saves the data, change its call as follows:
try
{ saveData();
} catch (IOException x)
{
}
The function saveData, your function to load data, and the constructor will need a “throws IOException” clause.
Additional Information:
• An accessor is needed to save the test grades, which should always be numbers.
• I would like that system to accept a student with no grades, then later add one or more grades, and when all grades are entered, calculate the final average or grade.
• The way to handle changes in data using a text file is:
o Open the data file for reading
o load all the data into an array of students from the file,
o Close the file
o Make all modifications to the data in the array of students.
o When saving, open the datafile for writing
o Loop through the students and save the data to the datafile.
o Close the data file.


MY CODE

import java.util.*; 
   import java.io.*;
   import javax.swing.*; 
   import java.awt.*; 
   import java.awt.event.*; 
  

   public class GradeCalculator2 extends JFrame 
   { 
      private static final int WIDTH = 550; 
      private static final int HEIGHT = 430; 
      int GradeCalc; 
      int student; 
      private StudentList[] sList = new StudentList[20]; 
   
  		 public class StudentList 
   { 
      String name; // Student's name. 
      double test1, test2, test3; // Grades on three tests. 
      double getAverage() { // compute average test grade 
         return (test1 + test2 + test3) / 3; 
       
   }
 			
   // instance variables 
      private int noOfStudents; 
      private double score, tst1, tst2, tst3; 
      private double classAvg, stAvg, totalScore; 
      private int displayedStudentIndex = 0; 
      private char ltrGrade; 
      private String stName; 
   
   // This area is for the GUI components 
   // Each item that will be displayed will 
   // have a label and a textfield, (L) and (TF), 
   // respectively. 
      private JLabel stNameL, tst1L, tst2L, tst3L, 
      classAvgL, stAvgL, headingL; 
      private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
      private JTextArea classAvgTA, stAvgTA; 
      private JButton exitB, nextB, prevB, calcGrade; 
   
      private ButtonHandler bHandler; 
   
	   public GradeCalculator2() 
      { 
         setTitle("Grade Calculator"); // set's the title 
         setSize(WIDTH, HEIGHT); // set the window size 
         Container pane = getContentPane(); // get the container 
         pane.setLayout(null); // set the container's layout to null 
      
         bHandler = new ButtonHandler(); // instantiate the button event handler 
      
      // instantiate the labels 
         headingL = new JLabel("STUDENT RECORD"); 
         stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
         tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
         tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
         tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
         stAvgL = new JLabel("Student Average " 
            + "\n" + "Class Average"); 
				
      //instantiate the text fields 
         stNameTF = new JTextField(65); 
         tst1TF = new JTextField(10); 
         tst2TF = new JTextField(10); 
         tst3TF = new JTextField(10); 
      
      // instantiate the text area 
         classAvgTA = new JTextArea(6, 20); 
         classAvgTA.setAutoscrolls(true); 
      
      // instantiate the buttons and register the listener 
         exitB = new JButton("Exit"); 
         exitB.addActionListener(bHandler); 
      
         nextB = new JButton("Next"); 
         nextB.addActionListener(bHandler); 
      
         prevB = new JButton("Previous"); 
         prevB.addActionListener(bHandler); 
      
         calcGrade = new JButton("Calc Grade"); 
         calcGrade.addActionListener(bHandler); 
      
      // set the size of the labels, text fields, and buttons 
         headingL.setSize(200, 30); 
         stNameL.setSize(100, 30); 
         stNameTF.setSize(100, 30); 
         tst1L.setSize(100, 30); 
         tst1TF.setSize(100, 30); 
         tst2L.setSize(120, 30); 
         tst2TF.setSize(100, 30); 
         tst3L.setSize(100, 30); 
         tst3TF.setSize(100, 30); 
         classAvgTA.setSize(370, 120); 
         calcGrade.setSize(100, 30); 
         prevB.setSize(100, 30);
         nextB.setSize(100, 30); 
         exitB.setSize(100, 30); 
      
      //set the location of the labels, text fields, 
      //and buttons 
         headingL.setLocation(220, 10); 
         stNameL.setLocation(20, 50); 
         stNameTF.setLocation(120, 50); 
         tst1L.setLocation(20, 100); 
         tst1TF.setLocation(120, 100); 
         tst2L.setLocation(300, 50); 
         tst2TF.setLocation(420, 50); 
         tst3L.setLocation(300, 100); 
         tst3TF.setLocation(420, 100); 
         classAvgTA.setLocation(70, 230); 
         prevB.setLocation(120, 370); 
         exitB.setLocation(220, 370); 
         nextB.setLocation(320, 370); 
         calcGrade.setLocation(420, 370); 
      
      //add the labels, text fields, and buttons to the pane 
         pane.add(headingL); 
         pane.add(stNameL); 
         pane.add(stNameTF); 
         pane.add(tst1L); 
         pane.add(tst1TF); 
         pane.add(tst2L); 
         pane.add(tst2TF); 
         pane.add(tst3L); 
         pane.add(classAvgTA); 
         pane.add(calcGrade); 
         pane.add(prevB); 
         pane.add(exitB); 
         pane.add(nextB); 
      
         setVisible(true); //show the window 
         setDefaultCloseOperation(EXIT_ON_CLOSE); 
         System.exit(0); 
			
			/**
				This program creates an instance of the GradeCalculator2 class, 
				which displays a window on the screen.
			*/
			
			public class GradeCalculator2
			{
				public static void main(String[] args
				{
					new GradeCalculator2();
				
      } 
   }
}

ERRORS:
GradeCalculator_2.java:45: invalid method declaration; return type required
public GradeCalculator2()
^
GradeCalculator_2.java:143: illegal start of expression
public class GradeCalculator2
^
GradeCalculator_2.java:145: ')' expected
public static void main(String[] args
^
GradeCalculator_2.java:151: reached end of file while parsing
}
^
4 errors

Recommended Answers

All 14 Replies

Member Avatar for coil

Just from looking at your errors, here are the problems:

1. At line 21, you're missing a closing brace. That probably threw everything else off. Put the brace there and see if you still get all these errors.

GradeCalculator_2.java:45: invalid method declaration; return type required
2. I think that this error should be fixed if you put the brace (see above). It generally means you forgot to put a return type (e.g. void, int), but it shouldn't trigger this error if you're writing a constructor.

GradeCalculator_2.java:143: illegal start of expression
3. Fix the brace. You shouldn't get this error.

GradeCalculator_2.java:145: ')' expected
4. You're missing a closing parentheses at line 145.

GradeCalculator_2.java:151: reached end of file while parsing
5. Fix the brace. This error should also go away.

Ok i did that and it threw 86 errors i listed the first few errors and all the rest are class, interface, or enum expected

sorry heres the errors:

 ----jGRASP exec: javac -g GradeCalculator_2.java

GradeCalculator_2.java:22: illegal start of type
              return (test1 + test2 + test3) / 3; 
              ^
GradeCalculator_2.java:22: <identifier> expected
              return (test1 + test2 + test3) / 3; 
                           ^
GradeCalculator_2.java:22: ';' expected
              return (test1 + test2 + test3) / 3; 
                             ^
GradeCalculator_2.java:22: illegal start of type
              return (test1 + test2 + test3) / 3; 
                                    ^
GradeCalculator_2.java:22: ';' expected
              return (test1 + test2 + test3) / 3; 
                                           ^
GradeCalculator_2.java:26: class, interface, or enum expected
    {
    ^
GradeCalculator_2.java:28: class, interface, or enum expected
      private double score, tst1, tst2, tst3; 
              ^
GradeCalculator_2.java:29: class, interface, or enum expected
      private double classAvg, stAvg, totalScore;

At line 22, you have an opening brace that does nothing. Remove that and it should remove alot of those errors. You have:

#
double getAverage() [b][U]{[/U] remove this[/b] // compute average test grade
return (test1 + test2 + test3) / 3;

Then obviously replace the { with a ;

Yeah i did that when I added the closing }

import java.util.*; 
   import java.io.*;
   import javax.swing.*; 
   import java.awt.*; 
   import java.awt.event.*; 
  

   public class GradeCalculator2 extends JFrame 
   { 
      private static final int WIDTH = 550; 
      private static final int HEIGHT = 430; 
      int GradeCalc; 
      int student; 
      private StudentList[] sList = new StudentList[20]; 
   
  		 public class StudentList 
   { 
      String name; // Student's name. 
      double test1, test2, test3; // Grades on three tests. 
      double getAverage();  // compute average test grade 
   }
		
	      return (test1 + test2 + test3) / 3; 
   
 			
   // instance variables 
	
      private int noOfStudents; 
      private double score, tst1, tst2, tst3; 
      private double classAvg, stAvg, totalScore; 
      private int displayedStudentIndex = 0; 
      private char ltrGrade; 
      private String stName; 
   
   // This area is for the GUI components 
   // Each item that will be displayed will 
   // have a label and a textfield, (L) and (TF), 
   // respectively. 
      private JLabel stNameL, tst1L, tst2L, tst3L, 
      classAvgL, stAvgL, headingL; 
      private JTextField stNameTF, tst1TF, tst2TF, tst3TF; 
      private JTextArea classAvgTA, stAvgTA; 
      private JButton exitB, nextB, prevB, calcGrade; 
   
      private ButtonHandler bHandler; 
   
	   public GradeCalculator2() 
      { 
         setTitle("Grade Calculator"); // set's the title 
         setSize(WIDTH, HEIGHT); // set the window size 
         Container pane = getContentPane(); // get the container 
         pane.setLayout(null); // set the container's layout to null 
      
         bHandler = new ButtonHandler(); // instantiate the button event handler 
      
      // instantiate the labels 
         headingL = new JLabel("STUDENT RECORD"); 
         stNameL = new JLabel("Student Name", SwingConstants.RIGHT); 
         tst1L = new JLabel("Test 1", SwingConstants.LEFT); 
         tst2L = new JLabel("Test 2", SwingConstants.LEFT); 
         tst3L = new JLabel("Test 3", SwingConstants.LEFT); 
         stAvgL = new JLabel("Student Average " 
            + "\n" + "Class Average"); 
				
      //instantiate the text fields 
         stNameTF = new JTextField(65); 
         tst1TF = new JTextField(10); 
         tst2TF = new JTextField(10); 
         tst3TF = new JTextField(10); 
      
      // instantiate the text area 
         classAvgTA = new JTextArea(6, 20); 
         classAvgTA.setAutoscrolls(true); 
      
      // instantiate the buttons and register the listener 
         exitB = new JButton("Exit"); 
         exitB.addActionListener(bHandler); 
      
         nextB = new JButton("Next"); 
         nextB.addActionListener(bHandler); 
      
         prevB = new JButton("Previous"); 
         prevB.addActionListener(bHandler); 
      
         calcGrade = new JButton("Calc Grade"); 
         calcGrade.addActionListener(bHandler); 
      
      // set the size of the labels, text fields, and buttons 
         headingL.setSize(200, 30); 
         stNameL.setSize(100, 30); 
         stNameTF.setSize(100, 30); 
         tst1L.setSize(100, 30); 
         tst1TF.setSize(100, 30); 
         tst2L.setSize(120, 30); 
         tst2TF.setSize(100, 30); 
         tst3L.setSize(100, 30); 
         tst3TF.setSize(100, 30); 
         classAvgTA.setSize(370, 120); 
         calcGrade.setSize(100, 30); 
         prevB.setSize(100, 30);
         nextB.setSize(100, 30); 
         exitB.setSize(100, 30); 
      
      //set the location of the labels, text fields, 
      //and buttons 
         headingL.setLocation(220, 10); 
         stNameL.setLocation(20, 50); 
         stNameTF.setLocation(120, 50); 
         tst1L.setLocation(20, 100); 
         tst1TF.setLocation(120, 100); 
         tst2L.setLocation(300, 50); 
         tst2TF.setLocation(420, 50); 
         tst3L.setLocation(300, 100); 
         tst3TF.setLocation(420, 100); 
         classAvgTA.setLocation(70, 230); 
         prevB.setLocation(120, 370); 
         exitB.setLocation(220, 370); 
         nextB.setLocation(320, 370); 
         calcGrade.setLocation(420, 370); 
      
      //add the labels, text fields, and buttons to the pane 
         pane.add(headingL); 
         pane.add(stNameL); 
         pane.add(stNameTF); 
         pane.add(tst1L); 
         pane.add(tst1TF); 
         pane.add(tst2L); 
         pane.add(tst2TF); 
         pane.add(tst3L); 
         pane.add(classAvgTA); 
         pane.add(calcGrade); 
         pane.add(prevB); 
         pane.add(exitB); 
         pane.add(nextB); 
      
         setVisible(true); //show the window 
         setDefaultCloseOperation(EXIT_ON_CLOSE); 
         System.exit(0); 
			
			/**
				This program creates an instance of the GradeCalculator2 class, 
				which displays a window on the screen.
			*/
			
			public class GradeCalculator2
			{
				public static void main(String[] args)
				{
					new GradeCalculator2();
				
      } 
   }
}
}

ERRORS NOW


----jGRASP exec: javac -g GradeCalculator_2.java

GradeCalculator_2.java:23: illegal start of type
return (test1 + test2 + test3) / 3;
^
GradeCalculator_2.java:23: <identifier> expected
return (test1 + test2 + test3) / 3;
^
GradeCalculator_2.java:23: ';' expected
return (test1 + test2 + test3) / 3;
^
GradeCalculator_2.java:23: illegal start of type
return (test1 + test2 + test3) / 3;
^
GradeCalculator_2.java:23: ';' expected
return (test1 + test2 + test3) / 3;
^
GradeCalculator_2.java:145: illegal start of expression
public class GradeCalculator2
^
6 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

What on earth are you doing?

public class GradeCalculator2 extends JFrame 
   { 
      private static final int WIDTH = 550; 
      private static final int HEIGHT = 430; 
      int GradeCalc; 
      int student; 
      private StudentList[] sList = new StudentList[20]; 
   
           public class StudentList 
   { 
      String name; // Student's name. 
      double test1, test2, test3; // Grades on three tests. 
      double getAverage();  // compute average test grade 
   }
        
          [U]return (test1 + test2 + test3) / 3;[/U]//These variables are in that student list class above

Plus, you have 2 classes called GradeCalculator2.
Your code is extremely confusing and seems to have alot of useless variables etc. Just place the underline code back into the braces right above. This should fix the first 5 errors.

Rename one of your classes, you have 2 classes with exactly the same name.

Im lost,

The second GradeCaltculator2 I thought i was creating an Instance. Should this not be in the program.

/**				
          This program creates an instance of the GradeCalculator2 class, 		 which displays a window on the screen.
       */ 			

public class GradeCalculator2			
     {
        public static void main(String[] args)				
        {	
            new GradeCalculator2();
        }
     }

What does creating an instance mean?
answer: Nothing, it doesn't make sense.

What you are doing there, is creating a class, not an instance of a class.
An object is an instance of a class. To create an object you use the following syntax,
Object variable = new Object();

I don't know where you are learning your java but you need to sort out these basics asap, otherwise you will run into problems like this all the time and your motivation will go out the window!


TIP: To make your code easier to maintain and read, create separate files for each class, until you fully understand what you are doing. You should always keep your main class separate.

class MainClass
{
public static void main(String[] args)
{ 
}
}

The class with the public static void main(String[] args) is the main class.

My motivation is out the window. I am taking an web class because it was not offered in school. Do you have a compiler or are you just looking at the code. I am using JGrasp.

I was just looking at the code until I ran out of ideas and you still had problems. I use NetbeansIDE which is very good for debugging programs.
Can you link me to where you are learning it?

By the way, if you follow what I said on the previous post it should compile and work.(As long as the rest of your logic is sound)

I only need two classes one for students and one for the student array:

Create a program to enter grades and calculate averages and letter grades.

  1. Need a class which will contain:
    a. Student Name
    b. Student Grades (an array of 3 grades)
    c. A constructor that clears the student data (use -1 for unset grades)
    d. Accessors (get functions) for each of the above, average, and letter grade
    e. Mutators (set functions) for items a, b, c
    f. Note that the accessor and mutator for Student grades has to have an argument for the grade index.
  2. Need another class which will contain:
    a. An Array of Students (1 above)
    b. A count of number of students in use
    c. Constructor that reads data from a text file and sets up the students
  3. You need to create a graphical user interface that allows you to:
    a. Read data from file
    b. Add new students
    c. Process existing students
    d. Add test grades
    e. Based on a radio button setting display either the average or the letter grade
    f. Save modified data to file
  4. A possible graphical look is as follows:
  5. Add comments and use proper indentation.

NOTE: You need actionCommand to handle saving a file and you cannot modify actionCommand with a throws IOException clause. The way to solve this is as follows:
Assuming that you have a method called saveData that saves the data, change its call as follows:

try 
{   saveData();
} catch (IOException x)
{
}

The function saveData, your function to load data, and the constructor will need a throws IOException clause.

Additional Information:

  • An accessor is needed to save the test grades, which should always be numbers.
  • I would like that system to accept a student with no grades, then later add one or more grades, and when all grades are entered, calculate the final average or grade.
  • The way to handle changes in data using a text file is:
  • Open the data file for reading
  • load all the data into an array of students from the file,
  • Close the file
  • Make all modifications to the data in the array of students.
  • When saving, open the datafile for writing
  • Loop through the students and save the data to the datafile.
  • Close the data file.

Well it seems as though this project will be late. My eyes are crossed and my brain is scrambled. I will have to walk away and go to sleep. Will you guys(Norm and Akill) be online in the morning to help me?

:) Maybe not in the morning, but tomorrow yes!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.