Java基础学习day07-作业

简介: 本作业包含六个Java编程案例:1)动物类继承与多态;2)加油卡支付系统;3)员工管理类设计;4)学生信息统计接口;5)USB设备控制;6)家电智能控制。综合运用抽象类、接口、继承、多态等面向对象技术,强化Java基础编程能力。

Java基础学习day07-作业

案例一

Animal类

package com.itheima.homework_day07.animal;

public abstract class Animal {
    // 需求:
    //定义一个父类Animal 包含name,weight属性和一个抽象的eat方法
    //定义两个子类Dog和Cat,Dog特有方法lookHome,Cat特有方法catchMouse;并且重写eat方法,Dog吃骨头,Cat吃鱼
    //要求:使用多态形式创建Dog和Cat对象,调用eat方法,并且使用向下转型,如果是Cat类型调用catchMouse功能,如果是Dog类型调用lookHome功能

    private String name;
    private double weight;

    public abstract void eat();
}

Cat类

package com.itheima.homework_day07.animal;

public class Cat extends Animal {
    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }

    public void catchMouse() {
        System.out.println("努力抓老鼠");
    }
}

Dog类

package com.itheima.homework_day07.animal;

public class Dog extends Animal {
    @Override
    public void eat() {
        System.out.println("狗吃骨头");
    }

    public void lookHome() {
        System.out.println("老老实实看家");
    }
}

Test类

package com.itheima.homework_day07.animal;

public class Test {
    public static void main(String[] args) {
        Animal a = new Cat();
        Animal b = new Dog();

        b.eat();
        if (b instanceof Dog d) {
            d.lookHome();
        }

        System.out.println("----------------");

        a.eat();
        if (a instanceof Cat c) {
            c.catchMouse();
        }
    }
}

案例二

Card类

package com.itheima.homework_day07.card;

public abstract class Card {
    // 需求1:
    // 某加油站推出了2种支付卡,一种是预存10000的金卡,后续加油享受8折优惠。
    // 另一种是预存5000的银卡 ,后续加油享受8.5折优惠。
    // 请分别实现2种卡片进入收银系统后的逻辑,卡片需要包含卡号,余额,支付功能 。

    private String id;
    private double balance;
    private double cost;

    public Card() {
    }

    public Card(String id, double balance, double cost) {
        this.id = id;
        this.balance = balance;
        this.cost = cost;
    }

    public abstract void pay();
    
    /**
     * 获取
     *
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     *
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取
     *
     * @return balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * 设置
     *
     * @param balance
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**
     * 获取
     *
     * @return cost
     */
    public double getCost() {
        return cost;
    }

    /**
     * 设置
     *
     * @param cost
     */
    public void setCost(double cost) {
        this.cost = cost;
    }

}

GoldenCard类

package com.itheima.homework_day07.card;

public class GoldenCard extends Card {


    public GoldenCard() {
        super(null, 10000, 0);
    }

    public GoldenCard(String id, double balance, double cost) {
        super(id, 10000, cost);
    }

    @Override
    public void pay() {
        double discountCost = getCost() * 0.8;
        setBalance(getBalance() - discountCost);
        System.out.println("尊敬的卡号id为:" + getId() + "的金卡用户,您本次消费" + getCost() + "元,打8折后扣款"
                + discountCost + "元,当前余额为:" + getBalance() + "元,欢迎下次光临");
    }
}

SilverCard类

package com.itheima.homework_day07.card;

public class SilverCard extends Card {

    public SilverCard() {
        super(null, 5000, 0);
    }

    public SilverCard(String id, double balance, double cost) {
        super(id, 5000, cost);
    }

    @Override
    public void pay() {
        double discountCost = getCost() * 0.85;
        setBalance(getBalance() - discountCost);
        System.out.println("尊敬的卡号id为:" + getId() + "的银卡用户,您本次消费" + getCost() + "元,打8.5折后扣款"
                + discountCost + "元,当前余额为:" + getBalance() + "元,欢迎下次光临");
    }
}

Test类

package com.itheima.homework_day07.card;

public class Test {
    public static void main(String[] args) {
        Card g = new GoldenCard();
        g.setId("001");
        g.setCost(1000.8);
        g.pay();

        Card s = new SilverCard();
        s.setId("002");
        s.setCost(200);
        s.pay();

    }
}

案例三

Employee类

package com.itheima.homework_day07.employee;

public abstract class Employee {
    // 需求2:
    // 1.经理
    //  成员变量:工号,姓名,工资
    //  成员方法:工作(管理其他人),吃饭(吃鱼)
    //2.厨师
    //  成员变量:工号,姓名,工资
    //  成员方法:工作(炒菜),吃饭(吃肉)

    private String id;
    private String name;
    private double salary;


    public Employee() {
    }

    public Employee(String id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    /**
     * 获取
     *
     * @return id
     */
    public String getId() {
        return id;
    }

    /**
     * 设置
     *
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return salary
     */
    public double getSalary() {
        return salary;
    }

    /**
     * 设置
     *
     * @param salary
     */
    public void setSalary(double salary) {
        this.salary = salary;
    }

    public abstract void eat();

    public abstract void work();

}

Cook类

package com.itheima.homework_day07.employee;

public class Cook extends Employee {
    @Override
    public void work() {
        System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的厨师在炒菜");
    }

    @Override
    public void eat() {
        System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的厨师在吃肉");
    }

}

Manager类

package com.itheima.homework_day07.employee;

public class Manager extends Employee {

    @Override
    public void work() {
        System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的经理在工作:管理其他人");
    }

    @Override
    public void eat() {
        System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的经理在吃鱼");
    }
}

Test类

package com.itheima.homework_day07.employee;

public class Test {
    public static void main(String[] args) {

        Employee e1 = new Manager();
        e1.setId("v001");
        e1.setName("张三");
        e1.setSalary(13000);
        e1.eat();
        e1.work();

        System.out.println("------------------------");

        Employee e2 = new Cook();
        e2.setId("p001");
        e2.setName("小李");
        e2.setSalary(7000);
        e2.eat();
        e2.work();

    }
}

案例四

Information接口

package com.itheima.homework_day07.student;

public interface Information {
    void show1();

    void show2();
}

ShowInformation1类

package com.itheima.homework_day07.student;

public class ShowInformation1 implements Information {
    // 接收全班信息
    private Student[] students;

    public ShowInformation1(Student[] students) {
        this.students = students;
    }

    // 打印全部信息
    @Override
    public void show1() {
        System.out.println("全部学生信息如下:");
        for (int i = 0; i < students.length; i++) {
            System.out.println("姓名:" + students[i].getName() + "\t性别:" + students[i].getGender() + "\t成绩:" + students[i].getScore());
        }
    }

    // 打印全班平均分
    @Override
    public void show2() {
        System.out.print("全班学生的平均成绩为:");
        double avgScore = 0;
        double sumScore = 0;
        for (int i = 0; i < students.length; i++) {
            sumScore = sumScore + students[i].getScore();
        }
        avgScore = sumScore / students.length;
        System.out.println(avgScore);
    }
}

ShowInformation2类

package com.itheima.homework_day07.student;

public class ShowInformation2 implements Information {
    private Student[] students;

    public ShowInformation2(Student[] students) {
        this.students = students;
    }

    // 打印全班信息(包含男女人数)
    @Override
    public void show1() {
        int boy = 0;
        int girl = 0;
        System.out.println("全班学生的信息(包含男女人数)为:");
        for (int i = 0; i < students.length; i++) {
            if (students[i].getGender() == '男') {
                boy++;
            } else {
                girl++;
            }
            System.out.println("姓名:" + students[i].getName() + "\t性别:" + students[i].getGender() + "\t成绩:" + students[i].getScore());
        }
        System.out.println("男生有" + boy + "人,女生有" + girl + "人");
    }

    // 打印全班平均分(去掉最高分、最低分)

    @Override
    public void show2() {
        double maxScore = students[0].getScore();
        double minScore = students[0].getScore();
        double sumScore = 0;
        double quScore = 0;
        double avgScore = 0;
        for (int i = 0; i < students.length; i++) {
            if (students[i].getScore() > maxScore) {
                maxScore = students[i].getScore();
            }
            if (students[i].getScore() < minScore) {
                minScore = students[i].getScore();
            }
            sumScore = sumScore + students[i].getScore();
        }
        quScore = sumScore - maxScore - minScore;
        avgScore = quScore / (students.length - 2);
        System.out.println("全班学生去掉最高分、最低分的平均成绩为:" + avgScore);
    }
}

Student类

package com.itheima.homework_day07.student;

public class Student {
    private String name;
    private char gender;
    private double score;


    public Student() {
    }

    public Student(String name, char gender, double score) {
        this.name = name;
        this.gender = gender;
        this.score = score;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return gender
     */
    public char getGender() {
        return gender;
    }

    /**
     * 设置
     *
     * @param gender
     */
    public void setGender(char gender) {
        this.gender = gender;
    }

    /**
     * 获取
     *
     * @return score
     */
    public double getScore() {
        return score;
    }

    /**
     * 设置
     *
     * @param score
     */
    public void setScore(double score) {
        this.score = score;
    }

    public String toString() {
        return "Student{name = " + name + ", gender = " + gender + ", score = " + score + "}";
    }
}

Test类

package com.itheima.homework_day07.student;

public class Test {
    public static void main(String[] args) {

        Student[] students = new Student[]{
                new Student("晋一", '男', 95),
                new Student("黄二", '男', 97),
                new Student("张三", '男', 96),
                new Student("李四", '女', 99),
                new Student("王五", '男', 98),
                new Student("赵六", '女', 97),
                new Student("孙七", '男', 99),
                new Student("周八", '女', 95),
                new Student("吴九", '男', 94),
                new Student("郑十", '女', 99)
        };
        Information showInformation1 = new ShowInformation1(students);
        showInformation1.show1();
        showInformation1.show2();

        System.out.println("================================================================================================");
        Information showInformation2 = new ShowInformation2(students);
        showInformation2.show1();
        showInformation2.show2();

    }
}

案例五

Usb接口

package com.itheima.homework_day07.usb;

public interface Usb {

    // 连接方法
    void connect();

    // 退出方法
    void exit();

}

Computer类

package com.itheima.homework_day07.usb;

public class Computer {

    public void use(Usb usb) {
        usb.connect();
        if (usb instanceof KeyBoard keyBoard) {
            keyBoard.input();
        } else if (usb instanceof Mouse mouse) {
            mouse.click();
        }
        usb.exit();
    }
}

KeyBoard类

package com.itheima.homework_day07.usb;

public class KeyBoard implements Usb {
    @Override
    public void connect() {
        System.out.println("连接上了键盘....");
    }

    @Override
    public void exit() {
        System.out.println("拔出了键盘....");
    }

    public void input() {
        System.out.println("使用了键盘输入了 HelloWolrd");
    }
}

Mouse类

package com.itheima.homework_day07.usb;

public class Mouse implements Usb {
    @Override
    public void connect() {
        System.out.println("连接上了鼠标...");
    }

    @Override
    public void exit() {
        System.out.println("拔出了鼠标...");
    }

    public void click() {
        System.out.println("使用了鼠标点击桌面..");
    }
}

Test类

package com.itheima.homework_day07.usb;

public class Test {
    public static void main(String[] args) {

        Mouse mouse = new Mouse();
        KeyBoard keyBoard = new KeyBoard();
        Computer computer = new Computer();

        computer.use(mouse);
        System.out.println("====================");
        computer.use(keyBoard);
    }
}

案例六

Button接口

package com.itheima.homework_day07.appliances;

public interface Button {
    void press();
}

Appliances类

package com.itheima.homework_day07.appliances;

public class Appliances implements Button {

    // 电器名字
    private String name;

    // 电器状态  设为默认关闭
    private boolean status = false;

    public Appliances() {
    }

    public Appliances(String name, boolean status) {
        this.name = name;
        this.status = status;
    }


    @Override
    public void press() {
        // 使用一次该方法,表示按一下开关按钮
        status = !status;
    }

    /**
     * 获取
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     *
     * @return status
     */
    public boolean isStatus() {
        return status;
    }

    /**
     * 设置
     *
     * @param status
     */
    public void setStatus(boolean status) {
        this.status = status;
    }

    public String toString() {
        return "Appliances{name = " + name + ", status = " + status + "}";
    }
}

AppliancesControl类

package com.itheima.homework_day07.appliances;

public class AppliancesControl extends Appliances {
    public void control(Appliances appliances) {
        System.out.println(appliances.getName() + "当前的状态为:" + (appliances.isStatus() ? "开着" : "关闭"));
        System.out.println("您按了开关");
        appliances.press();
        System.out.println(appliances.getName() + "当前的状态为:" + (appliances.isStatus() ? "开着" : "关闭"));
    }

    public void show(Appliances[] appliances) {
        for (int i = 0; i < appliances.length; i++) {
            System.out.println((i + 1) + "," + appliances[i].getName() + "状态目前是:" + (appliances[i].isStatus() ? "开着" : "关闭!"));
        }
    }
}

Light类

package com.itheima.homework_day07.appliances;

public class Light extends Appliances {
    // 吊灯

    public Light(String name, boolean status) {
        super(name, status);
    }
}

Tv类

package com.itheima.homework_day07.appliances;

public class Tv extends Appliances {
    // 电视

    public Tv(String name, boolean status) {
        super(name, status);
    }
}

Washer类

package com.itheima.homework_day07.appliances;

public class Washer extends Appliances {
    // 洗衣机

    public Washer(String name, boolean status) {
        super(name, status);
    }
}

Cooker类

package com.itheima.homework_day07.appliances;

public class Cooker extends Appliances {
    // 电饭煲

    public Cooker(String name, boolean status) {
        super(name, status);
    }
}

Curtain类

package com.itheima.homework_day07.appliances;

public class Curtain extends Appliances {
    // 窗帘

    public Curtain(String name, boolean status) {
        super(name, status);
    }
}

Ac类

package com.itheima.homework_day07.appliances;

public class Ac extends Appliances {

    // 空调


    public Ac(String name, boolean status) {
        super(name, status);
    }
}

Test类

package com.itheima.homework_day07.appliances;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Appliances[] appliances = new Appliances[6];
        appliances[0] = new Light("吊灯", false);
        appliances[1] = new Tv("电视机", false);
        appliances[2] = new Washer("洗衣机", false);
        appliances[3] = new Cooker("电饭煲", false);
        appliances[4] = new Curtain("窗帘", false);
        appliances[5] = new Ac("空调", false);

        AppliancesControl appliancesControl = new AppliancesControl();

        while (true) {
            appliancesControl.show(appliances);
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要操作的家电编号(输入0退出):");
            int id = sc.nextInt();
            switch (id) {
                case 1:
                    appliancesControl.control(appliances[0]);
                    break;
                case 2:
                    appliancesControl.control(appliances[1]);
                    break;
                case 3:
                    appliancesControl.control(appliances[2]);
                    break;
                case 4:
                    appliancesControl.control(appliances[3]);
                    break;
                case 5:
                    appliancesControl.control(appliances[4]);
                    break;
                case 6:
                    appliancesControl.control(appliances[5]);
                case 0:
                    System.out.println("已退出");
                    return;
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }
        }


    }
}
相关文章
|
8天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1192 4
|
7天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
950 12
|
6天前
|
机器学习/深度学习 物联网
Wan2.2再次开源数字人:Animate-14B!一键实现电影角色替换和动作驱动
今天,通义万相的视频生成模型又又又开源了!Wan2.2系列模型家族新增数字人成员Wan2.2-Animate-14B。
536 11
|
17天前
|
人工智能 运维 安全
|
8天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
339 0
|
8天前
|
消息中间件 Java Apache
SpringBoot集成RocketMq
RocketMQ 是一款开源的分布式消息中间件,采用纯 Java 编写,支持事务消息、顺序消息、批量消息、定时消息及消息回溯等功能。其优势包括去除对 ZooKeeper 的依赖、支持异步和同步刷盘、高吞吐量及消息过滤等特性。RocketMQ 具备高可用性和高可靠性,适用于大规模分布式系统,能有效保障消息传输的一致性和顺序性。
463 2
|
15天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
8天前
|
云栖大会
阿里云云栖大会2025年9月24日开启,免费申请大会门票,速度领取~
2025云栖大会将于9月24-26日举行,官网免费预约畅享票,审核后短信通知,持证件入场
1563 12