Write the classes indicated for the following class diagram. Copy your Employee
ID: 667899 • Letter: W
Question
Write the classes indicated for the following class diagram. Copy your Employee class from problem 2.
Problem 3
Store class:
The Store class is used to manage the collection of Employee objects that exist for the store. In other words, a store may have 5 employees so that its emps array would have the first 5 elements filled with references to these employees.
emps – An instance variable, which is an array that can hold up to 20 Employee objects. Employees are stored sequentially, with no gaps, starting with position 0. Employees can be added but not removed.
numEmps – An instance variable that stores the total number of Employee objects in the emps array. Initially this value is 0.
getNumEmps():int – Returns the number of employees. Getter for numEmps.
getEmp(i:int):Employee – Returns the employee at position i if there is one, otherwise returns null. For example to return the 3rd employee, you would return emps[2].
addEmp(e:Employee) – Adds the Employee, e in the next available slot. Does nothing if there are no more slots. For example if there are 3 employees then e will be stored in emps[3] and the number of employees will become 4.
totalHours():double – Returns the total number of hours worked over all the employees. Hint: loop through the emps and get the total hours for each one and add them up.
toString():string – Consider this object diagram as an example:
The toString method should return a message like this:
Num Employees: 3
Dave worked 5 days for a total of 40.00 hours
Anna worked 1 days for a total of 10.00 hours
Paul worked 0 days for a total of 0.00 hours
Total Hours Worked = 50.00
StoreTester class – use this main method exactly
public static void main(String[] args) {
Store store = new Store();
Employee e = new Employee("Dave");
for(int i=1; i<6; i++) e.setHours(i, 8);
store.addEmp(e);
e = new Employee("Anna");
e.setHours(6, 10);
store.addEmp(e);
e = new Employee("Paul");
store.addEmp(e);
System.out.println (store);
System.out.println (store.getEmp(1));
}
Explanation / Answer
Store.java
package mani;
public class Store {
Employee[] emps=new Employee[20];
int numEmps=0;
public Store(){
}
public int getNumEmps(){
return numEmps;
}
public Employee getEmp(int i){
if(numEmps>=i){
return emps[i-1];
}
else{
return null;
}
}
public void addEmp(Employee e){
if(numEmps<20){
emps[numEmps]=e;
numEmps++;
}
}
public double totalHours(){
double t=0;
for(int i=0;i<numEmps;i++){
t=t+emps[i].totalHours();
}
return t;
}
public String toString(){
String r="Num Employees :"+getNumEmps()+" ";
for(int i=0;i<numEmps;i++){
r=r+emps[i].name+"Worked "+emps[i].numDaysWorked()+"for a total of "+emps[i].totalHours()+" hours ";
}
r=r+"total Hours Worked:"+totalHours();
return r;
}
}
Employess.java
package mani;
public class Employee {
double[] hours=new double[7];
String name;
public Employee(String name){
this.name=name;
}
public double getHours(int day){
return hours[day];
}
public void setHours(int day,double hrs){
hours[day]=hrs;
}
public int numDaysWorked(){
int totalD=0;
for(int i=0;i<7;i++){
if(hours[i]==0.0){
}else{
totalD=totalD+1;
}
}
return totalD;
}
public double totalHours(){
double totalH=0;
for(int i=0;i<7;i++){
if(hours[i]==0.0){
}else{
totalH=totalH+hours[i];
}
}
return totalH;
}
public String toString(){
return "name="+name+" TotalHours:"+totalHours();
}
}
I defined the employee any changes please feel free to ask I mean comment
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.