Student Java Project

A Java Project displaying test scores and other information

Page 1 of 1

14 Replies - 10957 Views - Last Post: 13 March 2010 - 10:33 PM Rate Topic: -----

#1 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Student Java Project

Posted 13 March 2010 - 07:18 PM

I am having difficulty in getting my Java Project to work correctly. I currently have 28 errors, posted below. My code is posted after the errors. I can't figure how to resolve these errors and get my program running correctly. Any help/advice would be greatly appreciated, thank you.

My errors:
Student.java:11: illegal start of expression
   private String name;             //Student name
   ^
Student.java:12: illegal start of expression
   private int test1;               //Score on test 1
   ^
Student.java:13: illegal start of expression
   private int test2;               //Score on test 2
   ^
Student.java:14: illegal start of expression
   private int test3;               //Score on test 3
   ^
Student.java:18: illegal start of expression
   public Student(){
   ^
Student.java:18: ';' expected
   public Student(){
                   ^
Student.java:29: illegal start of expression
   public void setName (String nm){
   ^
Student.java:29: illegal start of expression
   public void setName (String nm){
          ^
Student.java:29: ';' expected
   public void setName (String nm){
                      ^
Student.java:29: ';' expected
   public void setName (String nm){
                                 ^
Student.java:34: illegal start of expression
   public String getName (){
   ^
Student.java:34: ';' expected
   public String getName (){
                        ^
Student.java:39: illegal start of expression
   public void setScore (int i, int score){
   ^
Student.java:39: illegal start of expression
   public void setScore (int i, int score){
          ^
Student.java:39: ';' expected
   public void setScore (int i, int score){
                       ^
Student.java:39: <identifier> expected
   public void setScore (int i, int score){
                               ^
Student.java:39: not a statement
   public void setScore (int i, int score){
                                    ^
Student.java:39: ';' expected
   public void setScore (int i, int score){
                                         ^
Student.java:46: illegal start of expression
   public int getScore (int i){
   ^
Student.java:46: ';' expected
   public int getScore (int i){
                      ^
Student.java:46: ';' expected
   public int getScore (int i){
                             ^
Student.java:53: illegal start of expression
   public int getAverage(){
   ^
Student.java:53: ';' expected
   public int getAverage(){
                        ^
Student.java:60: illegal start of expression
   public int getHighScore(){
   ^
Student.java:60: ';' expected
   public int getHighScore(){
                          ^
Student.java:69: illegal start of expression
   public String toString(){
   ^
Student.java:69: ';' expected
   public String toString(){
                         ^
Student.java:79: reached end of file while parsing
}
 ^
28 errors



/* Student.java
Manage a student's name and three test scores.
*/

public class Student 
{
public static void main ( String [] args);
{
   //Instance variables
   //Each student object has a name and three test scores
   private String name;             //Student name
   private int test1;               //Score on test 1
   private int test2;               //Score on test 2
   private int test3;               //Score on test 3

   //Constructor method

   public Student(){
   //Initialize a new student's name to the empty string and the test
   //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
   }
    
   //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  
}



Is This A Good Question/Topic? 0
  • +

Replies To: Student Java Project

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,517
  • Joined: 23-August 08

Re: Student Java Project

Posted 13 March 2010 - 07:24 PM

public static void main ( String [] args); // <-- That semicolon's out of place, isn't it?

Was This Post Helpful? 2
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 07:28 PM

View PostJackOfAllTrades, on 13 March 2010 - 05:24 PM, said:

public static void main ( String [] args); // <-- That semicolon's out of place, isn't it?

JackOfAllTrades hit it right on the money. Fix that and it should take care of those errors.
Was This Post Helpful? 0
  • +
  • -

#4 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Re: Student Java Project

Posted 13 March 2010 - 07:37 PM

I tried removing the semicolon there, but I still get the same 28 errors. It must be something really obvious. I must have missed something...
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 07:47 PM

You can't have methods in the main method. All methods need to be made outside of the main method and CALLED inside by use of a Student object.

Like this:
public class Student
{
    public Student()
    {
        // stuff
    }
    
    public <return type> methodName(<parameters>)
    {
        // stuff
    }
    
    public static void main(String[] args)
    {
        Student stu = new Student();
        stu.methodName();
    }
}


This post has been edited by zim1985: 13 March 2010 - 07:51 PM

Was This Post Helpful? 1
  • +
  • -

#6 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Re: Student Java Project

Posted 13 March 2010 - 08:10 PM

Now I have just 1 error after adding that into the code:

Student.java:10: cannot find symbol
symbol : method methodName()
location: class Student
stu.methodName();
Was This Post Helpful? 0
  • +
  • -

#7 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 08:16 PM

View PostGAMERJET, on 13 March 2010 - 06:10 PM, said:

Now I have just 1 error after adding that into the code:

Student.java:10: cannot find symbol
symbol : method methodName()
location: class Student
stu.methodName();

I meant that as an example.

You need to put all your code in, methods, constructors, variables used in the methods, all before the main method. Use my example as a guide for how to organize your code.

Could you post your current code?

This post has been edited by zim1985: 13 March 2010 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Re: Student Java Project

Posted 13 March 2010 - 08:33 PM

This is what I tried initially:

/* Student.java
Manage a student's name and three test scores.
*/

public class Student 
{
public static void main ( String [] args)
{
        Student stu = new Student();
        stu.methodName();
}

   //Instance variables
   //Each student object has a name and three test scores
   private String name;             //Student name
   private int test1;               //Score on test 1
   private int test2;               //Score on test 2
   private int test3;               //Score on test 3

   //Constructor method

   public Student(){
   //Initialize a new student's name to the empty string and the test
   //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
   }
    
   //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  
}



This is a reorganized rewrite, I tried to follow your example:
public class Student
{
    public Student()
    {
	    //Instance variables
       //Each student object has a name and three test scores
       private String name;             //Student name
       private int test1;               //Score on test 1
       private int test2;               //Score on test 2
       private int test3;               //Score on test 3
	
      //Initialize a new student's name to the empty string and the test
      //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
    }
    
    //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  
}
    public static void main(String[] args)
    {
        Student stu = new Student();
        stu.methodName();
    }




Which only gives 7 errors in the second rewrite.
Was This Post Helpful? 0
  • +
  • -

#9 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 08:40 PM

View PostGAMERJET, on 13 March 2010 - 06:33 PM, said:

This is what I tried initially:

/* Student.java
Manage a student's name and three test scores.
*/

public class Student 
{
public static void main ( String [] args)
{
        Student stu = new Student();
        stu.methodName();
}

   //Instance variables
   //Each student object has a name and three test scores
   private String name;             //Student name
   private int test1;               //Score on test 1
   private int test2;               //Score on test 2
   private int test3;               //Score on test 3

   //Constructor method

   public Student(){
   //Initialize a new student's name to the empty string and the test
   //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
   }
    
   //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  
}



This is a reorganized rewrite, I tried to follow your example:
public class Student
{
    public Student()
    {
	    //Instance variables
       //Each student object has a name and three test scores
       private String name;             //Student name
       private int test1;               //Score on test 1
       private int test2;               //Score on test 2
       private int test3;               //Score on test 3
	
      //Initialize a new student's name to the empty string and the test
      //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
    }
    
    //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  
}
    public static void main(String[] args)
    {
        Student stu = new Student();
        stu.methodName();
    }




Which only gives 7 errors in the second rewrite.

Ok, so you need to call the methods in the main method like how I showed you, except you need to use your own methods. Also, you have an extra bracket before the main. Make no mistake, the public static void main is a method, it just calls all the other methods and gets the program running.

In the main you will implement your methods to make the output be what you want it to be. You will call them using the "stu" Student object.

What are the errors?
Was This Post Helpful? 0
  • +
  • -

#10 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Re: Student Java Project

Posted 13 March 2010 - 08:46 PM

The errors for the rewrite are:

Student.java:7: illegal start of expression
private String name; //Student name
^
Student.java:8: illegal start of expression
private int test1; //Score on test 1
^
Student.java:9: illegal start of expression
private int test2; //Score on test 2
^
Student.java:10: illegal start of expression
private int test3; //Score on test 3
^
Student.java:73: class, interface, or enum expected
public static void main(String[] args)
^
Student.java:76: class, interface, or enum expected
stu.methodName();
^
Student.java:77: class, interface, or enum expected
}
^
7 errors
Was This Post Helpful? 0
  • +
  • -

#11 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 08:52 PM

You can't declare private variables in constructors. Declare them outside and then initialize them like you have in the constructor.

Then you have an extra bracket ("}") right before the public static void main. You need to move it after the main method.

I fixed it for you:
public class Student
{
       //Instance variable
      //Each student object has a name and three test scores
       private String name;             //Student name
       private int test1;               //Score on test 1
       private int test2;               //Score on test 2
       private int test3;               //Score on test 3

    public Student()
    {
            //Instance variable
        
      //Initialize a new student's name to the empty string and the test
      //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
    }
    
    //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  

    public static void main(String[] args)
    {
        Student stu = new Student();
        stu.methodName();
    }
}


This post has been edited by zim1985: 13 March 2010 - 08:53 PM

Was This Post Helpful? 0
  • +
  • -

#12 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Re: Student Java Project

Posted 13 March 2010 - 09:04 PM

I removed the private from the variables and I think I followed correctly.

I only have 1 error:

Student.java:78: cannot find symbol
symbol : method methodName()
location: class Student
stu.methodName();
^
1 error

My code:
public class Student
{
       //Instance variable
      //Each student object has a name and three test scores
        String name;             //Student name
        int test1;               //Score on test 1
        int test2;               //Score on test 2
        int test3;               //Score on test 3

    public Student()
    {
            //Instance variable
        
      //Initialize a new student's name to the empty string and the test
      //scores to zero.
      name = "";
      test1 = 0;
      test2 = 0;
      test3 = 0;
    }
    
    //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
   //Get a student's name
      return name;
   }
  
   public void setScore (int i, int score){
   //Set test i to score
      if      (i == 1) test1 = score;
      else if (i == 2) test2 = score;
      else             test3 = score;
   }

   public int getScore (int i){
   //Retrieve score i
      if      (i == 1) return test1;
      else if (i == 2) return test2;
      else             return test3;
   }
   
   public int getAverage(){
   //Compute and return the average
      int average;
      average = (int) Math.round((test1 + test2 + test3) / 3.0);
      return average;
   }
    
   public int getHighScore(){
   //Determine and return the highest score
      int highScore;
      highScore = test1;
      if (test2 > highScore) highScore = test2;
      if (test3 > highScore) highScore = test3;
      return highScore;
   }
    
   public String toString(){
   //Construct and return a string representation of the student
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + test1 + "\n" +
            "Test 2:  " + test2 + "\n" + 
            "Test 3:  " + test3 + "\n" +
            "Average: " + getAverage();
      return str;
   }  

    public static void main(String[] args)
    {
        Student stu = new Student();
        stu.methodName();
    }
}



It seems to be something with this line:
        stu.methodName();

Was This Post Helpful? 0
  • +
  • -

#13 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 09:10 PM

Highlight and delete my friend.

Again, I used that as an example of how to call a method in the main method. Not for something for you to actually implement.
Was This Post Helpful? 0
  • +
  • -

#14 GAMERJET  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-March 10

Re: Student Java Project

Posted 13 March 2010 - 09:21 PM

It seems to compile now with no errors, but all it does is Hit any key to start and then Hit any key to continue and then it closes. I attached a screenshot.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#15 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Student Java Project

Posted 13 March 2010 - 10:33 PM

In your main method you have to make the magic happen. You have to do the processing there and then use your methods to get things done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1