In Class Lab #9 Classes and objects Requirements Modify an existing partial prog
ID: 3582789 • Letter: I
Question
In Class Lab #9
Classes and objects
Requirements
Modify an existing partial program by implementing many unfinished features.
Place documentation comments at the beginning of the source code. These comments should include the course, your name, the date, and the assignment (Lab 9). Check the style guide for this course for details.
Create an Employee class
Implement a method to calculate and return the gross pay of an Employee object.
Resource
ClassLab.java
Points (10 points total)
2 points: for correctly implementing the Employee class
2 points: for correctly implementing the calcPay method
1 point: for correctly declaring an array of Employee objects
1 point: for correctly creating an Employee object for each element of the array
2 points: for correctly implementing the loop and input statements which get the Employee object data from the user
2 points: for correctly implementing the loop and output statements which display the Employee object data from the array
Note: Points may be deducted for missing documentation comments, use of tabs, improper indentation, and other breaches of CIS 150 standards
Example run of program
Explanation / Answer
package snippet;
import java.util.Scanner;
public class Employee {
String name;
double rate,hr;
Employee(String n,double r,double h)
{
name=n;
rate=r;
hr=h;
}
Employee()
{
name="";
rate=0;
hr=0;
}
void setName()//set name
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the employee name: ");
name=sc.nextLine();
}
void setRate()//set rate
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the employee rate:");
hr=sc.nextDouble();
}
void setHr()//set hours
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the employee hours:");
rate=sc.nextDouble();
}
double grosspay()
{
return rate*hr;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e[]=new Employee[3];
String n;
double h,r;
for(int i=0;i<3;i++)
{
e[i]=new Employee();
e[i].setName();
e[i].setHr();
e[i].setRate();
}
for(int i=0;i<3;i++)
{
System.out.println(e[i].name+" Hours:"+e[i].hr+" Rate: "+e[i].rate+"Gross Pay :"+e[i].grosspay());
}
}
}
============================================================================
Output:
Enter the employee name:
Dave Klick
Enter the employee hours:
13.75
Enter the employee rate:
12.50
Enter the employee name:
Cheryl Ladd
Enter the employee hours:
39.5
Enter the employee rate:
22.85
Enter the employee name:
Nike "Adidas" Reebok
Enter the employee hours:
41
Enter the employee rate:
27.50
Dave Klick Hours:12.5 Rate: 13.75Gross Pay :171.875
Cheryl Ladd Hours:22.85 Rate: 39.5Gross Pay :902.575
Nike "Adidas" Reebok Hours:27.5 Rate: 41.0Gross Pay :1127.5