i’ve just started to learn java yesterday from some books and am doing pretty good i guess i started doing some exercise but i came to this question where it asks me to create a class of student with methods and attributes so i know what is class n methods but i dont know how to do it any guideline?thank you
Reread the chapter, and try writing some code (your computer will not break if you don’t do it correctly the first time).
Once you have written some code, should it not work, feel free to post it and we’ll help you understand why it doesn’t work and what you need to do to fix it.
Hi,
It is asking for something like this code below, which has one property on the Student class, which is the name of the student. I have shown it with the ‘default’ constructor [which is the public Student() bit in the code] but you could have a constructor which accepts the name of the student as well [public Student(String name) ]. Anyway, here is it:
public class Student
{
private String name;
//Default class constructor called when object of this class is newly created
public Student()
{
//Initialise
name = "";
}
public String getName()
{
return name;
}
public void setName(String name)
{
//Assign the name passed in to this method to the name
// variable declared above for this class object instance
this.name = name;
}
}