In: Computer Science
Add two lines of code in main() function to create a variable, goodStudent, whose type is Student with name, "John" and age, 21. public class Student { private String name; private int age; public Student(){ name = ""; age= 0; } public Student(String initName){ name = initName; age = 0; } public String getName() { return name; } public void setAge(int anAge) { age = anAge; } public static void main (String[] args) { // !!!! Your two lines of code come here !!! } }
Program:
Explanation:
Line 26: Here creating object called goodStudent for class Student by passing string parameter
Line 27: calling setAge() method using object goodStudent
Program:
public class Student {
private String name;
private int age;
public Student(){
name = "";
age= 0;
}
public Student(String initName){
name = initName;
age = 0;
System.out.println(name);
}
public String getName() {
return name;
}
public void setAge(int anAge) {
age = anAge;
System.out.println(age);
}
public static void main (String[] args) {
Student goodStudent=new Student("John");
goodStudent.setAge(21);
}
}
Output: