user3002486
user3002486

Reputation: 431

Constructor Inheritance getting fatal error on new object

I have 3 classes: administrator, salaried employee, and employee. Administrator extends from salaried employee and that extends from employee. Here are their default constructors:

Employee:

public class Employee
{
private String name;
private Date hireDate;

public Employee( )
{
     name = "No name";
     hireDate = new Date("Jan", 1, 1000); //Just a placeholder.
}

/**
 Precondition: Neither theName nor theDate is null.
*/
public Employee(String theName, Date theDate)
{
    if (theName == null || theDate == null)
    {
         System.out.println("Fatal Error creating employee.");
         System.exit(0);
    }
    name = theName;
    hireDate = new Date(theDate);
}

public Employee(Employee originalObject)
{
     name = originalObject.name;
     hireDate = new Date(originalObject.hireDate);
}


}

SalariedEmployee:

public class SalariedEmployee extends Employee
{
private double salary; //annual

public SalariedEmployee( )
{
    super( );
    salary = 0;
}

/**
 Precondition: Neither theName nor theDate are null; 
 theSalary is nonnegative.
*/
public SalariedEmployee(String theName, Date theDate, double theSalary)
{
     super(theName, theDate);
     if (theSalary >= 0)
         salary = theSalary;
     else
     {
         System.out.println("Fatal Error: Negative salary.");
         System.exit(0);
     }
}

public SalariedEmployee(SalariedEmployee originalObject )
{
     super(originalObject);
     salary = originalObject.salary;
}

}

Administrator:

//Create a test program that uses scanner so person can input values for the admin class. 
public class Administrator extends SalariedEmployee
{
private String title;
private String responsibility;
private String supervisor;

//No argument Constructor
public Administrator( )
{
    super( );
    title = "No Title";
    responsibility = "No Responsibility";
    supervisor = "Dilbert";
}

//3 argument constructor
public Administrator(String theTitle, String theResponsibility, String theSupervisor)
{
     super( );
     if (theTitle == null || theResponsibility == null || theSupervisor == null)
     {
          System.out.println("Fatal Error creating employee.");
          System.exit(0);
     }
     title = theTitle;
     responsibility = theResponsibility;
     supervisor = theSupervisor;
}

//6 argument Constructor
public Administrator(String theName, Date theDate, double theSalary, String theTitle, String theResponsibility, String theSupervisor)
{
     super(theName,theDate,theSalary);
     title = theTitle;
     responsibility = theResponsibility;
     supervisor = theSupervisor;
}

}

I try to create a new administrator object using:

Administrator admin = new Administrator();

But I get a fatal error. What am I doing wrong?

Upvotes: 0

Views: 177

Answers (1)

user1071777
user1071777

Reputation: 1307

Your Date class outputs "Fatal Error" everywhere in an non-informative way (which we had no idea of until you posted your Date class code).

The first time you get Fatal Error is inside the setDate method because dateOK returned false for "Jan", 1, 1000.

Your monthOK method is expecting "January", so the "Jan" you're using for your default constructor is invalid. Change your default constructor to use

hireDate = new Date("January", 1, 1000);

Upvotes: 2

Related Questions