Define a class of Students with the following properties and actions: a string n
ID: 3858889 • Letter: D
Question
Define a class of Students with the following properties and actions: a string name, an integer age, a Boolean variable indicating whether or not the student is an IT major, a character gender, and an integer that tracks the number of students.
The default value for name is “Jon Doe”, for age is 0, for the Boolean variable is false, and for the gender is ‘u0000’.
To compute the student’s age, the year of birth and the current year must be used. The year of birth is provided as parameter. The current year is a class variable obtained thru an instance of LocalDate.
The student’s name and gender are set to the values provided.
To set the Boolean value, we use a support method that takes the student’s major and return true if the major is “IT”, and false otherwise. The setter for this data field must call the support method. Note that the parameter provided could be any case-variation of “IT” (e.g. “it”, “It”, “iT”), or any case-variation of “Information Technology”.
Create an instance method called toString(), that will use the getters and setters to create a String of the instance data (Note that this is similar to the displayInfo() method, except that the data is not printed)
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.
(For java eclipse please)
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.july;
import java.util.Calendar;
/**
*
* @author Sam
*/
public class Students {
private String name;
private int age;
private boolean itMajor;
private char gender;
private static int numberOfStudents = 0;
public Students() {
name = "Jon Doe";
age = 0;
itMajor = false;
gender = 'u0000';
numberOfStudents++;
}
public Students(String name, int yearOfBirth, String major, char gender) {
this.name = name;
this.gender = gender;
this.age = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth;
this.itMajor = major.equalsIgnoreCase("IT") || major.equalsIgnoreCase("Information Technology");
}
public void setAge (int yearOfBirth) {
age = Calendar.getInstance().get(Calendar.YEAR) - yearOfBirth;
}
public void setItMajor(String majorSubject) {
itMajor = majorSubject.equalsIgnoreCase("IT") || majorSubject.equalsIgnoreCase("Information Technology");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public boolean isItMajor() {
return itMajor;
}
public static int getNumberOfStudents() {
return numberOfStudents;
}
@Override
public String toString() {
return "Students{" + "name=" + getName() + ", age=" + getAge() + ", itMajor=" + isItMajor() + ", gender=" + getGender() + '}';
}
}
class TestStudents {
public static void main(String[] args) {
Students s1 = new Students();
System.out.println("Student is " + s1);
s1.setName("Alpha Beta");
System.out.println("Student is " + s1);
s1.setGender('M');
System.out.println("Student is " + s1);
s1.setGender('M');
System.out.println("Student is " + s1);
s1.setItMajor("Computer Science");
System.out.println("Student is " + s1);
s1.setAge(1993);
System.out.println("Student is " + s1);
}
}
this should satisfy all your needs. let me know if you need anything more....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.