im making a grades program to try to keep my skills sharp. since i have gotten out of apcs i havent really been doing too much of anything with java. i decided the other day that i was going to try to make a grade program, kind of like what a school would use, but command line ofcourse. im trying to make it better and better as i refresh my skills.

well, i hardly got started and im already crashing. bringing back the horrible memories of apcs.

what my problem is, i have a class for students. im using an arraylist so that everything can be edited pretty easily. im getting really confused on how to store the grades with the students...

should i have one list for each student that holds all grades? etc etc

its getting discouraging, but the main thing people dont really understand when im getting help is that i dont want it to be like a typical beginner oop class where when you want to use it you have a tester class where you so something like Student nathan = new Student(45,32,54,99,100,120); i want the program to rely solely on user input.

I have a menu that will let the user select what they want to do, and from there im trying to get the program to gain the information, so that if someone wanted to use it, they would never have to touch the source code.

sorry, about the rambling, just hard to describe what im trying to do.

btw: not anything for an assignment or school, so im open to whatever help you guys can provide.

here is the source:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nathan
 */
import java.util.Scanner;
public class Main {
    public static void main(String[]args)
  {
        Student firstBlock = new Student();
        Scanner sc = new Scanner(System.in);
        int selection;

        do{

        System.out.println("\tMenu");
        System.out.println("1.Add Students");
        System.out.println("2.Add Grades");
        System.out.println("3.Remove Students");
        System.out.println("4.Remove Grades");
        System.out.println("5.Exit");
        selection = sc.nextInt();

        if(selection == 1){
            
    
        }
        if(selection == 2){

        }
        
        }while(selection != 5);

    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nathan
 */
import java.util.*;
public class Student {

    private List<Double> grades = new ArrayList<Double>();
    private String name;
    private Scanner sc = new Scanner(System.in);

    public Student(String n) {
        name = n;
    }

    public Student(String n, List<Double> g) {
        name = n;
        grades.addAll(g);
    }
    
    
    public void addGrade(double g) {
        grades.add(g);
    }

    public String toString() {
        String s = name + ": ";
        for (Double i: grades)
            return s += i.toString() + " ";
    }

}

Are grades really just a Double precision number - ie (a) why Double - how precise are they? and (b) don't you care what the subject was, when the grade was awarded etc?
If there is more to it than just a vastly-precise number, then maybe a Grade class that encapsulates what for/who awarded/date awarded etc. In which case a List of Grades per Student makes tota,l sense to me.
Also, don't be too quick to deprecate
Student nathan = new Student(45,32,54,99,100,120)
Many (most?) real developers will use something like this to test the underlying classes before bolting them into a user interface - you really don't want to be debugging every part of your system at the same time.

you have a very good point with the using the 'Student student = new Student()' thing. i might try to use that until i have my classes to a considerable working point. the grades class also sounds like a good idea. also i will change my list to integers. but my question is, how can i create a list of grades for each student neatly, considering i wont know how many students i have until the user has inputted them? could i use a loop? i know i could do it with a regular array, i think, but with the list it seems a bit harder.

Okay, let's back up to requirements and walk through a little design process. Might be a good thread for beginners to follow.

Nathan - who are the users of this program, and what are they wanting to do with it? What information is each of them concerned with?

the program would hypothetically be aimed a teacher who wanted: a simple solution to keep track of the students in their class, a list of grades for each student, and the ability to add or remove a student and/or grade.

OK, I'm going to continue some low-level discussions in parallel with jon kiparsky's higher-level questions. This is development 21st century style, not classic waterfall. I'm a great beiever in rapid prototyping and "scrum" development.
We should know enough about what a Student or a Grade is to be able to start building version 0.1 of those classes in parallel with UI etc. If they are not able to evolve with the high-level requirements, we got them wrong at some very funsdamental level.
So.
What are the lists of int for? Each Student has zero or more Grades, so a List<Grade> as an instance variable for Student makes sense. As for the unknown number of Students, somewhere in this app you would expect to see a List<Student> containing all the known Students. Now whether that just sits as a static member of the Student class or as part of some class like "School" (etc) will emerge from jon's line of enquiry.
ps Lists are always* a better choice than arrays . If you think you need an array then either (1) you're programming a game where the rules are fully known in advance and never change or (2) there's an absolutely critical performance problem, or (3) you should be using an emun.
* ok, 95-99% of the time.

Edited 6 Years Ago by JamesCherrill: n/a

thats kind of my dilemma at this point. i dont know if i should make a method, or if i should do it in the main, but what i need is this: to prompt the user with a menu, say for example, they select the first option(add a student)..when they do this they will be prompted to enter the name of the student they would like to add, once this is done the name is written to the list. this is pretty much how the whole thing needs to work. im just having a little trouble with that, and also how i would get the list of grades to correspond well with the list of students. i know there is an obvious solution there, it just hasnt hit me yet.

Edited 6 Years Ago by gibson.nathan: n/a

Listen to jon.
But in the meantime, here are some generally useful guidelines.
1. Don't put anything in main except a quick call or two to get things started. So yes, a method like addStudents() that you call from main, and which has some private way of interacting with the user before it calls new Student(...). Think about what you could keep and what you would have to change if you later implemented a Swing GUI for that instead of console I/O.
Grades: Think of a list of grades for each student, ie as part of the student classes' instance variables. That way the association between a student and his grades is automatic. There's no obvious reason (yet) why you would need a list of Grades separate from the Students.

I'd suggest you go back to the drawing board and make a simple UML diagram, or a class hierarchy diagram. That'll help you get organized.

My personal take on it:
1. Get rid of the Scanner in the Student class.
2. Create another Class object which stores an ArrayList of Student objects, and add methods in the Class object to allow the user to add, delete, and access the Student objects.
3. Instantiate a Class object.
4. Put the code where you prompt the user in the main method, and after each entry, add a Student object to the Class object.

By creating a Class object, you can easily calculate grades and such because you have one central "storage" of Students, rather than unrelated objects.

In case you're still confused (my explanation may not be great) here's an example:

class Class {
private ArrayList<Student>s;

public Class() {
s=new ArrayList<Student>();
}

// methods
}

Edited 6 Years Ago by coil: n/a

@coil.
Yes. Probably. But listen to jon.
Do we know that a Student belongs to a class? Maybe he belongs to a School and attends many classes? This isn't specified yet, so its a bit early to do architecture. (and ps "Class" is not a good class name!).
I already covered the point that there will be a Collection of Students somewhere.
I assume that point 4 is a mistake (see my previous post).

the program would hypothetically be aimed a teacher who wanted: a simple solution to keep track of the students in their class, a list of grades for each student, and the ability to add or remove a student and/or grade.

Is aimed. No hypotheticals.

Okay, Teacher wants to track student grades, add or remove students, and assign and change grades. (I assume that every student gets a grade for the class, so "remove grade" isn't what we want)

Does the Teacher have one class, or several?

Is there one Teacher, or are there several?

Are there any other actors here? for example, do students view their grades or add/drop courses? Or is this strictly a teacher-oriented utility? (In that case, you might want to think about how the student gets notified of their grade... at least an export to a CSV would useful)

@coil.
Do we know that a Student belongs to a class? Maybe he belongs to a School and attends many classes? This isn't specified yet, so its a bit early to do architecture. (and

Definitely too early for architecture. We're still figuring out what it is that's being built.

ps "Class" is not a good class name!).

s/Class/Course/g

I'd suggest you go back to the drawing board and make a simple UML diagram, or a class hierarchy diagram. That'll help you get organized.

My personal take on it:
1. Get rid of the Scanner in the Student class.
2. Create another Class object which stores an ArrayList of Student objects, and add methods in the Class object to allow the user to add, delete, and access the Student objects.
3. Instantiate a Class object.
4. Put the code where you prompt the user in the main method, and after each entry, add a Student object to the Class object.

By creating a Class object, you can easily calculate grades and such because you have one central "storage" of Students, rather than unrelated objects.

In case you're still confused (my explanation may not be great) here's an example:

class Class {
private ArrayList<Student>s;

public Class() {
s=new ArrayList<Student>();
}

// methods
}

i see where your going, it makes alot of sense. im not sure if this could work, its been awhile like i said, but...

could we have a student class, that has an arraylist for the grades of that student and
then...

in the main could we have something like this:

PSUEDO CODE:

out: Please make a menu selection
in: 1 (add a student)
out: Enter the students name
in: name = nextLine();
Student name = new Student(); //could name be a variable that would change with what was
entered allowing a new instance of Student to be made everytime the user enters a new name???????????????????

ex:
user enters the name andrew
Student andrew = new Student();

just a thought..

Is aimed. No hypotheticals.

Okay, Teacher wants to track student grades, add or remove students, and assign and change grades. (I assume that every student gets a grade for the class, so "remove grade" isn't what we want)

Does the Teacher have one class, or several?

Is there one Teacher, or are there several?

Are there any other actors here? for example, do students view their grades or add/drop courses? Or is this strictly a teacher-oriented utility? (In that case, you might want to think about how the student gets notified of their grade... at least an export to a CSV would useful)

for the sake of right now and really just trying to do it to heighten my skills, i want to keep it hard enough for me to learn, but not so hard that i lose sight of what im trying to do. lets just keep it one class and one teacher for right now.

sorry, for posting so much, but i just got back to my computer and wanted to keep you guys up to date and not thinking i gave up interest. but thanks so much for you all helping. its all very much appreciated.

I agree jon, I'm still not sure what the ultimate purpose of this program is (I know you had a description in your original post, but it seems that you've made some revisions). Is it just a class-writing exercise with no real purpose, is it a full-blown program with a GUI (with frames and such), is it simply a program that stores grades for each student...

Perhaps if you can clarify that it would be easier to create a plan for this program.

Edited 6 Years Ago by coil: n/a

I agree jon, I'm still not sure what the ultimate purpose of this program is (I know you had a description in your original post, but it seems that you've made some revisions). Is it just a class-writing exercise with no real purpose, is it a full-blown program with a GUI (with frames and such), is it simply a program that stores grades for each student...

Perhaps if you can clarify that it would be easier to create a plan for this program.

alright, just to be clear, this has no purpose. it is simply something im doing because i want to learn.

you could say that it is a class writing exercise. it is really the first program that i have ever tried to do on this scale. like i said, im learning.

definitely no gui here. i couldnt make this in a gui if i wanted to, simply dont have the skills necessary.

and, yes, it is simple a program that will store grades for each student. with a few more features: ability to add students, grades, and remove them.

the only thing that makes it difficult for me is incorporating the user input in to all of it.

i could easily write the classes, but i have never really seen good examples of how to design oop classes and use input from the user. most of the tutorials i see are writing a class and then filling it with hard coded data in the source.

hope this clarifies. thanks.

OK, that definitely clears things up a bit. If you don't need to calculate average grades, or put students in a class, then I think you can code this program with just a Student class - we can leave out the Course/Teacher classes. Then, in the main, have a collection of Students.

If you want to start off simple, have a Student object with only one instance variable: a byte to store a percentage grade.

Once that works, then you can modify the byte into an ArrayList, and add a String variable for the name, etc.

As for user input, I suppose you could initialize a Student object as follows:

// get input
Student s=new Student(input...);

Or, if you have a collection of Students:

//get input
<collection>.add(new Student(input...));

Later, when you want to modify/access existing students:

//get index of Student
<collection>.get(index).<modify>

Edited 6 Years Ago by coil: n/a

progress is slow, but wanted to get some advice on my average method. look like it may work? im going of random online tutorials here.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nathan
 */
import java.util.*;
public class Student {

    private List<Double> grades = new ArrayList<Double>();
    private String name;
    private Scanner sc = new Scanner(System.in);
    private int average;
    private Iterator<Double> itr = grades.iterator();
    

    public Student(String n) {
        name = n;
    }

    public Student(String n, List<Double> g) {
        name = n;
        grades.addAll(g);
    }
    
    public int getAverage(){
        while(itr.hasNext())
        {
            double i = itr.next();
            int sum = 0;
            sum += i;
            average = sum/100;
            
        }
        return average;
        
    }
    
    
    
    public void addGrade(double g) {
        grades.add(g);
    }

  
   

}

A couple of problems with your average...you are creating a sum int in the while loop so it doesn't actually add up - it is erased each iteration. Also, divide by # of elements, not 100.

Do this instead:

public double getAverage(){ //return double not int
        double sum=0;
        for(Double d: grades) //foreach loop
        {
            sum += d; //add each element
        }
        return sum/grades.size(); //calculate average
 
    }

Lastly, get rid of the Scanner object in the Student class - you want it in the main, not in each object. You can keep the iterator if you want, but it might just be easier to do for(int i=0; i<grades.size(); i++); .

Edited 6 Years Ago by coil: n/a

alright, thanks for the advice there. my thinking was a little off. i know what im trying to do here, and im pretty sure this isnt how you do it, but will someone tell me if it is possible..

if(selection == 1){
            System.out.println("Please enter the name of "
                    + "the student you would like to add: ");
            String name = sc.nextLine();
            Student name = new Student();

No, I don't think that works. You could do <collection>.add(new Student(name)); assuming there is a corresponding constructor.

i have this in my main

if(selection == 1){
            
            System.out.println("Enter the name of the student you would"
                    + "like to add: ");
            String name = sc.nextLine();
            grades.add(new Student(name)); 

        }

but netbeans is saying that it cant find grades. if they are all in the same package it should be accessible, correct?

I believe so, but regardless, you should initialize grades in the same class as the above code segment.
That is:

public class GradeProgram {
private static ArrayList<Student>grades=new ArrayList<Student>();

public static void main(String[]args] {
if(selection==1) {
//etc.
}
}

for the sake of right now and really just trying to do it to heighten my skills, i want to keep it hard enough for me to learn, but not so hard that i lose sight of what im trying to do. lets just keep it one class and one teacher for right now.

Okay, that's cool. Part of the process is defining where you're going to stop. Good design is extensible, but at some point it stops making sense to extend. So if you start with a program that a teacher might use to keep track of one class, it's not too hard to make decisions that allow it to be used for several classes. (ie, a full teaching load in one program) You could see this possibly even being extended to allow several teachers to use it. (say, a CS department in a college, where multiple Students will take multiple Classes from Multiple Teachers - the head of the department might like this because he could get an overview of student progress in the middle of the term, based on teachers' grades and notes.) (Notes? Will we allow the teacher to keep notes? Aha! Please notice that ideas come up in the design phase, and you can decide whether they're worth considering for inclusion. Much easier - cheaper - to include that now, than to go back and have to do it later. This is one of the reasons you do the design before you write the code. So, do you want the teacher to be able to attach notes on student progress, reasons for particular marks?)


Okay. So for the moment, we have one user, a teacher, who's keeping track of one class. What sorts of things are they keeping track of? A typical class has some combination of assignments, quizzes, tests, midterm and final exams - any others? Probably.
So one thing the teacher needs to be able to do is to specify what sorts of grades he's recording, right?

Use case: Teacher defines a [Graded Item]. I don't know what the real name of that is, but he's going to do it a few times. A [Graded Item] will be something that the student is responsible for, which accounts for a certain amount of the student's grade. Now, different teachers do this in different ways. A teacher might say, you will have 5 programming assignments, each one is worth 40 points, and then you have a mid-term, which is worth 80 points, and a final, which is worth 120 points. Or, they might say "Your programming assignments will be worth half of your grade, and you will have a mid-term which will be worth 20% of your grade, and a final which will be worth 30%. If my math is right (always a dubious proposition!) these are equivalent, but evidently some teachers prefer one, and some teachers prefer another method. Since we're avoiding GUIs, we won't talk about graphical methods for doing this - but maybe we should do this in a way that leaves a hook for that. (notice this - even if you don't think you're going to do this particular extension. a) it's good practice, to think ahead to something you're not building now. b) code tends to stick around, and its really nice to leave yourself room to extend, because you can really be surprised by what will come back to haunt you. and c) it's not actually that hard to leave these hooks, it's mostly a matter of doing good modular design)

Okay, this is getting long, and maybe you just want to start writing code - I know you just want to start writing code, you're doing it already! - but here's a design trick. Think about what your user (only one, in this case) is going to do with this program. Then think about how they want to do it. This will tell you what you need to build.

Some other use cases:
- Teacher changes/adds/drops an assignment, test, or other such thing. What does that look like to the teacher?
- Teacher defines a non-assignment use case. Attendance, participation, bribes, or whatnot. How is this different from the first case, if at all? Is it maybe similar to the second case?
- Extra credit assignments, bonuses, and such. Consider pre-defined and ad hoc scenarios. "Each assignment will have an extra credit portion worth at most 20% of the points for that assignment" versus "Wow, Jane, that's an excellent solution to the knapsack problem, that's worth 10 extra points".


You may be noticing something - there don't really seem to be right answers to some of these. Yep. That's why it's called design. You have to make decisions, and ultimately it's your call. The point of all these use cases is to force yourself to think about the weird cases, the edge cases, the stuff you'll be glad you thought of ahead of time, and to make you do it now.

So think about these, and other cases that you're going to design. The more cases you work out in advance, the better your picture of the class structure you're going to build will be. (and yes, I am over-designing this. That's the point of the exercise!)

Good points Jon. Nathan, I think you should look into the Software Development Life Cycle (SDLC - Wikipedia link) if you plan to program larger projects in the future. As you'll see, the vast majority of the time spent on a project is planning and design, not coding, because if you have a clear vision of what you're actually doing, coding is relatively easy.

This article has been dead for over six months. Start a new discussion instead.