我一直被困在这里,这是一个Java程序,用于计算20名工人的平均工资,这是我到目前为止提出的。 ................................................... ................................................... ................................................... .......
import java.util.Scanner;
class Employee
{
int age;
String name, address, gender;
Scanner get = new Scanner(System.in);
Employee()
{
System.out.println("Enter Name of the Employee:");
name = get.nextLine();
System.out.println("Enter Gender of the Employee:");
gender = get.nextLine();
System.out.println("Enter Address of the Employee:");
address = get.nextLine();
System.out.println("Enter Age:");
age = get.nextInt();
}
void display()
{
System.out.println("Employee Name: "+name);
System.out.println("Age: "+age);
System.out.println("Gender: "+gender);
System.out.println("Address: "+address);
}
}
class fullTimeEmployees extends Employee
{
float salary;
int des;
fullTimeEmployee()
{
System.out.println("Enter Designation:");
des = get.nextInt();
System.out.println("Enter Salary:");
salary = get.nextFloat();
}
void display()
{
System.out.println("=============================="+"\n"+"Full Time Employee Details"+"\n"+"=============================="+"\n");
super.display();
System.out.println("Salary: "+salary);
System.out.println("Designation: "+des);
}
}
class partTimeEmployees extends Employee
{
int workinghrs, rate;
partTimeEmployees()
{
System.out.println("Enter Number of Working Hours:");
workinghrs = get.nextInt();
}
void calculatepay()
{
rate = 8 * workinghrs;
}
void display()
{
System.out.println("=============================="+"\n"+"Part Time Employee Details"+"\n"+"=============================="+"\n");
super.display();
System.out.println("Number of Working Hours: "+workinghrs);
System.out.println("Salary for "+workinghrs+" working hours is: $"+rate);
}
}
class Employees
{
public static void main(String args[])
{
System.out.println("================================"+"\n"+"Enter Full Time Employee Details"+"\n"+"================================"+"\n");
fullTimeEmployees ob1 = new fullTimeEmployees();
partTimeEmployees ob = new partTimeEmployees();
System.out.println("================================"+"\n"+"Enter Part Time Employee Details"+"\n"+"================================"+"\n");
ob1.display();
ob.calculatepay();
ob.display();
}
}
非常感谢您的协助!
答案 0 :(得分:0)
将构造函数fullTimeEmployee
更改为fullTimeEmployees
class fullTimeEmployees extends Employee
{
float salary;
int des;
fullTimeEmployees()
{
System.out.println("Enter Designation:");
des = get.nextInt();
System.out.println("Enter Salary:");
salary = get.nextFloat();
}
void display()
{
System.out.println("=============================="+"\n"+"Full Time Employee Details"+"\n"+"=============================="+"\n");
super.display();
System.out.println("Salary: "+salary);
System.out.println("Designation: "+des);
}
}
这适用于一名员工。如果要对20名员工执行相同操作,则使用循环;
public static void main(String args[])
{
fullTimeEmployees[] fullTimeEmployees= new fullTimeEmployees[20];
partTimeEmployees[] partTimeEmployees= new partTimeEmployees[20];
for(int i=0;i<20;i++){
System.out.println("================================"+"\n"+"Enter Full Time Employee Details"+"\n"+"================================"+"\n");
fullTimeEmployees[i] = new fullTimeEmployees();
System.out.println("================================"+"\n"+"Enter Part Time Employee Details"+"\n"+"================================"+"\n");
partTimeEmployees[i] = new partTimeEmployees();
}
System.out.println("================================"+"\n"+"Full Time Employee Details"+"\n"+"================================"+"\n");
for(int i=0 ;i<20;i++){
fullTimeEmployees[i].display();
}
System.out.println("================================"+"\n"+"Part Time Employee Details"+"\n"+"================================"+"\n");
for(int i=0 ;i<20;i++){
partTimeEmployees[i].calculatepay();
partTimeEmployees[i].display();
}
}
答案 1 :(得分:0)
在Employee中添加一个抽象方法,该方法返回工资:
public abstract int getSalary();
并在每个子类中实现它:
// class names start with upper case character
class FullTimeEmployee extends Employee
{
// don't use float, represent money in cents (and you should maybe represent the currency separately)
int salary;
// 'des' is not clear
int designtation;
fullTimeEmployee()
{
System.out.println("Enter Designation:");
designtation = get.nextInt();
System.out.println("Enter Salary:");
salary = get.nextInt();
public abstract int getSalary() {
return salary;
}
为什么是rate = 8 & workingHours
?
要计算平均值,请在迭代每个员工时跟踪总薪水和计入的雇员数:
int count = 0;
int totalSalary = 0;
Employee emp;
while (emp = getEmployee() != null) {
count++;
totalSalary += emp.getSalary();
}
double avgSalary = totalSalary / count;
System.out.println("Avg salary: " + avgSalary);
}
private Employee getEmployee() {
Employeee emp = null;
System.out.println("Part time or full time employee? any other key to quit (p/f):");
String input = get.nextString();
if ("f".equalsIgnoreCase(input)) {
System.out.println("================================"+"\n"+"Enter Full Time Employee Details"+"\n"+"================================"+"\n");
emp = new FullTimeEmployee();
}
else if ("p".equalsIgnoreCase(input)) {
emp = new PartTimeEmployee();
System.out.println("================================"+"\n"+"Enter Part Time Employee Details"+"\n"+"================================"+"\n");
}
return emp;
}