1. Create a new Employee class with the following characteristics: • Attributes
ID: 3633224 • Letter: 1
Question
1. Create a new Employee class with the following characteristics:
• Attributes (private instance variables)
– A String, name (the name of the Employee)
– An int, ID (the ID number of the Employee; should be greater than or equal to 0)
– An int, age (the age of the Employee; check for a reasonable age range)
– A double, salary (the salary of the Employee; again, check for reasonableness)
– A String, title (the Employee's title)
– A String, dept (the department name the Employee belongs to)
• Constructors
– A no-argument constructor (sets all attributes to "empty" ("") or 0 values)
– A 6-argument constructor with parameters name, ID, age, salary, title, &
dept
• Behaviors (methods)
– Create a getter method for salary
– Create a void print() method that prints all the attributes for an Employee
2. Create a subclass of Employee called Manager; a Manager is an Employee who manages other Employees. A Manager inherits all Employee information with the following changes:
• Attributes (private instance variables)
– A new int[], emps (the set of ID numbers of Employees the Manager manages)
• Constructors
– A no-argument constructor (sets all attributes to "empty" or 0 values)
– A 7-argument constructor with parameters name, ID, age, salary, title, dept, emps
• Behaviors (methods)
– Override the void print() method so that it prints all the attributes for a Manager
Explanation / Answer
I think this pretty much follows your requirements. Although one thing bugging me is that you're told to check for valid input in Id, age and salary but you're not told what to do with it if it's invalid input. So all I did was have the program print an error message to the console. Any more error handling can be added after that. I hope this helps. Please remember to rate :)
Employee.java is the following:
public class Employee {
// Attributes
private String name;
private int ID;
private int age;
private double salary;
private String title;
private String dept;
// Constructors
public Employee()
{
name = "";
ID = 0;
age = 0;
salary = 0;
title = "";
dept = "";
}
public Employee(String n, int i, int a, double s, String t, String d)
{
name = n;
if(i >= 0)
ID = i;
else
System.err.println("Invalid ID number!");
if( (a >= 16) && (a <= 90 ) )
age = a;
else
System.err.println("Invalid Age value!");
if( (s >= 100) )
salary = s;
else
System.err.println("Invalid Salary value!");
title = t;
dept = d;
}
// Behaviors
public double getSalary()
{
return salary;
}
public void print()
{
System.out.println("Employee Information: ");
System.out.println(" Name: " + name);
System.out.println(" ID: " + ID);
System.out.println(" Age: " + age);
System.out.println(" Salary: $ " + salary);
System.out.println(" Title: " + title);
System.out.println(" Dept.: " + dept);
}
}
Manager.java class is as follows:
public class Manager extends Employee {
//Attributes
private int[] emps;
//Constructors
public Manager()
{
super();
emps = new int[0];
}
public Manager(String n, int i, int a, double s, String t, String d, int[] e)
{
super(n, i, a, s, t, d);
emps = e;
}
// Behaviors
//Overriding the print method from superclass Eployee
@Override
public void print() {
super.print();
System.out.println("ID# of Employees managed:");
for(int i = 0; i < emps.length; i++)
{
System.out.println("ID#: " + emps[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.