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 { public void eat() { System.out.println("猫吃鱼"); } public void catchMouse() { System.out.println("努力抓老鼠"); } }
Dog类
package com.itheima.homework_day07.animal; public class Dog extends Animal { 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); } 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); } 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 { public void work() { System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的厨师在炒菜"); } public void eat() { System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的厨师在吃肉"); } }
Manager类
package com.itheima.homework_day07.employee; public class Manager extends Employee { public void work() { System.out.println("工号为:" + getId() + ",姓名为:" + getName() + ",工资为" + getSalary() + "的经理在工作:管理其他人"); } 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; } // 打印全部信息 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()); } } // 打印全班平均分 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; } // 打印全班信息(包含男女人数) 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 + "人"); } // 打印全班平均分(去掉最高分、最低分) 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 { public void connect() { System.out.println("连接上了键盘...."); } 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 { public void connect() { System.out.println("连接上了鼠标..."); } 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; } 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; } } } }