1. Create a project to model student with firstname, lastname, age, gender and student number. Create two construtors, 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 an 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
import java.util.*;
class Student {
private String firstname;
private String lastname;
private int age;
private String gender;
private int number;
private static int count = 0;
public Student() {
count++;
}
public Student(String firstname, String lastname, int age, String gender) {
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.gender = gender;
count++;
this.number = count;
}
public String toString() {
return "Full names : " + this.firstname + " " + this.lastname + " Age : " + this.age + " Gender : "
+ this.gender + " Student Number : " + String.format("22201%03d", this.number);
}
/**
* @return the firstname
*/
public String getFirstname() {
return firstname;
}
/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}
/**
* @return the lastname
*/
public String getLastname() {
return lastname;
}
/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the gender
*/
public String getGender() {
return gender;
}
/**
* @param gender the gender to set
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* @return the number
*/
public int getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(int number) {
this.number = number;
}
}
class App {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int numberStudents = keyboard.nextInt();
Student students[] = new Student[numberStudents];
keyboard.nextLine();
for (int i = 0; i < numberStudents; i++) {
System.out.print("Enter the first name of the student: ");
String firstname = keyboard.nextLine();
System.out.print("Enter the last name of the student: ");
String lastname = keyboard.nextLine();
System.out.print("Enter the age of the student: ");
int age = keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter the gender of the student: ");
String gender = keyboard.nextLine();
students[i] = new Student(firstname, lastname, age, gender);
}
for (int i = 0; i < numberStudents; i++) {
System.out.println(students[i].toString());
}
keyboard.close();
}
}
Comments
Leave a comment