1

I've got two classes, LabClass and Student with students being enrolled in the class and given grades.

I've called Student into LabClass as an ArrayList using the following:

private List<Student> students;

Now, I want to be able to individually allocate grades to each student after they have been enrolled.

I've created a method in the Student class to allocate grades and tried to call upon it in LabClass, but it didn't work.

The code in Student is as follows:

public void grade(int marks){
    grades = marks;
}

And I tried to call it within LabClass with the following code:

public void giveGrades(){
   for(Student student : students){
       student.grade(int marks);
   }
}

But I get returned the error ".class expected". What's wrong with my code?

Should I be using the set command of Arrays to change the element's value? If so, how should I write it such that it can take in external values from a parameter? Also, how do I specify which value of the element is being changed?

Brian
  • 119
  • 6
  • 1
    When you call a method, you're not supposed to specify the type of the arguments of the method. It should be `student.grade(marks)`, not `student.grade(int marks)` (assuming there is a field named `marks`, of type `int` in LabClass (which would be quite a strange design: classes don't have grades, and you rarely give the same grade to all the students)). – JB Nizet Nov 02 '17 at 20:19
  • do: ***student.grade(marks);*** – ΦXocę 웃 Пepeúpa ツ Nov 02 '17 at 20:21
  • @JBNizet I do not have a field named marks with type int in LabClass. Is it not possible to input these fields externally? There is a field called grades with type int in my Student class, but I don't think it's relevant? –  Brian Nov 02 '17 at 20:25
  • You want to allocate a grade to each student, but where are the grades you want to allocate? Are they going to be received from the console? Are you going to manually create a list of grades and assign one grade to each student? – Mauricio Martinez Nov 02 '17 at 20:37
  • @MauricioMartínezJiménez They're supposed to be received through an external parameter. For context, I'm a super beginner so I don't really know how to explain it, but my software, BlueJ, has a dialog box that comes out when you create an external parameter. –  Brian Nov 02 '17 at 20:45
  • I think that's worth to mention in the body of the question. The site says: "BlueJ allows you to interact with objects. You can inspect their value, call methods on them, pass them as parameters and more". I haven't used it but if you mention that tool maybe someone who knows about it can help you, or at least help to give to the question the right direction. Update: I added the tag bluej to your question. – Mauricio Martinez Nov 02 '17 at 20:52
  • @MauricioMartinez Thank you very much, Mauricio. I've approved your edit, hopefully somebody who knows how to help me will come along. –  Brian Nov 02 '17 at 21:07
  • Meanwhile you can check this other [question](https://stackoverflow.com/questions/16018998/how-do-i-enter-parameters-for-an-arraylist-in-bluej). My guess is that you will need a list of grades (that you can initialize from the bluej UI ) and use that list as a parameter (maybe in the method giveGrades) so you can assign each element in the grade list to each student in students – Mauricio Martinez Nov 02 '17 at 21:16

1 Answers1

1

First let's clarify some concepts here. As other mentioned, this code:

public void giveGrades(){
   for(Student student : students){
       student.grade(int marks);
   }
}

does not work because you must not set the type of the parameter when you're using a method (you do it when declaring it). So the first "fix" here would be:

public void giveGrades(){
   for(Student student : students){
       student.grade(marks);
   }
}

Now the question is: What is marks? It's not defined anywhere. Let's suppose for a moment that we do have it defined and that its value is 3. The previous code assigns 3 as a grade for all the students. That's not very realistic, even if "marks" is an int that we somehow receive as a parameter. We probably want a grade for each student, and that means we need a list. So let's say we want a parameter with the list of grades we want to assign to the students: ArrayList<Integer> grades (you can change it later to Double if you want, but the idea is the same). In this case we need to go through each element in the list of students and assign a element from the list grades. Something like this:

you have a list of students students: <s1, s2, s3, s4>

you have a list of grades grades: <g1, g2, g3, g4>

you eant to assign to each student a grade from the list: s1.grade(g1), s2.grade(g2), s3.grade(g3), s4.grade(g4)

We can achieve that with a for also, but one that allows us to iterat over both lists. A possible way (there are more sofisticated ones):

public void giveGrades(ArrayList<Integer> grades){
   for(int i = 0; i < students.size(); i++){
       students.get(i).grade(grades.get(i))
   }
}

That's from the java perspective. Now, according to your comments you need a external parameter and you mention the use of bluej. In the question How do I enter parameters for an ArrayList in BlueJ? you can get a better idea about how to pass a parameter to your method.