(Java) Make sure output match! Take a look at the following code: public class Employee extends Person { private static int numEmployees = 0; private double salary; private String title; public Employee() { super(); this.title = ""; this.salary=0.0; } public Employee(String name, int age, String gender, Address a, double salary, String title) { super(name,age,gender,a); this.salary = salary; this.title = title; } public double getSalary() { return this.salary; } public String getTitle() { return this.title; } public static int getNumEmployees() { return numEmployees; } public void setSalary(double salary) { this.salary = salary; } public void setTitle(String title) { this.title = title; } public static void updateNumEmployees() { numEmployees += 1; } @Override public String toString() { return "Name: "+this.getName()+"\n" + "Age: "+this.getAge()+"\n" + "Gender: "+this.getGender()+"\n" + "Address: "+this.getAddress().getStreetNumber()+" "+this.getAddress().getStreetName()+"\n" + "Salary: $"+this.getSalary()+"\n" + "Title: "+this.getTitle(); } } We are going to alter the Employee constructors so that one constructor calls this() and one constructor calls super() Recall that this() calls the constructor of the same class When you call super(), you are calling the constructor of the parent class. Specifically, we are going to alter the default constructor for the Employee class to call this() instead of super() Open up this method, remove the call to super() and replace it with a call to this() to call the multi-argument constructor. /** * Default constructor for the * Employee class. Calls the * multi-argument constructor of the * this class */ public Employee() { this(//fill in here); } Note that your call to this(....) will call the below constructor: public Employee(String name, int age, String gender, Address a, double salary, String title) You will need to pass in 6 arguments: "Name unknown" 0 "Gender unknown" new Address() 0.0 "Title unknown" Now, copy and paste the below code into EmployeeTest.java to verify that your default constructor works properly: /** * EmployeeTest.java * @author * CIS 36B, Activity 12.1 */ import java.util.Scanner; public class EmployeeTest { public static void main(String[] args) { System.out.println("***Calling the Employee default constructor***"); Employee E = new Employee(); System.out.println(E); } } Required Output: ***Calling the Employee default constructor*** Name: Name unknown Age: 0 Gender: Gender unknown Address: 0 Street unknown Salary: $0.0 Title: Title unknown When your program is giving the required output as shown above, upload Employee.java
(Java)
Make sure output match!
Take a look at the following code:
public class Employee extends Person {
private static int numEmployees = 0;
private double salary;
private String title;
public Employee() {
super();
this.title = "";
this.salary=0.0;
}
public Employee(String name, int age, String gender, Address a, double salary, String title) {
super(name,age,gender,a);
this.salary = salary;
this.title = title;
}
public double getSalary() {
return this.salary;
}
public String getTitle() {
return this.title;
}
public static int getNumEmployees() {
return numEmployees;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void setTitle(String title) {
this.title = title;
}
public static void updateNumEmployees() {
numEmployees += 1;
}
@Override public String toString() {
return "Name: "+this.getName()+"\n" +
"Age: "+this.getAge()+"\n" +
"Gender: "+this.getGender()+"\n" +
"Address: "+this.getAddress().getStreetNumber()+" "+this.getAddress().getStreetName()+"\n" +
"Salary: $"+this.getSalary()+"\n" +
"Title: "+this.getTitle();
}
}
- We are going to alter the Employee constructors so that one constructor calls this() and one constructor calls super()
- Recall that this() calls the constructor of the same class
- When you call super(), you are calling the constructor of the parent class.
- Specifically, we are going to alter the default constructor for the Employee class to call this() instead of super()
- Open up this method, remove the call to super() and replace it with a call to this() to call the multi-argument constructor.
* Default constructor for the
* Employee class. Calls the
* multi-argument constructor of the
* this class
*/
public Employee() {
this(//fill in here);
}
- Note that your call to this(....) will call the below constructor:
- You will need to pass in 6 arguments:
- "Name unknown"
- 0
- "Gender unknown"
- new Address()
- 0.0
- "Title unknown"
- Now, copy and paste the below code into EmployeeTest.java to verify that your default constructor works properly:
* EmployeeTest.java
* @author
* CIS 36B, Activity 12.1
*/
import java.util.Scanner;
public class EmployeeTest {
public static void main(String[] args) {
System.out.println("***Calling the Employee default constructor***");
Employee E = new Employee();
System.out.println(E);
}
}
Required Output:
Name: Name unknown
Age: 0
Gender: Gender unknown
Address: 0 Street unknown
Salary: $0.0
Title: Title unknown
- When your program is giving the required output as shown above, upload Employee.java
I have defined a default constructor in the class Address that will call the parameterized constructor of the same class and initialize the variables of Address type.
Next, in the class Employee(), I have defined the default constructor.
Inside the default constructor, I have passed this() with six arguments, that will call the parameterized constructor of the same class.
Next, in the EmployeeTest class I have created an object of the class Employee and then printed it to check the output.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images