博客
关于我
长春大学20级第十二周第一次上机(5月17号)
阅读量:803 次
发布时间:2023-03-25

本文共 4157 字,大约阅读时间需要 13 分钟。

接下来是以下内容的优化版本:

一、数字处理

给一个不多于5位的正整数。

(1)求它是几位数

将输入的数字转换为字符串,计算其长度即可确定位数。

(2)逆序打印出各位数字

将字符串转换为字符数组,从末尾开始遍历,依次输出字符即可得到逆序结果。

二、数组操作

编写一个类并验证它是否有效,该类包含一个维数组成员变量,完成数组的赋值与输出。

class test {
int[] nums = new int[5];
}

使用示例

test t = new test();
for (int i = 0; i < t.nums.length; i++) {
t.nums[i] = i;
}
for (int i = 0; i < t.nums.length; i++) {
System.out.println(t.nums[i]);
}

三、工资查询系统

实现一个工资查询的程序,支持三类员工:项目经理、 高级程序员、 程序员。

基于工资计算的规则

  • 基础工资:5000元/月。
  • 绩效工资
    • 项目经理:5000元/月。
    • 高级程序员:5000元/月。
    • 程序员:无额外绩效工资。
  • 加班费
    • 项目经理:800元/天。
    • 高级程序员:500元/天。
    • 程序员:200元/天。
  • 实现代码

    interface Factory {
    int searchSalary(String position);
    int workTime(int day, String position);
    }
    class Manager implements Factory {
    @Override
    public int searchSalary(String position) {
    int salary = 15000;
    System.out.println(position + "正常的工资为: " + salary);
    return salary;
    }
    @Override
    public int workTime(int day, String position) {
    int salary = 15000;
    int final_salary = salary + 800 * day;
    System.out.println(position + "加班后的工资为: " + final_salary);
    return final_salary;
    }
    }
    class SeniorProgrammer implements Factory {
    @Override
    public int searchSalary(String position) {
    int salary = 10000;
    System.out.println(position + "正常的工资为: " + salary);
    return salary;
    }
    @Override
    public int workTime(int day, String position) {
    int salary = 10000;
    int final_salary = salary + 500 * day;
    System.out.println(position + "加班后的工资为: " + final_salary);
    return final_salary;
    }
    }
    class Programmer implements Factory {
    @Override
    public int searchSalary(String position) {
    int salary = 5000;
    System.out.println(position + "正常的工资为: " + salary);
    return salary;
    }
    @Override
    public int workTime(int day, String position) {
    int salary = 5000;
    int final_salary = salary + 200 * day;
    System.out.println(position + "加班后的工资为: " + final_salary);
    return final_salary;
    }
    }
    class FactoryImpl {
    public void useBegin(Factory fac, String position) {
    fac.searchSalary(position);
    }
    public void useFinal(Factory fac, int day, String position) {
    fac.workTime(day, position);
    }
    }
    public class Main {
    public static void main(String[] args) {
    FactoryImpl f = new FactoryImpl();
    f.useBegin(new Manager(), "项目经理");
    f.useFinal(new Manager(), 6, "项目经理");
    f.useBegin(new SeniorProgrammer(), "高级程序员");
    f.useFinal(new SeniorProgrammer(), 7, "高级程序员");
    f.useBegin(new Programmer(), "程序员");
    f.useFinal(new Programmer(), 8, "程序员");
    }
    }

    四、计算器功能

    利用接口做参数,编写一个计算器,支持加减乘除运算。

    接口定义

    interface Computer {
    int computer(int n, int m);
    }

    实现类

    class Add implements Computer {
    @Override
    public int computer(int n, int m) {
    int value = n + m;
    System.out.println("n + m = " + value);
    return value;
    }
    }
    class Sub implements Computer {
    @Override
    public int computer(int n, int m) {
    int value = n - m;
    System.out.println("n - m = " + value);
    return value;
    }
    }
    class Multiply implements Computer {
    @Override
    public int computer(int n, int m) {
    int value = n * m;
    System.out.println("n * m = " + value);
    return value;
    }
    }
    class Divide implements Computer {
    @Override
    public int computer(int n, int m) {
    if (m != 0) {
    int value = n / m;
    System.out.println("n / m = " + value);
    return value;
    } else {
    System.out.println("分母不能为零");
    return 0;
    }
    }
    }
    class UseCompute {
    public UseCompute(int n, int m) {
    System.out.println("n的值为:" + n + ", m的值为:" + m);
    }
    public void useCom(Computer com, int n, int m) {
    com.computer(n, m);
    }
    }
    public class Main {
    public static void main(String[] args) {
    UseCompute uc = new UseCompute(20, 10);
    uc.useCom(new Add(), 20, 10);
    uc.useCom(new Sub(), 20, 10);
    uc.useCom(new Multiply(), 20, 10);
    uc.useCom(new Divide(), 20, 10);
    }
    }

    转载地址:http://bzhfk.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现均值滤波(附完整源码)
    查看>>
    Objective-C实现埃拉托斯特尼筛法算法(附完整源码)
    查看>>
    Objective-C实现域名解析(附完整源码)
    查看>>
    Objective-C实现域名转IP(附完整源码)
    查看>>
    Objective-C实现培根密码算法(附完整源码)
    查看>>
    Objective-C实现基于 LIFO的堆栈算法(附完整源码)
    查看>>
    Objective-C实现基于 LinkedList 的添加两个数字的解决方案算法(附完整源码)
    查看>>
    Objective-C实现基于opencv的抖动算法(附完整源码)
    查看>>
    Objective-C实现基于事件对象实现线程同步(附完整源码)
    查看>>
    Objective-C实现基于信号实现线程同步(附完整源码)
    查看>>
    Objective-C实现基于文件流拷贝文件(附完整源码)
    查看>>
    Objective-C实现基于模板的双向链表(附完整源码)
    查看>>
    Objective-C实现基于模板的顺序表(附完整源码)
    查看>>
    Objective-C实现基本二叉树算法(附完整源码)
    查看>>
    Objective-C实现堆排序(附完整源码)
    查看>>
    Objective-C实现填充环形矩阵(附完整源码)
    查看>>
    Objective-C实现声音录制播放程序(附完整源码)
    查看>>
    Objective-C实现备忘录模式(附完整源码)
    查看>>
    Objective-C实现复制粘贴文本功能(附完整源码)
    查看>>
    Objective-C实现复数类+-x%(附完整源码)
    查看>>