In: Computer Science
I was wondering why my merger class isn't compiling - Java
--------------------------------------------------------------------------------------------------------------------
public class Person{
private String firstName;
private String lastName;
private int age;
private String email;
private String phone;
private String address;
public Person(String firstName, String lastName, int age, String
email, String phone, String address){
setFirstName(firstName);
setLastName(lastName);
setAge(age);
setEmail(email);
setPhone(phone);
setAddress(address);
}
public Person(String firstName, String lastName, String
email){
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public int getAge(){
return age;
}
public String getEmail(){
return email;
}
public String getPhone(){
return phone;
}
public String getAddress(){
return address;
}
public void setFirstName(String firstName){
if(firstName == null || firstName.length() == 0){
this.firstName = "N/A";
System.out.println("Invalid First Name");
}
else{
this.firstName = firstName;
}
}
public void setLastName(String lastName){
if(lastName == null || lastName.length() == 0){
this.firstName = "N/A";
System.out.println("Invalid Last Name");
}
else{
this.lastName = lastName;
}
}
public void setAge(int age){
this.age = age;
}
public void setEmail(String email){
if(email == null || email.length() == 0){
this.firstName = "N/A";
System.out.println("Invalid email");
}
else{
this.email = email;
}
}
public void setPhone(String phone){
if(phone == null || phone.length() == 0){
this.phone = "N/A";
System.out.println("Invalid phone");
}
else{
this.phone = phone;
}
}
public void setAddress(String address){
if(address == null || address.length() == 0){
this.address = "N/A";
System.out.println("Invalid address");
}
else{
this.address = address;
}
}
public String toString(){
return "Person: First Name = " + firstName + ", Last Name = " +
lastName + ", Age = " + age + ", Email = " + email
+ ", Phone = " + phone + ", Address = " + address;
}
public static void main(String args[]){
Person p = new Person("John", "Doe", 23, "[email protected]",
"703-416-2478", "George Mason University, Fairfax,
VA-22030");
System.out.println(p);
System.out.println();
Person p2 = new Person("Adam", "Smith", "[email protected]");
System.out.println(p2);
}
}
--------------------------------------------------------------------------------------------------------------------
public class Student extends Person{
private String department;
private float completedCredits;
private float remainingCredits;
private float cgpa;
private int startYear;
private int expectedEndYear;
public Student(String firstName, String lastName, int age,
String email, String phone, String address) {
super(firstName, lastName, age, email, phone, address);
}
public Student(String firstName, String lastName, String email)
{
super(firstName, lastName, email);
}
public String getDepartment() {
return department;
}
public float getCompletedCredits() {
return completedCredits;
}
public float getRemainingCredits() {
return remainingCredits;
}
public float getCgpa() {
return cgpa;
}
public int getStartYear() {
return startYear;
}
public int getExpectedEndYear() {
return expectedEndYear;
}
public void setDepartment(String department) {
this.department = department;
}
public void setCompletedCredits(float completedCredits) {
this.completedCredits = completedCredits;
}
public void setRemainingCredits(float remainingCredits) {
this.remainingCredits = remainingCredits;
}
public void setCgpa(float cgpa) {
this.cgpa = cgpa;
}
public void setStartYear(int startYear) {
this.startYear = startYear;
}
public void setExpectedEndYear(int expectedEndYear) {
this.expectedEndYear = expectedEndYear;
}
public String toString() {
return super.toString() + ",\n Department = " + department
+
",\n CompletedCredits = " + completedCredits +
",\n RemainingCredits = " + remainingCredits +
",\n Cgpa = " + cgpa +
",\n StartYear = " + startYear +
",\n ExpectedEndYear = " + expectedEndYear + "\n";
}
}
--------------------------------------------------------------------------------------------------------------------
public class Undergraduate extends Student {
private int numOfProjectsDone;
public Undergraduate(String firstName, String lastName, int age,
String email, String phone, String address) {
super(firstName, lastName, age, email, phone, address);
}
public Undergraduate(String firstName, String lastName, String
email) {
super(firstName, lastName, email);
}
public int getNumOfProjectsDone() {
return numOfProjectsDone;
}
public void setNumOfProjectsDone(int numOfProjectsDone) {
this.numOfProjectsDone = numOfProjectsDone;
}
public String toString() {
return super.toString() + ",\n NumOfProjectsDone = " +
numOfProjectsDone;
}
}
--------------------------------------------------------------------------------------------------------------------
public class Graduate extends Student {
private String thesis;
private String adviserName;
public Graduate(String firstName, String lastName, int age, String
email, String phone, String address) {
super(firstName, lastName, age, email, phone, address);
}
public Graduate(String firstName, String lastName, String email)
{
super(firstName, lastName, email);
}
public String getThesis() {
return thesis;
}
public String getAdviserName() {
return adviserName;
}
public void setThesis(String thesis) {
this.thesis = thesis;
}
public void setAdviserName(String adviserName) {
this.adviserName = adviserName;
}
public String toString() {
return super.toString() + ",\n Thesis = " + thesis + ",\n
AdviserName='" + adviserName ;
}
}
--------------------------------------------------------------------------------------------------------------------
public class MergeTwoArrays{
public static Student[] mergeArrays(Student[] array1, Student[]
array2){
Student[] mArray = new Student[array1.length +
array2.length];
int i = 0;
int j = 0;
int k = 0;
while(i < array1.length && j <
array2.length){
if (array1[i].getCgpa() > array2[j].getCgpa()){
mArray[k++] = array1[i++];
}
else{
mArray[k++] = array2[j++];
}
}
while (i < array1.length){
mArray[k++] = array1[i++];
}
while (j < array2.length){
mArray[k++] = array2[j++];
}
return mArray;
}
public static void main(String args[]){
Student s1 = new Student("ali", "beck", 23, "[email protected]",
"755-123-4123", "home",
"Computer Science", (float)(18), (float)(102), (float)(3.88), 2017,
2021);
Student s2 = new Student("ali1", "beck1", 23, "[email protected]",
"755-123-4123", "home1",
"Computer Science", (float)(18), (float)(102), (float)(3.86), 2017,
2021);
Student s3 = new Student("ali2", "beck2", 23, "[email protected]",
"755-123-4123", "home2",
"Computer Science", (float)(18), (float)(102), (float)(3.97), 2017,
2021);
Undergraduate u1 = new Undergraduate("ali3", "beck3", 23,
"[email protected]", "755-123-4123", "home3",
"Computer Science", (float)(18), (float)(102), (float)(3.85), 2017,
2021, 4);
Undergraduate u2 = new Undergraduate("ali4", "beck4", 23,
"[email protected]", "755-123-4123", "home4",
"Computer Science", (float)(18), (float)(102), (float)(3.87), 2017,
2021, 4);
Undergraduate u3 = new Undergraduate("ali5", "beck5", 23,
"[email protected]", "755-123-4123", "home5",
"Computer Science", (float)(18), (float)(102), (float)(3.76), 2017,
2021, 4);
Graduate g1 = new Graduate("ali6", "beck6", 23, "[email protected]",
"755-123-4123", "home6",
"Computer Science", (float)(18), (float)(102), (float)(3.83), 2017,
2021, "Machine Learning", "Mr. X");
Graduate g2 = new Graduate("ali7", "beck7", 23, "[email protected]",
"755-123-4123", "home7",
"Computer Science", (float)(18), (float)(102), (float)(3.74), 2017,
2021, "Machine Learning", "Mr. X");
Graduate g3 = new Graduate("ali8", "beck8", 23, "[email protected]",
"755-123-4123", "home8",
"Computer Science", (float)(18), (float)(102), (float)(3.72), 2017,
2021, "Machine Learning", "Mr. X");
Student[] array1 = {s1, s2, u1, g1};
Student[] array2 = {s3, u2, u3, g2, g3};
System.out.println("\n------------------ Printing Array1 (sorted in
descending order of CGPA) ---------------------");
for (Student p: array1){
System.out.println(p);
}
System.out.println("\n------------------ Printing Array2 (sorted
in descending order of CGPA) ---------------------");
for (Student p: array2){
System.out.println(p);
}
Student[] mArray = mergeArrays(array1, array2);
System.out.println("\n------------------ Printing Merged Array
(sorted in descending order of CGPA) ---------------------");
for (Student p: mArray){
System.out.println(p);
}
}
}
Hey,
in your merger class you have initialized the constructor with all the parameters, but forgot to initialize in the constructor of Student class,
This is the case for all the objects created for Graduate, UnderGraduate, you have to declare the parameters in the constructor of these classes(Graduate and Undergraduate) and have to set the values which are local to the classes(like thesis for Graduate class)
So modify the constructors in Student, Graduate and Undergraduate class.
I have added the code down below.
public class Student extends Person{
private String department;
private float completedCredits;
private float remainingCredits;
private float cgpa;
private int startYear;
private int expectedEndYear;
public Student(String firstName, String lastName, int age, String email, String phone, String address,
String department, float completedCredits, float remainingCredits, float cgpa, int startYear, int expectedEndYear ) {
super(firstName, lastName, age, email, phone, address);
setDepartment(department); //initializing the local datas of student
setCompletedCredits(completedCredits);
setRemainingCredits(remainingCredits);
setCgpa(cgpa);
setStartYear(startYear);
setExpectedEndYear(expectedEndYear);
}
public Student(String firstName, String lastName, String email) {
super(firstName, lastName, email);
}
public String getDepartment() {
return department;
}
public float getCompletedCredits() {
return completedCredits;
}
public float getRemainingCredits() {
return remainingCredits;
}
public float getCgpa() {
return cgpa;
}
public int getStartYear() {
return startYear;
}
public int getExpectedEndYear() {
return expectedEndYear;
}
public void setDepartment(String department) {
this.department = department;
}
public void setCompletedCredits(float completedCredits) {
this.completedCredits = completedCredits;
}
public void setRemainingCredits(float remainingCredits) {
this.remainingCredits = remainingCredits;
}
public void setCgpa(float cgpa) {
this.cgpa = cgpa;
}
public void setStartYear(int startYear) {
this.startYear = startYear;
}
public void setExpectedEndYear(int expectedEndYear) {
this.expectedEndYear = expectedEndYear;
}
public String toString() {
return super.toString() + ",\n Department = " + department +
",\n CompletedCredits = " + completedCredits +
",\n RemainingCredits = " + remainingCredits +
",\n Cgpa = " + cgpa +
",\n StartYear = " + startYear +
",\n ExpectedEndYear = " + expectedEndYear + "\n";
}
}
public class Undergraduate extends Student {
private int numOfProjectsDone;
public Undergraduate(String firstName, String lastName, int age, String email, String phone, String address,
String department, float completedCredits, float remainingCredits, float cgpa, int startYear, int expectedEndYear,
int numOfProjectsDone) {
super(firstName, lastName, age, email, phone, address,department, completedCredits, remainingCredits, cgpa, startYear,expectedEndYear);
setNumOfProjectsDone(numOfProjectsDone);
}
public Undergraduate(String firstName, String lastName, String email) {
super(firstName, lastName, email);
}
public int getNumOfProjectsDone() {
return numOfProjectsDone;
}
public void setNumOfProjectsDone(int numOfProjectsDone) {
this.numOfProjectsDone = numOfProjectsDone;
}
public String toString() {
return super.toString() + ",\n NumOfProjectsDone = " + numOfProjectsDone;
}
}
public class Graduate extends Student {
private String thesis;
private String adviserName;
public Graduate(String firstName, String lastName, int age, String email, String phone, String address,
String department, float completedCredits, float remainingCredits, float cgpa, int startYear, int expectedEndYear,
String thesis, String adviserName) {
super(firstName, lastName, age, email, phone, address,department, completedCredits, remainingCredits, cgpa, startYear,expectedEndYear);
setThesis(thesis);
setAdviserName(adviserName);
}
public Graduate(String firstName, String lastName, String email) {
super(firstName, lastName, email);
}
public String getThesis() {
return thesis;
}
public String getAdviserName() {
return adviserName;
}
public void setThesis(String thesis) {
this.thesis = thesis;
}
public void setAdviserName(String adviserName) {
this.adviserName = adviserName;
}
public String toString() {
return super.toString() + ",\n Thesis = " + thesis + ",\n AdviserName='" + adviserName ;
}
}