用Java语音设计一个雇员Employee类,具体要求如下: (1) 设计雇员Employee类,记录雇员的情况,包括姓名、年薪、受雇时间(分别对应的成员变量声明如下:String name, double salary, MyDate start ); (2) 定义MyDate类作为日期,其中包括年、月、日,并定义一个方法String DateString() ,实现返回如格式“2020-3-5”的日期字符串; (3) 设计一个有参构造方法Employee(String name, double salary, MyDate start) 和一个输出该类对象信息的方法void show()。 (4) 编写 Main类,在其main方法中创建一个Employee对象,其姓名、年薪、受雇时间信息由键盘输入,例如:姓名Mike,年薪30万,受雇时间2020-1-10,并调用show()方法输出其信息。Main类代码如下: public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.nextLine(); double salary = sc.nextDouble(); int year = sc.nextInt(); int month = sc.nextInt(); int day = sc.nextInt(); MyDate start = new MyDate(year, month, day); Employee em1 = new Employee(name, salary, start); em1.show(); } } 输入格式:(分别输入姓名、年薪、受雇时间,每个信息占一行) 姓名 年薪 受雇时间的年份 月份 日 输出格式: 姓名,年薪,受雇时间的年份-月份-日 输入样例: Harry 500000 2009 10 1 输出样例: Harry,500000.0,2009-10-1
时间: 2024-01-21 07:03:57 浏览: 87
以下是Java代码实现:
Employee.java
```java
public class Employee {
private String name;
private double salary;
private MyDate start;
public Employee(String name, double salary, MyDate start) {
this.name = name;
this.salary = salary;
this.start = start;
}
public void show() {
System.out.println(name + "," + salary + "," + start.DateString());
}
}
```
MyDate.java
```java
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public String DateString() {
return year + "-" + month + "-" + day;
}
}
```
Main.java
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
double salary = sc.nextDouble();
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
MyDate start = new MyDate(year, month, day);
Employee em1 = new Employee(name, salary, start);
em1.show();
}
}
```
输入样例:
```
Harry
500000
2009
10
1
```
输出样例:
```
Harry,500000.0,2009-10-1
```
阅读全文