Question

In: Computer Science

I was wondering why my merger class isn't compiling - Java -------------------------------------------------------------------------------------------------------------------- public class Person{ private...

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);
}

}

}

Solutions

Expert Solution

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 ;
}
}

Related Solutions

I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------...
I was wondering is someone could tell me why my code isn't compiling - Java ------------------------------------------------------------------------------------------------------------ class Robot{ int serialNumber; boolean flies,autonomous,teleoperated; public void setCapabilities(int serialNumber, boolean flies, boolean autonomous, boolean teleoperated){ this.serialNumber = serialNumber; this.flies = flies; this.autonomous = autonomous; this.teleoperated = teleoperated; } public int getSerialNumber(){ return this.serialNumber; } public boolean canFly(){ return this.flies; } public boolean isAutonomous(){ return this.autonomous; } public boolean isTeleoperated(){ return this.teleoperated; } public String getCapabilities(){ StringBuilder str = new StringBuilder(); if(this.flies){str.append("canFly");str.append(" ");} if(this.autonomous){str.append("autonomous");str.append("...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
public class Person { private String name; public Person() { name = "No name yet"; }...
public class Person { private String name; public Person() { name = "No name yet"; } public Person(String initialName) { name = initialName; } public void setName(String newName) { name = newName; } public String getName() { return name; } public void writeOutput() { System.out.println("Name: " + name); } public boolean hasSameName(Person otherPerson) { return this.name.equalsIgnoreCase(otherPerson.name); } } 2- Write a Program Patient. Java Class that extends Person to include  Social security Gender  Appropriate construtors, accessors, and mutators....
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
The following code is included for the java programming problem: public class Bunny {        private...
The following code is included for the java programming problem: public class Bunny {        private int bunnyNum;        public Bunny(int i) {               bunnyNum = i;        }        public void hop() {               System.out.println("Bunny " + bunnyNum + " hops");        } } Create an ArrayList <????> with Bunny as the generic type. Use an index for-loop to build (use .add(….) ) the Bunny ArrayList. From the output below, you need to have 5. Use an index for-loop...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT