2

This is a homework assignment and is also my first Java program. I wrote a StudentAverage class, now I want to test the class methods, but when I write my tester program, the IDE is telling me that I cannot declare my main static. I am using Eclipse as my IDE.

Since this is a homework assignment and I am still learning Java, I would also appreciate some guidance as to what I am doing wrong.

Here is my code:

/**
 *Program:      StudentAverage, Calculate the student average quizzes taken
 * @author:     Jose Mazorra
 * Date:        July 11, 2013
 * Class:       CIS406
 */

/**
   A student who is taking quizzes.
*/
public class StudentAverage
{ 
    //Instances variables
    private String name;
    private double quizScores;
    private double numOfQuizzesTaken;

   /**
      Constructs a student with a given name.
      @param n the name
   */
   public StudentAverage(String stuName)
   {  
     name = (stuName);
   }

   /**
      Gets the name of this student.
      @return the name
   */
   public String getName()
   {  
     return name;
   }

   /**
      Adds a quiz score.
      @param score the score to add
   */
   public void addQuiz(int score)
   {  
     numOfQuizzesTaken++;
     quizScores = quizScores + score;
   }


   /**
      Gets the sum of all quiz scores.
      @return the total score
   */
   public double getTotalScore()
   {  
     return quizScores;
   }

/**Returns the average of all quiz taken
 * by the student
 * @return average score
 */

public double getAverageScore(){

double avgScore;

avgScore = (quizScores / numOfQuizzesTaken);

return avgScore;

}

public class StudentAverageTester{

    public static void main(String[] args){

        StudentAverage student = new StudentAverage()

        student.name("Jose");
        student.numOfQuizzesTaken(10);
        student.quizScores(400);

        double avg = student.avgScore();

        System.out.println(name);
        System.out.println(avg);
        System.out.println("Expected 40");



    }


}

}
3
  • 1
    Are both public classes in the same file? Commented Jul 12, 2013 at 15:12
  • 6
    Either remove the public keyword in the second class or move it to its own seperate file Commented Jul 12, 2013 at 15:12
  • 2
    Consider reading up on jUnit and using that to test your code. Commented Jul 12, 2013 at 15:13

4 Answers 4

2

You have created StudentAverageTester as a non-static inner class of StudentAverage. Non-static inner classes are not permitted to have static declarations, that is why you are seeing the compilation error (see JLS 8.1.3).

It would really be better if you extracted the StudentAverageTester class into it's own StudentAverageTester.java file.

Sign up to request clarification or add additional context in comments.

Comments

2

You have a few mistakes in your code. First thing, do what everyone sais and put StudentAverageTester in its own file. Currently, you are declaring inside the StudentAverage class. You also did not declare a no arg constructor in your StudentAverage class. In StudentAverageTester you have

StudentAverage student = new StudentAverage()

but it should be

StudentAverage student = new StudentAverage("Some name")

You also forgot the semi colon.

UPDATE

Your name property is private. You can't access it like that in StudentAverageTester. You need to declare a setter method as in setName(String name).

Consider reviewing your StudentAverageTester class. You are calling methods you did not define and accessing private members directly. You can't do that. Use setters and getters.

8 Comments

In fact, if you add static marker to StudentAverageTester you will see the other compiler error messages and after fixing them the program will compile and run as expected.
@LuiggiMendoza Oh yeah, there's a lot of mistakes. I'll fix it for him. I've been there.
IMO fixing OP's errors is part of his homework, so it would be better to just help him to solve the problem stated in question instead of solving his/her assignment. The main problem is having a static method in an inner class (as Jonathan's answer points).
@LuiggiMendoza Yep, you're right. Learning pains are always a good thing.
These are more like labor pains, or in my case kidney stone pains. :-)
|
1

You are creating one class another class.I don't think you intend to do that.Also if both these classes are in one file only one class can be public.Better move each class to its separate file

Update:

So the Issue here is that you are creating Inner class and in Java inner classes cannot have static methods.Because an inner class is implicitly associated with an instance of its outer class, it cannot define any static methods itself. Since a static nested class cannot refer directly to instance variables or methods defined in its enclosing class, it can use them only through an object reference, it's safe to declare static methods in a static nested class.

4 Comments

You are creating one class another class I would suggest to rethink what you're going to write, this sentence doesn't have too much sense. And you can have several classes in a single java file, but just one of them can be public top class (if that's what you were trying to say).
@LuiggiMendoza I said one class another class to simply nested class terminology.Considering the fact that asker is writing his first java code you won't expect him to write nested class.And regarding one public class per file this is what I actually said in my answer.
When I started my Java learning I created inner classes without knowing their real impact on the application design, for me making it work was enough :). Of course, now I know it's not like that and I would prefer that this idea is pretty clear for OP. Besides, OP says that * IDE is telling me that I cannot declare my main static* (which is really odd, by the way) instead of The public type <ClassName> must be defined in its own file that will be the error by having 2 public classes in the same file.
@LuiggiMendoza You are right. static method in non-static nested class leads to The method main cannot be declared static; static methods can only be declared in a static or top level type in eclipse.
0

Thank you all for your comments and suggestions. It was really helpful. I followed the suggestions and created 2 files. One for my tester and one for the main class.

Here is my tester class code

class StudentTester {

    public static void main (String[] args){

        StudentAverage testAverage = new StudentAverage("Jose Mazorra", 50);

            testAverage.addQuiz(900);
            String name = testAverage.getName();

            double avg = testAverage.getAverageScore();

            System.out.println(name);
            System.out.println(avg);


    }

}

It was much easiear to create separate files than doing what I was originally doing which was to have everything in one file. Much cleaner and easier to maintan. I also made some modifications to my main clase code:

    /**
   A student who is taking quizzes.
*/
public class StudentAverage
{ 
    //Instances variables
    private String name;
    private double quizScores;
    private double numOfQuizzesTaken;

    /**
     * Constructs a Student object with a name "Jose Mazorra" and zero total quiz score
     */
    public StudentAverage()
    {
        name = "Jose mazorra";
        quizScores = 0;
        numOfQuizzesTaken = 1;
    }

    /**
     * Constructs a Student object with an initial name and total quiz score
     */
    public StudentAverage(String pName, double pQuizScore)
    {
        name = pName;
        quizScores = pQuizScore;
        numOfQuizzesTaken = 20;
    }


   /**
      Gets the name of this student.
      @return the name
   */
   public String getName()
   {  
     return name;
   }

   /**
      Adds a quiz score.
      @param score the score to add
   */
   public void addQuiz(int score)
   {  
       quizScores = quizScores + score;
   }


  /**
      Gets the sum of all quiz scores.
      @return the total score
   */
   public double getTotalScore()
   {  
     return quizScores;
   }

/**Returns the average of all quiz taken
 * by the student
 * @return average score
 */

public double getAverageScore(){

double avgScore;

avgScore = (quizScores / numOfQuizzesTaken);

return avgScore;

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.