Luffy
Luffy

Reputation: 51

Parameters, Setters and Getters

Is there a way to pass an argument from a class into an array individually (I am not sure how to word it correctly so let me try it with an example).

Suppose I have a class named Lab and from this class there is an instance variable named grade with a getter and setter. Also, there is an array.

public class Lab {
    private double grade;
    private double[] array = new double[10];

    public void setGrade(double grade) { //sets grades for lab reports      
    }

    public double getGrade() {
        return grade;       
    }

Now, from a main class called Teacher I want to call lab and pass students grades into setGrade individually and for each grade passed into the parameter I want those grades to be placed in an array so that in a later class (that I will create) I can retrieve those lab grades.

Scanner input was my first thought but I was just wondering if there is another way to do this using getters, setters, constructors, etc.

Upvotes: 0

Views: 1711

Answers (3)

ManyQuestions
ManyQuestions

Reputation: 1089

You could use an ArrayList. That way you could do the following:

private ArrayList<Double> grades = new ArrayList<>();

public void addGrade(double grade){
    grades.add(grade);
}

A grade will be added to 'grades' everytime you call addGrade(double grade).

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347194

Because you want to pass a single value each time to an array, you need to keep track of where you can safely add the next value

public class Lab {
    private double grade;
    private double[] array = new double[10];
    private int insertPoint = 0;

    public void setGrade(double grade) {
        if (insertPoint < array.length) {
            array[insertPoint] = grade;
            insertPoint++;
        }
    }

Now, to get a value, you'll need to know which value you want to get...

public double getGrade(int index) {
    double value = 0;
    if (index >= 0 && < insertPoint) {
        value = array[insertPoint];
    }
    return value;       
}

Another option might be to pass the index you want to set...

    public void setGrade(int index, double grade) {
        if (index >= 0 && index < array.length) {
            array[index] = grade;
        }
    }

Or you could use variable arguments...

    public void setGrade(double... grade) {
        for (int index = 0; index < grade.length && index < array.length; index++) {
            array[index] = grade;
    }

This would allow you to pass a single value or multiple values to the same method...

Lab lab = new Lab();
lab.setGrade(1);
lab.setGrade(1, 2, 3, 4, 5);
double grades[] = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
lab.setGrabe(grades);

Just remember, in above example, each call to setGrades replaces the elements in the array (up to the number of elements you pass)

It would be simpler to use some kind of List though

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

If your Lab class has to hold multiple grades then it should make it clear as

public class Lab {

    private double[] studentGrades;

    public void setStudentGrades(double[] grades) { //sets grades for lab reports      
        studentGrades = grades;
    }

    public double[] getStudentGrades() {
        return studentGrades;       
    }

Now, in your Teacher class you'll collect the grades in a local array and use the setter to set the values on your Lab class object as

int numOfStudents = scanner.nextInt();

double[] grades = new double[numOfStudents];
for(i = 0; i < numOfStudents; i++) {
     grades[i] = scanner.nextDouble();
}

lab.setStudentGrades(grades);

Upvotes: 0

Related Questions