136k views
2 votes
Create a class Students in java that has three data members’ id, name and static numOfStudents.

Define a method that is used to give the data members new values, and add 1 to numOfStudents.
A method that is used to print student’s information.
Create three objects of Student’s class and use the available methods for each object

User CallMeLaNN
by
9.0k points

1 Answer

2 votes

Final answer:

To create a class Students in Java with three data members (id, name, and static numOfStudents), define a method to set the data members, add 1 to numOfStudents, and another method to print student's information. Use the available methods for each object of the Students class.

Step-by-step explanation:

To create a class called Students in Java with three data members: id, name, and a static variable numOfStudents, we can define the class as follows:



public class Students {
private int id;
private String name;
private static int numOfStudents;

public void setData(int id, String name) {
this.id = id;
this.name = name;
numOfStudents++;
}

public void displayData() {
System.out.println("Student ID: " + id);
System.out.println("Student Name: " + name);
}

public static void main(String[] args) {
Students student1 = new Students();
Students student2 = new Students();
Students student3 = new Students();

student1.setData(1, "John");
student2.setData(2, "Jane");
student3.setData(3, "Smith");

student1.displayData();
student2.displayData();
student3.displayData();

System.out.println("Number of students: " + numOfStudents);
}
}



In this code, the

setData

method is used to assign values to the

id

and

name

data members, and increment the value of

numOfStudents

by 1. The

displayData

method is used to print the student's information. Finally, in the

main

method, three objects of the

Students

class are created and their data is set using the

setData

method. The

displayData

method is then called to print the information of each student, followed by printing the total number of students using the

numOfStudents

static variable.

User Btmills
by
8.0k points