Declaring a Manager class that inherits from the Employee class (Java) - java

I'm a beginner in Java and I need help with how I can declare a Manager class that inherits from the Employee class and adds a bonus instance variable to store a salary bonus. I have this code that I can work around and a test program that I can supply to test it. Any suggestions on how to implement the constructors and methods here
public class Demo {
class Employee {
int id;
public Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class Manager extends Employee {
double bonus;
public Manager() {
super();
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Manager m = new Manager();
m.setId(1);
m.setBonus(100);
System.out.println("Manger has an ID of " + m.getId() + " with a bonus of " + m.getBonus());
}
}

It will look like below. Study it, and see if you can understand it.
class Employee {
int id;
public Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class Manager extends Employee {
double bonus;
public Manager() {
super();
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
}
public class Demo {
public static void main(String[] args) {
Manager m = new Manager();
m.setId(1);
m.setBonus(100);
System.out.println("Manger has an ID of " + m.getId() + " with a bonus of " + m.getBonus());
}
}

Lets start with 3 fields id,name and Salary for Employee and manager gets an additional field bonus.The salary for Manager will have bonus component added to the salary.
For Employee class create an argument constructor and set all the fields.Create getters for the fields required.
public class Employee {
protected int id;
protected String Name;
protected double Salary;
public Employee(int id, String name, double salary) {
this.id = id;
this.Name = name;
this.Salary = salary;
}
public int getId() {
return id;
}
public String getName() {
return Name;
}
public double getSalary() {
return Salary;
}
}
The Manager class now will extend Employee class.Based on the requirement the only difference between employee and manager is the salary component.Since all the fields in Employee class is set as protected and we extend the Employee class the state of the field can be changed in the child class.
In the Constructor of Manager class the salary will be calculated as salary + bonus.
public class Manager extends Employee {
private double bonus;
public Manager(int id, String name, long salary,double bonus) {
super(id, name, salary);
this.bonus = bonus;
this.Salary = salary + this.bonus;
}
public static void main(String[] args)
{
Manager mng = new Manager(1,"Manager",10000,100);
Employee emp = new Employee(2,"Employer",10000);
System.out.println("MANAGER SALARY " + mng.getSalary()+" "+mng.getId());
System.out.println("EMPLOYER SALARY " + emp.getSalary()+" "+emp.getId());
}
}

Related

I need to create a class that represents an employee. if the user types nothing then it should output "(not set)"

Create a class that represents an employee. This class will have three constructors to initialize variables. If the constructor doesn't provide a parameter for a field, make it either "(not set)" or "0" as appropriate.
Name:
Employee
Fields:
name : String
idNumber : int
department : String
position : String
Methods:
Employee()
Employee(name : String, idNumber : int)
Employee(name : String, idNumber : int, department : String, position : String)
getName() : String
getDepartment() : String
getPosition() : String
getIdNumber() : int
My issue is that I don't know if the Employee() field is a void
Also not sure why public class Employee is included in the comment rather than the code.
public class Employee {
private String name;
private int idNumber;
private string department;
private string position;
/* public void setEmployee (String n)
{
name = n;
if (n == null)
{
n = "(not set)";
}
}
public void Employee (string n, int id)
{
name = n;
idNumber =id;
}
public void Employee (string n, int id, string d, string p)
{
name = n;
idNumber = id;
department = d;
position = p;
} */
public String getName ()
{
return name;
}
public string getDepartment ()
{
return department;
}
public string getPosition ()
{
return position;
}
public int getIdNumber ()
{
return idNumber;
}
}
Also having an issue with this second part
Not sure where to go from here
import java.util.Scanner;
public class EmployeeDem {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String option;
do {
System.out.println("-- Employee ID Menu --\n Enter 'exit' to quit");
option = keyboard.nextLine();
System.out.println("What is your name?");
String name = keyboard.nextLine();
System.out.println("What is your ID number?");
int idNumber = keyboard.nextInt();
System.out.println("What is your department?");
String department = keyboard.nextLine();
System.out.println("What is your position?");
String position = keyboard.nextLine();
} while (option != "exit");
//class obj instance
Employee myEmployee = new Employee();
myEmployee.
}
}
You can initialize the variables first when creating the class, if you don't change them using setters or constructors they will have the initial value that you setted. I initialized them with "(not set)" and 0
public class Employee {
private String name = "(not set)";
private int idNumber = 0;
private String department = "(not set)";
private String position = "(not set)";
public Employee() {}
public Employee(String name, int idNumber) {
this.name = name;
this.idNumber = idNumber;
}
public Employee(String name, int idNumber, String position) {
this.name = name;
this.idNumber = idNumber;
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
Hope this is what you want :)

Do child/sub classes have to be in their own separate file in order to test? [duplicate]

This question already has answers here:
Why is each public class in a separate file?
(11 answers)
Closed 3 years ago.
I am using Eclipse to run this code program to test a Person class and its subclasses. In Eclipse it shows there are errors--that each child class must be defined in its own file.
I am learning Java, and would like to know if this is a must? Or can I make it work with parent and child classes all in one file? If I'm missing something, please point me in the right direction. Thank you!
Here is my code: [I put this is all in one file on Eclipse]
import java.util.*;
//Test program to test Person class and its subclasses
public class Test {
public static void main(String[] args) {
Person person = new Person("person");
Student student = new Student ("student");
Employee employee = new Employee("employee");
Faculty faculty = new Faculty("faculty");
Staff staff = new Staff("staff");
//invoke toString() methods
System.out.println(person.toString());
System.out.println(student.toString());
System.out.println(employee.toString());
System.out.println(faculty.toString());
System.out.println(staff.toString());
}
}
//Defining class Person
public class Person {
protected String name;
protected String address;
protected String phoneNum;
protected String email;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress () {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getEmail() {
return email;
}
public void setEmail (String email) {
this.email = email;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Student extends Person
public class Student extends Person {
public static final String FRESHMAN = "freshman";
public static final String SOPHMORE = "sophmore";
public static final String JUNIOR = "junior";
public static final String SENIOR = "senior";
protected String classStatus;
public Student(String name) {
super(name);
}
public Student(String name, String classStatus) {
super(name);
this.classStatus = classStatus;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Employee extends Person
public class Employee extends Person {
protected double salary;
protected String office;
protected MyDate dateHired;
public Employee(String name) {
this(name, 0, "none", new MyDate());
}
public Employee(String name, double salary, String office, MyDate dateHired) {
super(name);
this.salary = salary;
this.office = office;
this.dateHired - dateHired;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getOffice() {
return office;
}
public void setOffice (String office) {
this.office = office;
}
public MyDate getDateHired() {
return dateHired;
}
public void setDateHired(MyDate dateHired) {
this.dateHired = dateHired;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:" + this.getClass().getName();
}
}
//Defines class Faculty extends Employee
public class Faculty extends Employee {
public static String LECTURER = "lecturer";
public static String ASSISTANT_PROFESSOR = "assistant professor";
public static String ASSOCIATE_PROFESSOR + "associate professor";
public static PROFESSOR = "professor";
protected String officeHours;
protected String rank;
public Faculty(String name) {
this(name, "9-5 PM", "Employee");
}
public Faculty(String name, String officeHours, String rank) {
super(name);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours() {
return officeHours;
}
public void setOfficeHours(String officeHours) {
this.officeHours = officeHours;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank=rank;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Defines class Staff extends Employee
public class Staff extends Employee {
protected String title;
public Staff(String name) {
this(name, "none");
}
public Staff(String name, String title) {
super(name);
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#Override
public String toString() {
return "Name:"+getName()+"Class:"+this.getClass().getName();
}
}
//Define class MyDate
public class MyDate {
private int month, day, year;
public MyDate (int month, int day, int year) {
this.day=day;
this.month=month;
this.year=year;
}
}
Yes, there should be one class per file. Moreover, you are using the MyDate class in the Employee class, which you need to extend and you cannot extends more than one class, so it's better use the predefined Date class which is present java.util.Date. Import this in the Employee class.
import java.util.Date;
instead of this:
public Employee(String name, double salary, String office, MyDate dateHired)
use:
public Employee(String name, double salary, String office, Date dateHired)
There are some careless mistakes:
in Employee class
public static String ASSOCIATE_PROFESSOR + "associate professor";
change to:
public static String ASSOCIATE_PROFESSOR = "associate professor";
Similarly in faculty class
public static String ASSOCIATE_PROFESSOR + "associate professor";
put = instead of +.
Now this code will work.
Yes it is a must. One class per file. Class can have inner classes. You can define subclasses as inner classes. But I recommend putting them in separate files and don't use inner classes.

How to fix Syntax error, insert "... VariableDeclaratorId..."

I am following a question but I am getting this error. I am not sure how to fix it, I have looked up this error but i can't relate it to my code. Would appreciate it any help given!
import java.util.Scanner;
public class Employee {
private static String name;
private static String job;
private static float salary;
private static int id;
public Employee() {
Scanner scan = new Scanner(System.in);
name = scan.nextLine();
job = scan.nextLine();
salary = scan.nextFloat();
id = scan.nextInt();
}
public Employee(String name, String job, float salary, int id) {
name = name;
job = job;
salary = salary;
id = id;
}
public String getName(name) {
return name;
}
public String getJobTitle(job) {
return job;
}
public float salary(salary){
return salary;
}
public static void showEmp() {
System.out.println("Employee is called: " + name);
System.out.println("They are a " +job);
System.out.println("Their salary is " + salary);
System.out.println("Their Employee ID number is " + id);
}
}
Your getter should not have a paramter:
public String getName() {
return name;
}
The variables should not be static also the showEmp method.
Full code should look like:
import java.util.Scanner;
public class Employee {
private String name;
private String job;
private float salary;
private int id;
public Employee() {
Scanner scan = new Scanner(System.in);
name = scan.nextLine();
job = scan.nextLine();
salary = scan.nextFloat();
id = scan.nextInt();
}
public Employee(String name, String job, float salary, int id) {
this.name = name;
this.job = job;
this.salary = salary;
this.id = id;
}
public String getName() {
return name;
}
public String getJobTitle() {
return job;
}
public float salary() {
return salary;
}
public void showEmp() {
System.out.println("Employee is called: " + name);
System.out.println("They are a " + job);
System.out.println("Their salary is " + salary);
System.out.println("Their Employee ID number is " + id);
}
}

implementation of inheritance in arraylist

addEmployee – This method will take Employee reference as parameter and add the same to the employees list after checking if employee with same id does not exist. It will return total employees count if addition is successful, else return -1.
public class Employee {
private int empId;
private String name;
private double basicPay;
private double perksPay;
public Employee()
{
}
public Employee(int empId, String name, double basicPay, double perksPay) {
super();
this.empId = empId;
this.name = name;
this.basicPay = basicPay;
this.perksPay = perksPay;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBasicPay() {
return basicPay;
}
public void setBasicPay(double basicPay) {
this.basicPay = basicPay;
}
public double getPerksPay() {
return perksPay;
}
public void setPerksPay(double perksPay) {
this.perksPay = perksPay;
}
public class Organization extends Employee
{
ArrayList<Employee> emp=new ArrayList<Employee>();
public int addEmployee(Employee e)
{
.......
}
}
The old java way:
public int addEmployee(Employee e) {
for (Employee employee : emp) {
if (e.getId() == employee.getId()) {
return -1;
}
}
emp.add(e);
return emp.size();
}
EDIT:
The Java8 way:
public int addEmployee(Employee e) {
List<Employee> alreadyInList = emp.stream().filter(em -> em.getId() == e.getId()).collect(Collectors.toList());
return alreadyInList.isEmpty() ? -1 : alreadyInList.size();
}

how to convert this multiple inheritance to interfaces in Java?

I want to implement the following multiple inheritance case:
(took from http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/)
Person Employee
|_________________|
|
Nurse
at the end I want to print the salaries from Employee and Nurse. In C++ it is easy to be done by using multiple inheritance, but I have problems with Java.
I have the following codes:
public class Person{
protected String name;
protected int age;
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
and the interface:
public interface Employee{
public double getSalary();
}
and the class Nurse:
public class Nurse extends Person implements Employee{
private double salary;
public Nurse(String name, int age, double salary){
super(name,age);
this.salary=salary;
}
#Override
public double getSalary(){
return salary;
}
}
but I do not have a clue how to make the Employee to print its salary, because it is an interface. I do not want to use other abstract class called Employee. How to fix this?
Thanks
Assuming that all employees are people (unless you are hiring monkeys!!), could you not chain the classes?
public class Person {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
public class Employee extends Person {
private double salary;
public double getSalary(){
return salary;
}
}
public class Nurse extends Employee {
}
Java allows multiple inheritance of interfaces only, so in order to implement that you have to define an interface for every class you'd like to combine and compose them in a single class by delegating all methods derived from the partial interfaces to their implementation.
Person interface - as you've defined it
PersonImpl class:
public final class PersonImpl implements Person {
private final String name;
private final int age;
public PersonImpl(String name, int age) {
this.name = name;
this.age = age;
}
#Override
public String getName() {
return name;
}
#Override
public int getAge() {
return age;
}
}
EmployeeImpl class:
public final class EmployeeImpl implements Employee {
private final double salary;
public EmployeeImpl(double salary) {
this.salary = salary;
}
#Override
public double getSalary() {
return salary;
}
}
Nurse class - composed from to other classes and delegates the functionality to them:
public class Nurse implements Employee, Person {
private final Employee employee;
private final Person person;
public Nurse(String name, int age, double salary) {
person = new PersonImpl(name, age);
employee = new EmployeeImpl(salary);
}
#Override
public double getSalary() {
return employee.getSalary();
}
#Override
public String getName() {
return person.getName();
}
#Override
public int getAge() {
return person.getAge();
}
}
It's advisable to define an interface for every class you code in order to able that approach in the future.

Categories