题目:
【问题描述】
定义一个 Employee类,有名字、年龄、基本收入、分红收入等属性,定义构造方法用来初始化类的这些属性。
定义方法输出Employee的名字,年龄,总收入(基本收入和分红收入的和)。编写应用程序使用Employee。
【输入形式】
请输入员工的名字:
请输入员工的年龄:
请输入员工的基本收入:
请输入员工的分红收入:
【输出形式】
员工的名字:
员工的年龄:
员工的总收入(基本收入+分红收入):
【输入样例】
Please input the employee’s name: Tom
Please input the employee’s age: 20
Please input the employee’s sarary: 3000.0
Please input the employee’s bonus: 4000.0
【输出样例】
Employee’s name: Tom
Employee’s age: 20
Employee’s income: 7000.0
代码:
import java.util.Scanner;
class Employee{
String name;
int age;
float sarary;
float bonus;
Employee(String n,int a,float s,float b){
name=n;
age=a;
sarary=s;
bonus=b;
}
void employee() {
System.out.println("Employee's name: "+name);
System.out.println("Employee's age: "+age);
System.out.println("Employee's income: "+(sarary+bonus));
}
}
public class Test{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Please input the employee's name:");
String n=in.next();
System.out.println("Please input the employee's age:");
int a=in.nextInt();
System.out.println("Please input the employee's sarary:");
float s=in.nextFloat();
System.out.println("Please input the employee's bonus:");
float b=in.nextFloat();
Employee e=new Employee(n,a,s,b);
e.employee();
}
}