Create a project to model students with firstname, lastname, age, gender and student number. Create two constructors, one to be used as default and another to include all attributes. Add a class attribute to keep count of all registered students. Use the class attribute to generate student numbers with the following format 22201XYZ, where XYZ is the current count provided by the class attribute. Implement the respective getters and setters for each attribute. Now given a list of student information create the respective objects and print out their details see samples below, make sure the student class has a toString method.
Sample Run1
2
John Doe 29 M
Kelly Daniela 40 F
Output1:
Full names : John Doe
Age : 29
Gender : M
Student Number : 22201001
Full names : Kelly Daniela
Age : 40
Gender : F
Student Number : 22201002
public class Students {
private String firstName;
private String lastName;
private int age;
private String gender;
private int studentNumber;
public Students(){ }
public Students(String firstName, String lastName, int age, String gender, int studentNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.studentNumber = studentNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(int studentNumber) {
this.studentNumber = studentNumber;
}
@Override
public String toString() {
return "Full names: " + firstName +" " + lastName + "\n" +
"Age: " + age + "\n" +
"Gender: " + gender + "\n" +
"StudentNumber: " + studentNumber;
}
}
public class Attribute {
private int countOfRegisteredUsers = 0;
public int generateStudentNumber(){
countOfRegisteredUsers++;
return 22201000 + countOfRegisteredUsers;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Attribute attribute = new Attribute();
System.out.println("Enter the number of students:");
int n = Integer.parseInt(scanner.nextLine());
System.out.println("Enter student details via space \" \" ");
List<String[]> studentDetails = new ArrayList<>();
for(int i = 0; i < n; i++){
String details = scanner.nextLine();
studentDetails.add(details.split(" "));
System.out.println();
}
List<Students> students = new ArrayList<>();
for(String[] details : studentDetails) {
students.add(new Students(
details[0],
details[1],
Integer.parseInt(details[2]),
details[3],
attribute.generateStudentNumber()));
}
for(Students student : students){
System.out.println(student.toString() + "\n");
}
}
}
Comments
Leave a comment