Write a program to print the names of students by creating a
Student class. If no name is passed while creating an object of
Student class, then the name should be "Unknown", otherwise the
name should be equal to the String value passed while creating
object of Student class.
public class Student {
private String name;
public Student() {
name = "Unknown";
}
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Main {
public static void main(String[] args) {
Student[] students = {new Student(), new Student("Tom"), new Student("Sam"), new Student()};
for (Student student : students) {
System.out.println(student.getName());
}
}
}
Comments
Leave a comment