Create a TestStudent class in which you’ll create instances: Create a student in
ID: 3857358 • Letter: C
Question
Create a TestStudent class in which you’ll create instances: Create a student instance with default values Repeatedly asks the user for the student’s name, birthyear, gender, and major, until the user enters “DONE” to indicate no more student to enter For each student entered, create an instance of the Student class. Print the total number of Students in the class Call the toString() method to print the data for each instance.
Related to this question: http://www.chegg.com/homework-help/questions-and-answers/using-java-define-class-students-following-properties-actions-string-name-integer-age-bool-q23080267
Explanation / Answer
public class Student {
private String name;
private int birthyear;
private String major;
private char gender;
public static int numberOfStudents;
public Student() {
// TODO Auto-generated constructor stub
this.name = "";
this.birthyear = 0;
this.major = "";
this.gender = ' ';
numberOfStudents++;
}
/**
* @param name
* @param birthyear
* @param major
* @param gender
*/
public Student(String name, int birthyear, String major, char gender) {
this.name = name;
this.birthyear = birthyear;
this.major = major;
this.gender = gender;
numberOfStudents++;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the birthyear
*/
public int getBirthyear() {
return birthyear;
}
/**
* @return the major
*/
public String getMajor() {
return major;
}
/**
* @return the gender
*/
public char getGender() {
return gender;
}
/**
* @return the num
*/
public static int getNumberOfStudents() {
return numberOfStudents;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param birthyear
* the birthyear to set
*/
public void setBirthyear(int birthyear) {
this.birthyear = birthyear;
}
/**
* @param major
* the major to set
*/
public void setMajor(String major) {
this.major = major;
}
/**
* @param gender
* the gender to set
*/
public void setGender(char gender) {
this.gender = gender;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Name=" + name + ", Birthyear=" + birthyear + ", Major=" + major
+ ", Gender=" + gender + " Number of Students:"
+ numberOfStudents;
}
}
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
Scanner scanner = null;
try {
String name;
int birthyear;
String major;
char gender;
scanner = new Scanner(System.in);
do {
System.out.print("Enter the Student Name:");
name = scanner.next();
if (name.equalsIgnoreCase("DONE")) {
break;
} else {
System.out.print("Enter the Student Birthyear:");
birthyear = scanner.nextInt();
System.out.print("Enter the Student Major:");
major = scanner.next();
System.out
.print("Enter the Student Gender(M-Male/F-Female):");
gender = scanner.next().charAt(0);
Student student = new Student(name, birthyear, major,
gender);
System.out.println(student.toString());
}
} while (true);
} catch (Exception e) {
// TODO: handle exception
}
}
}
OUTPUT:
Enter the Student Name:Srinivas
Enter the Student Birthyear:1990
Enter the Student Major:CSC
Enter the Student Gender(M-Male/F-Female):M
Name=Srinivas, Birthyear=1990, Major=CSC, Gender=M
Number of Students:1
Enter the Student Name:Madhavi
Enter the Student Birthyear:1991
Enter the Student Major:CSC
Enter the Student Gender(M-Male/F-Female):F
Name=Madhavi, Birthyear=1991, Major=CSC, Gender=F
Number of Students:2
Enter the Student Name:done