We encourage you to work in pairs for this challenge to create a Student class with constructors.
First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables?
Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes the instance variables to default values, one that has 4 parameters to set the instance variables, and one that has 1 parameter for the most important instance variable and uses defaults for the others.
Add a print() method that uses System.out.println to print out all the instance variables.
Add a main method that constructs at least 3 Student objects using the 3 different constructors and then calls their print() methods.
/** class Student
* with 4 instance variables,
* 3 constructors, a print method,
* and a main method to test them.
*/
public class Student
{
// Write 4 instance variables
// Write 3 constructors to initialize the instance variables
// 1. no parameters using default values
// 2. 1 parameter and the rest default values
// 3. 4 parameters
// Write a print method that prints all the instance
variables
public void print()
{
}
// main method
public static void main(String[] args)
{
// Construct 3 Student objects using the 3 different
constructors
// call their print() methods
}
}
Basic details of a Student is Name, age, Rollno and branch. Consider the below example instance variables and their data types.
Name - It is a string value
Age - It is integer value
Roll-no - It is a string value
Branch - It is a String value
Constructor:
constructors are the methods used to initialise the instace variables/objects of a class. when an object is created for a class then automatically constructor is called.
Program:
public class Student
{
String name;
int age;
String rollno;
String branch;
public Student() //Default Constructor
{
this.name="Hari";
this.age=20;
this.rollno="5R3";
this.branch="IT";
}
public Student(String name) //Parameterised
constructor with 1 parameter
{
this.name=name;
this.age=25;
this.rollno="5R3";
this.branch="CSE";
}
public Student(String name,int age,String
rollno,String branch) //Parameterised constructor with 4
parameters
{
this.name=name;
this.age=age;
this.rollno=rollno;
this.branch=branch;
}
// print method to display the instace variables
public void print()
{
System.out.println("Student
Details:");
System.out.println("Name:
"+this.name);
System.out.println("Age:
"+this.age);
System.out.println("Roll-No:
"+this.rollno);
System.out.println("Branch:
"+this.branch);
System.out.println();
}
public static void main(String args[])
{
Student s1=new Student();
Student s2=new
Student("Sri");
Student s3=new
Student("Hareesh",20,"5d3","ECE");
s1.print();
s2.print();
s3.print();
}
}
Output:
E:\>javac Student.java
E:\>java Student
Student Details:
Name: Hari
Age: 20
Roll-No: 5R3
Branch: IT
Student Details:
Name: Sri
Age: 25
Roll-No: 5R3
Branch: CSE
Student Details:
Name: Hareesh
Age: 20
Roll-No: 5d3
Branch: ECE
Explanation:
In Object oriented programming, when an object is created for a class, complier searches for a constructor to execute, after that only it xecutes other methods. Here we created 3 different constuctors with 0 parameters, 1 parameter, 4 parameters respectively. If we do not pass arguments to the constructors, it take the default values.
Get Answers For Free
Most questions answered within 1 hours.