Here is your code, save it in Student.java file
import java.io.*;
public class Student
{
public String name;
private int roll;
private int marks;
Student(String name)
{
this.name = name;
}
Student(String firstname, String lastname)
{
this.name = firstname + ' ' + lastname;
}
public void setRoll(int roll)
{
this.roll = roll;
}
public int getRoll()
{
return this.roll;
}
public void setMarks(int marks)
{
this.marks = marks;
}
public int getMarks()
{
return this.marks;
}
public void printStudentDetails()
{
System.out.println("name: " + this.name);
System.out.println("roll: " + this.getRoll());
System.out.println("marks: " + this.getMarks());
}
public static void main(String[] args)
{
Student s1 = new Student("Harsh");
s1.setRoll(12);
s1.setMarks(94);
System.out.println("Details of student 1: ");
s1.printStudentDetails();
Student s2 = new Student("Aryan", "Roy");
s2.setRoll(21);
s2.setMarks(49);
System.out.println("\nDetails of student 2: ");
s2.printStudentDetails();
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.