Question
You want to track the number of students enrolled. Modify your Student class to include that variable and a method to get it. Your running
You want to track the number of students enrolled. Modify your Student class to include that variable and a method to get it. Your running class should:
1. Display the current number of students enrolled; that is, print the number of students after each instance is created, along with new student information
2. You are to create one more student, taking data from the user.
THE CODE IS CREATED BASED ON THE QUESTION below
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, and a character gender.
The default value for name is an empty string, for age is 0, for the Boolean variable is false, and for the gender is \u0000.
To compute the students age, the year of birth and the current year must be provided.
The students name and gender are set to the values provided.
To set the Boolean value, the students major must be provided. If it is IT, the Boolean variable is set to true; otherwise, it is set to false.
A student can change major. In such situation, the new major must be provided. If it is IT, the Boolean variable is set to true; otherwise, it is set to false.
import java.time.LocalDate;
class Student
{
private String name;
private int age;
private Boolean IT_major;
private char gender;
public static int num; //static variable for number of students
public Student() //default constructor
{
name = " ";
age = 0;
IT_major = false;
gender = '\u0000'; num++;
}
public Student(String name,char gender) //parameterized constructor
{
this.name = name;
this.gender = gender ;
num++;
}
public void setAge(int Birth_year,int Current_year) //set age by sending birth year and current year
{
age = Current_year - Birth_year;
}
public int getAge()
{
return age;
}
public void setIT_Major(String major)
{
IT_major = Is_IT_major(major); //call Is_IT_major()
}
public boolean Is_IT_major(String major) //check if student is IT_major
{
if(major.equalsIgnoreCase("IT")|| major.equalsIgnoreCase("Information Technology"))
return true;
else return false;
}
public Boolean getIT_major()
{
return IT_major;
}
public String toString() //override toString()
{
return " Student Details: Name :"+name+" Age : "+ getAge()+ " gender : "+gender +" Is IT Major : "+getIT_major();
}
}
class Test
{
public static void main (String[] args)
{
LocalDate d = LocalDate.of(2020, 01, 29); // LocalDate object
int Current_year = d.getYear();
Student s = new Student("Christina Lewis",'f');
s.setIT_Major("It");
s.setAge(2000,Current_year);
System.out.println(s.toString());
System.out.println("Number of students :"+s.num);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started