ok i got this assignment, and i'm a bit confused about what this method is supposed to do.

i have to create a "setLab" method and accept a Grade object (which is already done and provided). this is what i got so far:

public class CourseGrades{
	
	private Grade [] grades = new Grade [4]; //it tells me to create this array
	
	public void setLab(Grade g){
		
	     //??????
	}

then it tells me that the object (Grade g) should already hold a student's score, which i assume that i do in the main method. ok then, it says grades[0] should reference this object.

so does that mean this?
grades[0] = new Grade;

i'm confused b/c the method doesn't do anything else but that, and it's hard to see its practical value.

Recommended Answers

All 4 Replies

The way you have it set up, I imagine it is THIS:

public void setLab(Grade g, int index)

Otherwise how do you know WHICH grade to set.

Setters and Getters don't do much. They are usually one line functions. The key is the "public" versus "private" modifier. grades[] is private, but setLab is public. The only way you can set grades[] from outside of the CourseGrades class is with a setter function.

but what was the point of accepting a Grade g object as an argument in the first place?

extra info: the CourseGrades have 3 more other methods that accepts either a Grade g object or some other object.

i imagined that my main class looks like this:

Grade g = new Grade g(80);
CourseGrades c = new CourseGrades ();
c.setLab(g);

but what was the point of accepting a Grade g object as an argument in the first place?

It's the only way you can access grades.

public void setLab(Grade g, int index)
{
    grades[index] = g;
}

You can't do this directly.

CourseGrades cg = new CourseGrades(); // or whatever the constructor is
Grade g = new Grade("C"); // or whatever the constructor is
cg.grades[0] = g;

so instead you do this:

CourseGrades cg = new CourseGrades(); // or whatever the constructor is
Grade g = new Grade("C"); // or whatever the constructor is
cg.setLab(g, 0);

ok i think i'm getting it, thanks. will continue to work on this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.