Answer to Question #311444 in Java | JSP | JSF for NUCHO

Question #311444

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


1
Expert's answer
2022-03-14T10:30:54-0400
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 + "\nAge : " + this.age + "\nGender : "
				+ this.gender + "\nStudent 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);
		int numberStudents = keyboard.nextInt();
		Student students[] = new Student[numberStudents];
		keyboard.nextLine();
		for (int i = 0; i < numberStudents; i++) {
			String values[] = keyboard.nextLine().split(" ");
			students[i] = new Student(values[0], values[1], Integer.parseInt(values[2]), values[3]);
		}
		for (int i = 0; i < numberStudents; i++) {
			System.out.println(students[i].toString());
		}


		keyboard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS