public class Student {
    String name;
    int age;
    String major;

    // 0 params
    public Student() {
        this.name = "Unknown";
        this.age = 0;
        this.major = "Undeclared";
    }

    // 1 param
    public Student(String name) {
        this.name = name;
        this.age = 0;
        this.major = "Undeclared";
    }

    // 3 params
    public Student(String name, int age, String major) {
        this.name = name;
        this.age = age;
        this.major = major;
    }

    public void printStudentDetails() {
        System.out.println("Name: " + this.name);
        System.out.println("Age: " + this.age);
        System.out.println("Major: " + this.major);
    }
}

Student student1 = new Student();
Student student2 = new Student("John");
Student student3 = new Student("John Mortensen", 21, "Computer Science Teacher");

student1.printStudentDetails();
student2.printStudentDetails();
student3.printStudentDetails();
Name: Unknown
Age: 0
Major: Undeclared
Name: John
Age: 0
Major: Undeclared
Name: John Mortensen
Age: 21
Major: Computer Science Teacher

The Student class has three constructors:

  • A default constructor that takes no arguments and initializes the name to “Unknown”, the age to 0, and the major to “Undeclared”.
  • A constructor that takes a String argument for the name. It sets the age to 0 and the major to “Undeclared”.
  • A constructor that takes three arguments: a String for the name, an int for the age, and another String for the major. The Student class also has a method printStudentDetails() that prints the name, age, and major of a student to the console.

After defining the Student class, the code creates three Student objects:

  • student1 is created using the default constructor.
  • student2 is created with the name “John”.
  • student3 is created with the name “John Mortensen”, age 21, and major “Computer Science Teacher”. Finally, the printStudentDetails() method is called on each of the Student objects to print their details to the console. This will display the name, age, and major of each student.