Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using getters and setters in java

I have following 3 java classes List, Students & Main.

public class List {

}
public class Students {

    String name;
    int age;
    int marks;
    String grade;


    public Students(String name, int age, int marks, String grade) {
        this.name = name;
        this.age = age;
        this.marks = marks;
        this.grade = grade;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.print("Enter student's  name: ");
        String name = input.next();
        System.out.print("Enter student's age: ");
        int age = input.nextInt();
        System.out.print("Enter student's marks: ");
        int marks = input.nextInt();
        System.out.print("Enter student's grade: ");
        String grade = input.next();

        List[] studentsList = new List[10];

        studentsList[0] = new List();
        studentsList[0].setName(name);
        studentsList[0].setAge(age);
        studentsList[0].setMarks(marks);
        studentsList[0].setGrade(grade);
    }
}

Array studentsList has to be declared using List class and I have declared the variables, constructor and getters-setters in Students class. I want to add the student details to the first element of the array using getters & setters. However it is giving me an error and I feel like my entire approach towards implementing this is wrong. Can anyone help me out with this? Thank you!

Error : cannot resolve method ‘setName’ in ‘List’ (same for all other 3 assignments.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First off, the List interface in java is already defined. You should not be redefining it. If you are creating a new List, you need to have it implement java’s List interface.

Assuming you are using java’s List and not your own…

Your Student class if fine. When you are trying to initialize a new Student you are initializing it as a List.

So instead of:

List[] studentsList = new List[10];

studentsList[0] = new List();
studentsList[0].setName(name);
studentsList[0].setAge(age);
studentsList[0].setMarks(marks);
studentsList[0].setNumLiters(grade);

Use

List<Student> students = new ArrayList<Student>();

Student newStudent = new Student(name, age, marks, grade);
students.add(newStudent);

If you want to use the get and set functions, you need to add another constructor to your class. This is called overloading.

public Student(){
}

Then

List<Student> students = new ArrayList<Student>();

Student newStudent = new Student();

newStudent.setName(name);
newStudent.setAge(age);
newStudent.setMarks(marks);
newStudent.setNumLiters(grade);

students.add(newStudent);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading