For the class of students defined in the previous activity, create two instances
ID: 3813387 • Letter: F
Question
For the class of students defined in the previous activity, create two instances: The first instance must be created with default values. Then take the information from the user and set the data fields values. The second instance must be created with the following initial values: name=“John Doe”, age = 20, gender = ‘F’, major=“CSE” Then change the major of the second instance to “IT” Note: Write these instances in a comment block, as they are not to be tested. This must be finished in Java. See code to add to below:
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 = "Jon Doe";
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(2017, 04, 07); // 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);
}
}
Explanation / Answer
Hi
I have updated the code and highlighted the code changes below. And also please note that i hvae commented the code that i added as per your instructions. Please find the below
import java.time.LocalDate;
import java.util.Scanner;
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 = "Jon Doe";
age = 0;
IT_major = false;
gender = 'u0000';
num++;
}
public Student(String name,char gender) //parameterized constructor
{
this.name = name;
this.gender = gender ;
num++;
}
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 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(2017, 04, 07); // 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);
/*
Student s1 = new Student();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the student name: ");
String name = scan.nextLine();
System.out.println("Enter the gender: ");
char gender = scan.next().charAt(0);
System.out.println("Enter the major: ");
String major = scan.next();
System.out.println("Enter the year: ");
int year = scan.nextInt();
s1.setAge(year, Current_year);
s1.setIT_Major(major);
s1.setGender(gender);
s1.setName(name);
System.out.println(s1.toString());
Student s2 = new Student("John Doe", 'F' );
s2.setAge(20, Current_year);
s2.setIT_Major("CSE");
System.out.println(s2.toString());
s2.setIT_Major("IT");
System.out.println(s2.toString());
*/
}
}