Grade Calculator Problems

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4

57 Replies - 1976 Views - Last Post: 07 June 2010 - 03:14 PM Rate Topic: -----

#46 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 11:44 AM

now what i get another negative for??????????????????????
Was This Post Helpful? 0
  • +
  • -

#47 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 11:46 AM

What do you mean by 'How?'. Have you made the changes the others have told you to do?
Was This Post Helpful? 0
  • +
  • -

#48 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 11:53 AM

he said: access-modifier type name = value;

which i did: private StudentList[] = [20]

I got the same error:

<identifier> expected
private StudentList[] =[20];
^

i tried to name it:

private StudentList[] sList = [20]

I got:

GradeCalculator.java:13: illegal start of expression
private StudentList[] sList = [20];
^
GradeCalculator.java:13: illegal start of type
private StudentList[] sList = [20];
^
GradeCalculator.java:13: <identifier> expected
private StudentList[] sList = [20];
^
3 errors
Was This Post Helpful? 0
  • +
  • -

#49 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,651
  • Joined: 27-December 08

Re: Grade Calculator Problems

Posted 07 June 2010 - 11:53 AM

I also showed you an example of what I meant. You have access modifier and type there, but no name and [20] is not a value. However, new StudentList[20] is a value since you are instantiating an array.
Was This Post Helpful? 0
  • +
  • -

#50 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 11:57 AM

when i do that it throws 13 errors!

GradeCalculator.java:13: cannot find symbol
symbol : class StudentList
location: class GradeCalculator
private StudentList[] sList = new StudentList[20];
^
GradeCalculator.java:13: cannot find symbol
symbol : class StudentList
location: class GradeCalculator
private StudentList[] sList = new StudentList[20];
^
GradeCalculator.java:143: int cannot be dereferenced
gradeCalc.StudentList[s] = new Student();
^
GradeCalculator.java:143: cannot find symbol
symbol : class Student
location: class GradeCalculator
gradeCalc.StudentList[s] = new Student();
^
GradeCalculator.java:145: int cannot be dereferenced
gradeCalc.noOfStudents =
^
GradeCalculator.java:147: int cannot be dereferenced
gradeCalc.score =
^
GradeCalculator.java:150: int cannot be dereferenced
gradeCalc.getStudentData(inFile);
^
GradeCalculator.java:151: int cannot be dereferenced
gradeCalc.displayGradeAverage(0);
^
GradeCalculator.java:187: cannot find symbol
symbol : variable studentList
location: class GradeCalculator
boolean classAvg = studentList[(int) (totalScore / noOfStudents)].getClassAverage();
^
GradeCalculator.java:188: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stNameTF.setText(StudentList[stName].getFirstName() + " "
^
GradeCalculator.java:189: cannot find symbol
symbol : variable studentList
location: class GradeCalculator
+ studentList[stName].getLastName());
^
GradeCalculator.java:190: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stAvgTA.setText(""+StudentList[(int) (totalScore / 5.0)].getStudentAverage());
^
GradeCalculator.java:191: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
^
13 errors
Was This Post Helpful? 0
  • +
  • -

#51 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Grade Calculator Problems

Posted 07 June 2010 - 01:08 PM

My god man, I understand you are new and all but these concepts are the foundation to the Java Programming Language. They should have been said the 2nd day of programming (assuming the first day was the hello world program :) )

Anyways, I will explain it to you in the most expanded, in depth way possible.

So all these data types are called variables. Variables...You know, like x = 5 or y = x + 2. Things you learn back in algebra. A variable is a name (like x or like studentGrades) that represents something that has a possibility of being changed.

For example: If x + 5 = y and y = 5 then x = 0 but if y = 6 then x = 1. The variables change based on...input. But for ALL variables, they are associated with a name.

In Java there are variables as well. Java wanted to define these variables as different things. For example, a variable could be a number...like an int. It could be a word...like a string. OR it could be an object, like a studentList. To declare a variable in Java, it looks something like this:
(var-type) Name;



Now (var-type) is itself a variable! (lol not trying to confuse you) So if you want a number, your var-type is an int... and your Name is whatever you want it to be...so:
//based on (var-type) Name;
int someNumber;



Now the name can usually be ANYTHING (except certain keywords like "this" or others, there is a list somewhere)

The problem is that java can't read 2 variables with the same name. So if you do this:
int someNumber = 5;
int someNumber = 3;



That isn't going to work because you are declaring 2 of the same "Name" to 2 different things! That is like naming all your children in real life Bob! Not only is it not original, but when you say "Hey Bob, can you get the mail?", you are addressing only one Bob but all your children scramble to get the mail! Very ineffective so Java makes it impossible to "name all your children Bob".

Now there is a difference between declaring 2 variables and changing a variable after it is declared. For example:
int someNumber = 5;
someNumber = 6;



What you did here was you declared a variable, and then you changed it...because as I said before: "Variables can be changed". That is like creating a variable called cookiesInTheCookieJar and assigning it to 5. Later, you add a cookie to the "cookiesInTheCookieJar" variable and now you have 6.
------------------------------------------------------------------
Done with variable tutorial

Now on to customized objects like your Student and your StudentList.

So let's start off by making a Student Variable.
Student Bob; 
//Yo, it's one of your kids!


Now you have your student, but all the data fields you have in the student object are not initialized. So Java has a "special way" of initializing objects using the "new" keyword and then using the constructor you built in the Student class. So let's say in the Student class you created a constructor like this:
public Student(int gradeOnFirstExam){
//code to do something with the grade on first exam
}


And you want to run that. Well Java has made it easy for you to do that.
Student Bob = new Student(100); 
//Your kid got a 100 on the first exam, be proud


So you used the new keyword which made a NEW object of a Student with a grade of 100. In that same line, it assigned that variable bob, to that student with a grade of 100. So now you can say that bob(the variable) IS the student with a grade of 100.

Just "Student Bob;" is a variable with nothing on the inside.
It is like the example with the cookie jar variable. If you never say "cookiesInTheCookieJar = 5" then you don't know (and specifically Java doesn't know) how many cookies there are in the cookie jar.

I could go more in depth on this and talk about the difference between primitives like ints and references like Student but I will leave it at this basic level for now. You only need to know so much at this point, right? :)
----------------------------------------------------------------
Done with explaining the new keyword.


Now onto your problem:
   private StudentList[] sList = new StudentList [20]; 
                private static final int MAX_NUMBER_OF_STUDENTS = 20; 
      private StudentList[] sList = new StudentList[MAX_NUMBER_OF_STUDENTS];



What you did here was you declared 2 variables with the same name. As I said before, this is like naming 2 of your children Bob, it won't work in java. So your next logical step is to get rid of the declaration on the second sList so it looks something like this:
   private StudentList[] sList = new StudentList [20]; 
                private static final int MAX_NUMBER_OF_STUDENTS = 20; 
      sList = new StudentList[MAX_NUMBER_OF_STUDENTS];


That code is actually runnable without error. There is still a little thing that should be troubling you. You assigned a value to your variable already, yet you do it again on the next line! That is like saying:
Student Bob = new Student(100);
Bob = new Student(100);


What you are doing is assigning Bob to a new student with the same exact value. Why? The answer is: there is no reason.. So take it out.

Your final code should look something like this:
   private StudentList[] sList = new StudentList [20]; 



Now you might be wondering about what private does. That just means that only things inside the class can access this variable. you add it on in the declaration, nothing real fancy about it, if you want to learn more about it I would suggest looking up Encapsulation.

Hope I helped with my off-kilter examples.

This post has been edited by Ember: 07 June 2010 - 01:16 PM

Was This Post Helpful? 4
  • +
  • -

#52 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 01:39 PM

have you tried running that code with the:

private StudentList[] sList = new StudentList[20];

because it want compile
Was This Post Helpful? 0
  • +
  • -

#53 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Grade Calculator Problems

Posted 07 June 2010 - 01:42 PM

View Postblknmld69, on 07 June 2010 - 12:39 PM, said:

have you tried running that code with the:

private StudentList[] sList = new StudentList[20];

because it want compile

Well...We can't compile anything. You didn't supply us with the StudentList class so it will always come up as an error saying that the class doesn't exist for us.
Was This Post Helpful? 0
  • +
  • -

#54 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 01:52 PM

the class list is created through the input
Was This Post Helpful? 0
  • +
  • -

#55 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: Grade Calculator Problems

Posted 07 June 2010 - 02:07 PM

View Postblknmld69, on 07 June 2010 - 08:52 PM, said:

the class list is created through the input


Have you made a class with the name StudentList ?

Else you can not use StudentList as type in
private StudentList[] sList = new StudentList[20];

You make an array called sList and in that array you store objects of the class StudentList.

You need to have made the class 'StudentList' and you need to import it like you do with the gui classes.
Was This Post Helpful? 0
  • +
  • -

#56 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 02:18 PM

no i havent, classmate just brought that to my attention too!
Was This Post Helpful? 0
  • +
  • -

#57 blknmld69  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 102
  • Joined: 28-November 09

Re: Grade Calculator Problems

Posted 07 June 2010 - 02:44 PM

Created StudentList:

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


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;
}
}


public class GradeCalculator extends JFrame
{
private static final int WIDTH = 550;
private static final int HEIGHT = 430;
int gradeCalc;
int student;
private StudentList[] sList = new StudentList[20];


// 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 GradeCalculator()
{
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);
}


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

Scanner inFile =
new Scanner(new FileReader("AcademicGrades.txt"));
{
for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++)
gradeCalc.StudentList[s] = new Student();

gradeCalc.noOfStudents =
inFile.nextInt(); // get the number of Students
gradeCalc.score =
inFile.nextDouble(); // get the Student's scores

gradeCalc.getStudentData(inFile);
gradeCalc.displayGradeAverage(0);
}

// get the Student data from file

public void getStudentData(Scanner inFile)
{
System.out.println("Grade Calculator is getting information...");
System.out.println("One Moment Please");
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Previous"))
if (displayedStudentIndex > 0)
displayGradeAverage(displayedStudentIndex - 1);
else
displayGradeAverage(displayedStudentIndex);
else if (e.getActionCommand().equals("Next"))
if (displayedStudentIndex + 1 < noOfStudents)
displayGradeAverage(displayedStudentIndex + 1);
else
displayGradeAverage(displayedStudentIndex);
else if (e.getActionCommand().equals("Calc Grade"))
displayGradeAverage(0);
else
System.exit(0);
}
}

public void displayGradeAverage(int stName)
{
displayedStudentIndex = stName;
String strName = "";
boolean classAvg = StudentList[(int) (totalScore / noOfStudents)].getClassAverage();
stNameTF.setText(StudentList[stName].getFirstName() + " "
+ StudentList[stName].getLastName());
stAvgTA.setText(""+StudentList[(int) (totalScore / 3.0)].getStudentAverage());
classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
}

Errors

StudentList.java:18: class GradeCalculator is public, should be declared in a file named GradeCalculator.java
public class GradeCalculator extends JFrame
^
StudentList.java:150: cannot find symbol
symbol : variable MAX_NUMBER_OF_STUDENTS
location: class GradeCalculator
for (int s = 0; s < MAX_NUMBER_OF_STUDENTS; s++)
^
StudentList.java:151: int cannot be dereferenced
gradeCalc.StudentList[s] = new Student();
^
StudentList.java:151: cannot find symbol
symbol : class Student
location: class GradeCalculator
gradeCalc.StudentList[s] = new Student();
^
StudentList.java:153: int cannot be dereferenced
gradeCalc.noOfStudents =
^
StudentList.java:155: int cannot be dereferenced
gradeCalc.score =
^
StudentList.java:158: int cannot be dereferenced
gradeCalc.getStudentData(inFile);
^
StudentList.java:159: int cannot be dereferenced
gradeCalc.displayGradeAverage(0);
^
StudentList.java:194: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
boolean classAvg = StudentList[(int) (totalScore / noOfStudents)].getClassAverage();
^
StudentList.java:195: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stNameTF.setText(StudentList[stName].getFirstName() + " "
^
StudentList.java:196: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
+ StudentList[stName].getLastName());
^
StudentList.java:197: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
stAvgTA.setText(""+StudentList[(int) (totalScore / 3.0)].getStudentAverage());
^
StudentList.java:198: cannot find symbol
symbol : variable StudentList
location: class GradeCalculator
classAvgTA.setText(""+StudentList[(int) (totalScore / noOfStudents)].getClassAverage()); }
^
13 errors
Was This Post Helpful? 0
  • +
  • -

#58 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,651
  • Joined: 27-December 08

Re: Grade Calculator Problems

Posted 07 June 2010 - 03:14 PM

You've been a member since November and you have ~60 posts. You should know the rules by now. As this isn't your first violation, I'm closing this thread. Feel free to open up a new thread once you re-read the Rules and are willing to follow them.
Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • « First
  • 2
  • 3
  • 4