I need to know how I can insert data inside objects inside an array of objects using my already made Set methods.
i need to know how should i do it through user , i mean JOptionPane input dialog
student[] s = new student[5];
for (int i=1 ; i <= s.length ;i++) {
s[i] = new student(i,"AAA","Ecommerce",0.0);
}
for (int i=1; i<=s.length;i++) {
name = JOptionPane.showInputDialog("Please Write Name for student n " + i);
major = JOptionPane.showInputDialog("Please Write Major for student n " + i);
gpa = Double.parseDouble(JOptionPane.showInputDialog("Please Write GPA for student n " +i));
s[i] = new student(i,name,major,gpa);
}
I tried to do vars here that get data from user by JOptionPane, but it seems that i only use my already made constructor , not the Set methods.
I need to use the methods because it has some validation code inside it.
Any ideas?
Don’t use values as constructor parameters.
Use only implicit constructor that will allocate memory.
As for set methods, write them like :
In this set methods do your validation code. Your class needs to have private attiributes, which are exact same as your constructor parameters.
Then change
line s[i]=new student(i,name,major,gpa);with
I hope this is what you meant
Edit :
Or continue using constructors and do validaing with parameters BEFORE you make new instance of Student