Java面向对象基础与算法应用实验

1. 题目一:员工信息类设计与实例化
   - 描述:创建一个名为 `Employee1_1` 的类,用于表示员工信息。该类包含员工的工号、姓名、性别、年龄、婚姻状况、工资和部门等属性。编写一个构造函数用于初始化员工对象,并重写 `toString` 方法以输出员工的详细信息。在 `Main1_1` 类的 `main` 方法中创建一个 `Employee1_1` 对象并打印其信息。

package shiyan;

public class Employee1_1 {
    private String id;
    private String name;
    private String gender;
    private int age;
    private String maritalStatus;
    private double salary;
    private String department;


    // 构造函数
    public Employee1_1(String id, String name, String gender, int age, String maritalStatus, double salary, String department) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.maritalStatus = maritalStatus;
        this.salary = salary;
        this.department = department;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "工号='" + id + '\'' +
                ", 姓名='" + name + '\'' +
                ", 性别='" + gender + '\'' +
                ", 年龄=" + age +
                ", 婚否='" + maritalStatus + '\'' +
                ", 工资=" + salary +
                ", 部门='" + department + '\'' +
                '}';
    }
}

package shiyan;

public class Main1_1 {
        public static void main(String[] args) {
            // 创建员工对象并赋值
            Employee1_1 employee = new Employee1_1("Z001", "李磊", "男", 30, "已婚", 7800.0, "人事部");
            // 输出员工信息
            System.out.println(employee);
        }
    }
 

2. 题目二:中国剩余定理问题求解
   - 描述:编写一个程序,找出1000以内所有满足以下条件的整数:能被3除余2,被5除余3,被7除余2。打印出这些整数以及满足条件的总数。如果没有找到满足条件的数,则输出相应的提示信息。


package shiyan;
public class ChineseRemainderTheorem1_2{
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 1000; i++) {
            if (i % 3 == 2 && i % 5 == 3 && i % 7 == 2) {
                System.out.println(i);
                count++;
            }
        }
        if (count == 0) {
            System.out.println("在1000以内没有找到满足条件的数。");
        } else {
            System.out.println("共有 " + count + " 个满足条件的数。");
        }
    }
}

3. 题目三:星座符号编码生成
   - 描述:创建一个名为 `ConstellationSymbols1_3` 的程序,用于输出12星座的名称、对应的编码以及星座符号。星座名称存储在一个数组中,每个星座的编码基于一个基础编码9800,并根据星座在数组中的索引递增。星座符号使用编码对应的字符表示。

package shiyan;
//ConstellationSymbols1_3 为“星座符号”
public class ConstellationSymbols1_3 {
    public static void main(String[] args) {
        // 星座名称数组
        String[] constellations = {
                "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座",
                "处女座", "天秤座", "天蝎座", "射手座", "摩羯座",
                "水瓶座", "双鱼座"
        };
        // 第一个星座的编码是9800
        int baseCode = 9800;
        // 输出星座及其符号
        for (int i = 0; i < constellations.length; i++) {
            int code = baseCode + i;
            System.out.println("编码: " + code + ", 星座: " + constellations[i] + ", 符号: " + (char)code);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值