创建一个工人类worker 以及一个list集合,在集合中添加三个工人信息:姓名 年龄 工资
时间: 2023-12-14 18:39:16 浏览: 103
好的,以下是 Python 代码示例:
```python
class Worker:
def __init__(self, name, age, salary):
self.name = name
self.age = age
self.salary = salary
workers = []
workers.append(Worker("张三", 25, 5000))
workers.append(Worker("李四", 30, 6000))
workers.append(Worker("王五", 28, 5500))
```
以上代码定义了一个名为 `Worker` 的类,其中包含了三个属性:`name`、 `age` 和 `salary`。接着创建了一个空的列表 `workers`,并通过 `append` 方法向其中添加了三个 `Worker` 对象,分别代表三个工人的信息。
相关问题
创建一个工人类(worker),以及一个list集合,在集合中添加三个工人信息: 姓名 年龄 工资 张三 30 3000 Worker w1=new Worker(“张三”,30,3000); 李四 25 3500 王五 27 3200 用java代码显示
下面是创建工人类(worker)和添加到list集合的Java代码:
```java
class Worker {
private String name;
private int age;
private double salary;
public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public String toString() {
return "姓名:" + name + " 年龄:" + age + " 工资:" + salary;
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Worker> workers = new ArrayList<>();
Worker w1 = new Worker("张三", 30, 3000);
Worker w2 = new Worker("李四", 25, 3500);
Worker w3 = new Worker("王五", 27, 3200);
workers.add(w1);
workers.add(w2);
workers.add(w3);
for (Worker worker : workers) {
System.out.println(worker);
}
}
}
```
输出结果如下:
```
姓名:张三 年龄:30 工资:3000.0
姓名:李四 年龄:25 工资:3500.0
姓名:王五 年龄:27 工资:3200.0
```
(List)编程:创建一个工人类,属性:姓名、年龄、工资,要求如下:(1) 为 Worker 提供无、有参数的构造方法,属性私有,并提供公开的 get/set (2) 创建多个 Worker 类,存储在 List 集合中。 (3) 打印输出所有的工人信息。 (4) 计算所有工人的平均工资。 (5) 打印输出姓名中包含 "胡" 的所有工人信息。 (7) 键盘输入一个姓名,查找是否存在此员工,存在,则打印输出员工的信息, 如果不存在,则输出"查无此人" (8) 输入一个工资,查询工资 大于 输入工资的员工信息。
好的,以下是符合要求的代码:
```java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Worker {
private String name;
private int age;
private double salary;
public Worker() {
}
public Worker(String name, int age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void main(String[] args) {
List<Worker> workers = new ArrayList<>();
workers.add(new Worker("张三", 25, 3000));
workers.add(new Worker("李四", 30, 4000));
workers.add(new Worker("王五", 35, 5000));
workers.add(new Worker("胡六", 40, 6000));
workers.add(new Worker("胡七", 45, 7000));
// 打印输出所有工人信息
System.out.println("所有工人信息:");
for (Worker worker : workers) {
System.out.println(worker.getName() + "," + worker.getAge() + "岁,工资:" + worker.getSalary());
}
// 计算平均工资
double totalSalary = 0;
for (Worker worker : workers) {
totalSalary += worker.getSalary();
}
double avgSalary = totalSalary / workers.size();
System.out.println("平均工资:" + avgSalary);
// 打印输出姓名中包含 "胡" 的所有工人信息
System.out.println("姓名中包含 '胡' 的所有工人信息:");
for (Worker worker : workers) {
if (worker.getName().contains("胡")) {
System.out.println(worker.getName() + "," + worker.getAge() + "岁,工资:" + worker.getSalary());
}
}
// 键盘输入一个姓名,查找是否存在此员工
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查找的员工姓名:");
String name = scanner.nextLine();
boolean isFound = false;
for (Worker worker : workers) {
if (worker.getName().equals(name)) {
System.out.println(worker.getName() + "," + worker.getAge() + "岁,工资:" + worker.getSalary());
isFound = true;
break;
}
}
if (!isFound) {
System.out.println("查无此人");
}
// 输入一个工资,查询工资大于输入工资的员工信息
System.out.print("请输入工资:");
double salary = scanner.nextDouble();
System.out.println("工资大于" + salary + "的员工信息:");
for (Worker worker : workers) {
if (worker.getSalary() > salary) {
System.out.println(worker.getName() + "," + worker.getAge() + "岁,工资:" + worker.getSalary());
}
}
}
}
```
这个程序中,我们创建了一个 `Worker` 类,并在其内部实现了无参和有参构造方法,以及封装了属性并提供了 get/set 方法。然后我们创建了多个 `Worker` 对象,并将其存储在 `List` 集合中。接着,我们按照要求实现了打印输出所有工人信息、计算平均工资、打印输出姓名中包含 "胡" 的所有工人信息、查找是否存在此员工以及查询工资大于输入工资的员工信息等功能。
阅读全文