韩老师本章课程链接:https://www.bilibili.com/video/BV1fh411y7R8?p=343&spm_id_from=pageDriver
第一题
代码
package com.quange.homework; public class Homework01 { public static void main(String[] args) { Person[] people = new Person[4]; people[0] = new Person("jack", 50, "java工程师"); people[1] = new Person("tom", 20, "php工程师"); people[2] = new Person("mary", 30, "大数据工程师"); people[3] = new Person("change", 60, "c工程师"); // 输出当前数组对象 for (int i = 0; i < people.length; i++) { System.out.println(people[i]); } // 使用冒泡排序,按照年龄从大到小排序 Person tmp = null; // 临时变量 用于交换 boolean flag = true; // 标志是否完成排序,默认完成 for (int i = 0; i < people.length -1; i++) { for (int j = 0; j < people.length -1 -i; j++) { if (people[j].getAge() < people[j+1].getAge()){ flag = false; tmp = people[j]; people[j] = people[j+1]; people[j+1] = tmp; } } if (flag){ break; }else { flag = true; // 重置flag ,进行下次判断 } } System.out.println("=================排序后=============="); // 输出当前数组对象 for (int i = 0; i < people.length; i++) { System.out.println(people[i]); } } public static class Person{ private String name; private int age; private String job; public Person(String name, int age, String job) { this.name = name; this.age = age; this.job = job; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", job='" + job + '\'' + '}'; } } }
输出
Person{name='jack', age=50, job='java工程师'} Person{name='tom', age=20, job='php工程师'} Person{name='mary', age=30, job='大数据工程师'} Person{name='change', age=60, job='c工程师'} =================排序后============== Person{name='change', age=60, job='c工程师'} Person{name='jack', age=50, job='java工程师'} Person{name='mary', age=30, job='大数据工程师'} Person{name='tom', age=20, job='php工程师'} 进程已结束,退出代码0
第二题
第三题
代码
package com.quange.homework; public class Homework03 { public static void main(String[] args) { Teacher teacher = new Teacher("quange", 40, "高级职称", 3000, 1.3); teacher.introduce(); } // 父类 老师类 static class Teacher{ private String name; private int age; private String post; private double salary; private double grade; public Teacher(String name, int age, String post, double salary, double grade) { this.name = name; this.age = age; this.post = post; this.salary = salary; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPost() { return post; } public void setPost(String post) { this.post = post; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } public void introduce(){ System.out.println("姓名:" + name + "年龄:" + age+ "职称:" + post + "基本工资:" + salary + " 职级:" + grade); } } // 子类教授类 class Professor extends Teacher{ public Professor(String name, int age, String post, double salary, double grade) { super(name, age, post, salary, grade); } // 重写父类的 introduce 方法 @Override public void introduce() { super.introduce(); } } }
输出
姓名:quange年龄:40职称:高级职称基本工资:3000.0 职级:1.3 进程已结束,退出代码0
第四题
代码
package com.quange.homework; public class Homework04 { public static void main(String[] args) { Manager manager = new Manager("tom", 100, 20, 1.2); // 设置经理的奖金 manager.setBonus(1000); // 打印经理工资情况 manager.wages(); Ordinary change = new Ordinary("change", 90, 25, 1.0); // 打印员工工资情况 change.wages(); } // 父类 员工 static class Staff{ private String name; // 姓名 private double daily_wage; // 单日工资 private int work_days; // 工作天数 private double grade; // 职级 public Staff(String name, double daily_wage, int work_days, double grade) { this.name = name; this.daily_wage = daily_wage; this.work_days = work_days; this.grade = grade; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getDaily_wage() { return daily_wage; } public void setDaily_wage(double daily_wage) { this.daily_wage = daily_wage; } public int getWork_days() { return work_days; } public void setWork_days(int work_days) { this.work_days = work_days; } public double getGrade() { return grade; } public void setGrade(double grade) { this.grade = grade; } // 打印工资方法 public void wages(){ System.out.println("员工:"+ name + "工资为:" + (daily_wage * work_days * grade)); } } // 子类 部门经理 static class Manager extends Staff{ private double bonus; public Manager(String name, double daily_wage, int work_days, double grade) { super(name, daily_wage, work_days, grade); } public double getBonus() { return bonus; } public void setBonus(double bonus) { this.bonus = bonus; } @Override public void wages() { System.out.println("经理:" + getName() + "工资为:" + ((getDaily_wage() * getWork_days() * getGrade() )+ bonus)); } } // 子类 普通员工 static class Ordinary extends Staff{ public Ordinary(String name, double daily_wage, int work_days, double grade) { super(name, daily_wage, work_days, grade); } @Override public void wages() { super.wages(); } } }
输出
经理:tom工资为:3400.0 员工:change工资为:2250.0 进程已结束,退出代码0
- THE END -
最后修改:2022年4月15日
版权声明:
一、本站致力于为软件爱好者提供国内外软件开发技术和软件共享,着力为用户提供优资资源。
二、本站提供的所有下载文件均为网络共享资源,请于下载后的24小时内删除。如需体验更多乐趣,还请支持正版。
三、我站提供用户下载的所有内容均转自互联网。如有内容侵犯您的版权或其他利益的,请编辑邮件并加以说明发送到站长邮箱。站长会进行审查之后,情况属实的会在三个工作日内为您删除。
如若转载,请注明出处:https://www.quange.cc/note/java/learn-java/795.html
共有 0 条评论