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;
            }
        }


    }
}
相关文章
|
1月前
|
存储 Oracle Java
java零基础学习者入门课程
本课程为Java零基础入门教程,涵盖环境搭建、变量、运算符、条件循环、数组及面向对象基础,每讲配示例代码与实践建议,助你循序渐进掌握核心知识,轻松迈入Java编程世界。
271 0
|
2月前
|
Java API 容器
Java基础学习day08-2
本节讲解Java方法引用与常用API,包括静态、实例、特定类型方法及构造器引用的格式与使用场景,并结合代码示例深入解析。同时介绍String和ArrayList的核心方法及其实际应用。
153 1
|
1月前
|
IDE Java 编译器
java编程最基础学习
Java入门需掌握:环境搭建、基础语法、面向对象、数组集合与异常处理。通过实践编写简单程序,逐步深入学习,打牢编程基础。
204 0
|
1月前
|
负载均衡 Java API
grpc-java 架构学习指南
本指南系统解析 grpc-java 架构,涵盖分层设计、核心流程与源码结构,结合实战路径与调试技巧,助你从入门到精通,掌握高性能 RPC 开发精髓。
253 7
|
2月前
|
Java
Java基础学习day08-作业
本作业涵盖Java中Lambda表达式的应用,包括Runnable与Comparator接口的简化实现、自定义函数式接口NumberProcessor进行加减乘及最大值操作,以及通过IntProcessor处理整数数组,实现遍历、平方和奇偶判断等功能,强化函数式编程实践。
75 5
|
2月前
|
Java 程序员
Java基础学习day08
本节讲解Java中的代码块(静态与实例)及其作用,深入介绍内部类(成员、静态、局部及匿名)的定义与使用,并引入函数式编程思想,重点阐述Lambda表达式及其在简化匿名内部类中的应用。
138 5
|
2月前
|
Java
Java基础学习day06-作业
本内容为Java基础学习作业,涵盖两个案例:一是通过Card类及其子类GoldenCard、SilverCard实现加油卡系统,体现封装与继承;二是通过Shape类及子类Circle、Rectangle演示多态与方法重写,强化面向对象编程理解。
80 1
|
2月前
|
设计模式 存储 Java
Java基础学习day07
本节讲解Java中的final关键字、单例设计模式、枚举类、抽象类与接口。涵盖常量定义、单例写法(饿汉式/懒汉式)、枚举特点及应用场景,以及抽象类与接口的使用与区别,助力掌握核心面向对象编程思想。
138 1
|
存储 安全 Java
24、使用 Java 官方教程学习:① 类变量和类方法详解;② 深入介绍 main() 方法
24、使用 Java 官方教程学习:① 类变量和类方法详解;② 深入介绍 main() 方法
189 1
【JAVA学习之路 | 进阶篇】Map接口及其实现类及常用方法
【JAVA学习之路 | 进阶篇】Map接口及其实现类及常用方法