Closed Solved

JAVA - student program

7 answers Last reply Best Answer
More about java student program
  1. Best answer
    1) I can't tell you without debugging through this code. Do you know how to use your IDE to debug through your code?

    2) Again I'd need to debug.

    3) You need to iterate over the array elements. For example:

    StringBuilder b = new StringBuilder();
    for(int i=0; i<courses.length; i++){
    Course c = courses;

    You should implement a toString method on Course

    4) Like 3 you need to iterate over the array and add up all the Course credits. Average grade will just be the sum of the Course grades divided by the size of the course array.

    5) This is a tricky file format. Who has defined this file format? Can you assume that the address is always two lines? XML would be a better structure for this.

    I've included some comments about your classes. They are intended to be useful not damning. I appreciate you're new to Java! I'm just pointing out design issues and coding convention mistakes to try and help you. They shouldn't take too long to do. Some of it might confuse so feel free to ask about anything - I'll try and explain it better.


    The Person class:

    1) Get into the habit in calling super() in every constructor. Even if you aren't extending anything (you are actually extending Object)

    2) I wouldn't default the name or address to anything other than null or "". Presumably you've set it as something else so it's printed to the screen or to file by another method. That method should handle what null or "" means in this context. Not the data class.

    3) This class should not have the main method. Whilst it will work it's not good practice. It's good to think about what a Class' responsibilities are. Your program has nothing to do with People, Students or Courses. If you were to use this Person class in another program would it contain the main method? Not at all.

    Conceptually your program is a fourth class, maybe it's called StudentRecords or simply Runner. Look at your practical sheet for a good class name. I'm going to refer to this fourth class as Runner for now.

    The Course Class:

    1) Call super in the constructor.

    2) Don't have a property of the class the same as the Class name. It will compile and run fine it's just confusing. A Course has a name not a course. So change that course field to name.

    The Student Class:

    1) The courses field is somewhat confusing here. Firstly you are using an array rather than a Collection. Secondly, you are defaulting to an array of length 4 but then in your constructor you create an array of length 100?

    Consider using the Vector class instead. It's probably a little advanced if you are just learning Java but the basics would be:
    private Vector<Course> courses = new Vector<Courses>();

    Then you just call the add method on it to add additional Course objects. You don't need to care about it's size - it will manage all that for you.

    2) Avoid defaulting major in the constructor. Let it be null or an empty string.

    3) Taken doesn't need to be stored. You can derive it's value from the size of the courses structure. So courses.length (or courses.size() if you change to using a Vector). Either add a getCourses method that people can get the size of or remove the setTaken method and make the getTaken method do "return courses.length". You may want to check if the courses object / array is null if doing the latter.

    4) Class method names always start with lowercase. LoadFromFile, LoadFromConsole and GradeReport are badly named. It will work and compile but it's against Java programming conventions.

    5) I'm not convinced LoadFromFile, LoadFromConsole and GradeReport are methods that should be on the Student class. Consider putting these on the Runner class instead.

    6) Don't force the caller of setCourses to define the index in the array. Make a method called addCourse which takes a course. This method will add it to the end of the array. Using a Vector for your courses pays off here because you can just add the Course without fear of the array being too small.

    The Runner Class

    1) Contains the main method. This method creates a new instance of Runner and calls the run method. No reason to store it; doing new Runner().run() is acceptable.

    2) The run method contains what you had in your previous main method.

    3) The LoadFromFile, LoadFromConsole and GradeReport methods from the Student class are now on this class.

    4) You've hardcoded some of these "magic" input values and used the values in the multiple places. For example you've made an input of 1 mean "load grades from console". I'd define this input as a static constant. I.e. in the runner class add:

    public static const int LOAD_FROM_CONSOLE = 1;

    Then use this constant in your main method:

    System.out.println(LOAD_FROM_CONSOLE+". Load Grades From Console" );


    switch (choice){

    So why go to all that bother? Well it means that you are using the exact value everywhere. It's more readable and quite importantly if you suddenly decide the input needs to be 5 instead you just need to change the constant rather than the millions of places it's hardcoded.
  2. Is your code working now?
  3. Yeah made any progress buddy?
  4. Sorry for the lack of response, yea I got it to work, met up with my professor and was very simple after he explained it. Got a 100 on it =D.

    BTW this is the best site I have ever found!!!!!!!!!!! Love all the help everyone gives.
    THANK YOU to everyone
  5. Best answer selected by laserpp.
  6. Well done buddy glad you got such an awesome grade - keep it up!
  7. This topic has been closed by Area51reopened
Ask a new question

Read More

Programming Apps