[个人笔记]面向对象编程题

一、存储所有的学生信息
    1.遍历所有的学生信息
    2.输出所有学生的学生姓名以及学生所在的学校名字,学校位置等
    3.统计身高在179~180之间的学生个数
    4.统计学校为清华大学的学生人数
public class StudentSchool {
    public static void main(String[] args) {

        School school1 = new School("清华大学", "北京");
        School school2 = new School("北京大学", "北京");
        School school3 = new School("复旦大学", "上海");

        Student s1 = new Student("张三", 181.5, 18, school2);
        Student s2 = new Student("李四", 179.5, 20, school2);
        Student s3 = new Student("王五", 178.2, 36, school3);
        Student s4 = new Student("赵六", 179.5, 25, school1);
        Student s5 = new Student("某某", 179.5, 20, school3);
        Student[] students = StudentsTool.saveStudents(s1, s2, s3, s4, s5);

        //输出所有学生信息
        System.out.println("输出所有学生信息");
        StudentsTool.showInfo(students);

        //输出所有学生的学生姓名以及学生所在的学校名字,学校位置等
        System.out.println("输出所有学生的学生姓名以及学生所在的学校名字,学校位置等");
        StudentsTool.showSchool(students);

        //统计身高在179~180之间的学生个数
        System.out.println("统计身高在179~180之间的学生个数");
        StudentsTool.countHeight(students, 179.0, 180.0);

        //统计学校为清华大学的学生人数
        System.out.println("统计学校为清华大学的学生人数");
        StudentsTool.countSchool(students, "清华大学");
    }

}

/**
 * 学生信息管理静态工具类
 */
class StudentsTool {
    //存储学生信息
    public static Student[] saveStudents(Student... args) {
        if (args.length == 0) {
            System.out.println("信息有误,没有收到学生数据");
            return args;
        }
        return args;
    }

    //展示学生数组的所有学生信息
    public static void showInfo(Student[] students) {
        System.out.println(Arrays.toString(students));
    }

    //展示所有学生的学生姓名以及学校信息
    public static void showSchool(Student[] students) {
        for (Student s : students) {
            s.showSchool();
        }
    }

    //统计指定范围的身高的学生数
    public static void countHeight(Student[] students, double from, double to) {
        int count = 0;
        for (Student s : students) {
            if (s.getHeight() > from && s.getHeight() < to) {
                count++;
            }
        }
        System.out.println("身高范围在" + from + "~" + to + ":" + "有" + count + "人");
    }

    //统计属于指定学校的学生个数
    public static void countSchool(Student[] students, String school) {
        int count = 0;
        for (Student s : students) {
            if (s.getSchool().getSchoolName().equals(school)) {
                count++;
            }
        }
        System.out.println("属于" + school + "的有:" + count + "人");
    }
}

/**
 * 学生类
 */

class School {
    private String schoolName;
    private String schoolAddress;

    public School() {
    }

    public School(String schoolName, String schoolAddress) {
        this.schoolName = schoolName;
        this.schoolAddress = schoolAddress;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public String getSchoolAddress() {
        return schoolAddress;
    }

    public void setSchoolAddress(String schoolAddress) {
        this.schoolAddress = schoolAddress;
    }

    @Override
    public String toString() {
        return "{" +
                "schoolName='" + schoolName + '\'' +
                ", schoolAddress='" + schoolAddress + '\'' +
                '}';
    }
}


class Student {
    private String name;
    private double height;
    private int age;
    private School school;


    public Student() {
    }


    public Student(String name, double height, int age, School school) {
        this.name = name;
        this.height = height;
        this.age = age;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public void showSchool() {
        System.out.println(this.name + ":" + this.school);
    }


    @Override
    public String toString() {
        return "{" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }
}

二、面向对象的思维编程:
*   定义长方形类,含:
*    属性:宽、高(整型);
*    方法:求周长、面积;
*      构造方法3个:(1)无参——宽、高默认值为1;(2)1个参数——宽、高均为参数值;(3)2个参数——宽、高各为参数值。
*   要求:进行测试。
public class Rectangle {

    public static void main(String[] args) {
        Rectangle rectangle=new Rectangle(4,6);
        rectangle.calculArea();
        rectangle.calcuPerimeter();
    }
    private int length;
    private int width;

    public Rectangle() {
    }

    public Rectangle(int length) {
        this.length = length;
        this.width=width;
    }

    public Rectangle(int length, int width) {
        this.length = length;
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void calculArea(){
        System.out.println("该长方形面积为:"+length*width);
    }

    public void calcuPerimeter(){
        System.out.println("该长方形周长为:"+2*(length+width));
    }

    @Override
    public String toString() {
        return "Rectangle{" +
                "length=" + length +
                ", width=" + width +
                '}';
    }
}

三、假如我们在开发一个系统时需要对员工进行建模,【员工】包含3个属性:姓名、工号以及工资。要求如下:

1) 编写员工类

2) 创建一个具体的员工: 名称:上海尚学堂  工号:001  工资:99999

3) 添加2个构造器:  无参构造 与  姓名和工资有参构造

class Test{
    public static void main(String[] args) {
        Staff staff =new Staff("上海尚学堂",99999);
        staff.setStaffNo(001);
        System.out.println(staff);
    }
}
public class Staff {
    private String name;
    private int staffNo;
    private int sal;

    public Staff() {
    }

    public Staff(String name, int sal) {
        this.name = name;
        this.sal = sal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStaffNo() {
        return staffNo;
    }

    public void setStaffNo(int staffNo) {
        this.staffNo = staffNo;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", staffNo=" + staffNo +
                ", sal=" + sal +
                '}';
    }


}

四、Cola公司的雇员分为以下若干类:
(1) ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。

方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
(2) SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。   

属性:月薪
(3) HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。

属性:每小时的工资、每月工作的小时数
(4) SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。

属性:月销售额、提成率

(5) 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

public class ColaCompany {
    public static void main(String[] args) {
        ColaEmployee employee1=new SalariedEmployee("Salsried",4,10000);
        ColaEmployee employee2=new HourEmployee("HourEmployee",2,40,240);
        ColaEmployee employee3=new SalesEmployee("SalesEmployee",5,200000);

        ColaEmployee[] employees=new ColaEmployee[]{employee1,employee2,employee3};
        for (ColaEmployee colaEmployee:employees){
            Company.printSal(colaEmployee,5);
        }
    }
}

class Company{
    //调用该方法可以打印出某月某个员工的工资数额,
    public static void printSal(ColaEmployee colaEmployee,int month){
        colaEmployee.getSalary(month);
    }
}

abstract class ColaEmployee{
    private String name;
    private int birthMonth;

    public ColaEmployee() {
    }

    public ColaEmployee(String name, int birthMonth) {
        this.name = name;
        this.birthMonth = birthMonth;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBirthMonth() {
        return birthMonth;
    }

    public void setBirthMonth(int birthMonth) {
        this.birthMonth = birthMonth;
    }
    //getSalary(int month)根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
    public abstract void getSalary(int month);
}

/**
 * SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。
 *  * 属性:月薪
 */
class SalariedEmployee extends ColaEmployee{
    private int sal;

    public SalariedEmployee() {
    }

    public SalariedEmployee(String name, int birthMonth, int sal) {
        super(name, birthMonth);
        this.sal = sal;
    }

    public int getSal() {
        return sal;
    }

    public void setSal(int sal) {
        this.sal = sal;
    }

    @Override
    public void getSalary(int month) {
        System.out.println("SalariedEmployee:");
        if(getBirthMonth()==month){
            System.out.println("本月工资为:"+sal+100);
        }else{
            System.out.println("本月工资为:"+sal);
        }
        System.out.println("-------------------------------------");
    }
}
/**
 * HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。
 *  * 属性:每小时的工资、每月工作的小时数
 */
class HourEmployee extends ColaEmployee{
    private int hourSal;
    private int workHour;

    public HourEmployee() {

    }

    public HourEmployee(String name, int birthMonth, int hourSal, int workHour) {
        super(name, birthMonth);
        this.hourSal = hourSal;
        this.workHour = workHour;
    }

    public int getHourSal() {
        return hourSal;
    }

    public void setHourSal(int hourSal) {
        this.hourSal = hourSal;
    }

    public int getWorkHour() {
        return workHour;
    }

    public void setWorkHour(int workHour) {
        this.workHour = workHour;
    }

    @Override
    public void getSalary(int month) {
        double sal=0;
        if (getWorkHour() > 160){
            sal=160*hourSal+((workHour-160)*hourSal*1.5);
        }else{
            sal=workHour*hourSal;
        }

        System.out.println("HourEmployee:");
        System.out.println(month==getBirthMonth() ? sal+100 : sal);
        System.out.println("-------------------------------------");
    }
}

/**
 *  SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。
 *  * 属性:月销售额、提成率
 */
class SalesEmployee extends ColaEmployee{
    private int salesVolume;
    private float royaltyRate=0.05f;

    public SalesEmployee() {
    }

    public SalesEmployee(String name, int birthMonth, int salesVolume) {
        super(name, birthMonth);
        this.salesVolume = salesVolume;
    }

    public int getSalesVolume() {
        return salesVolume;
    }

    public void setSalesVolume(int salesVolume) {
        this.salesVolume = salesVolume;
    }

    public float getRoyaltyRate() {
        return royaltyRate;
    }

    public void setRoyaltyRate(float royaltyRate) {
        this.royaltyRate = royaltyRate;
    }

    @Override
    public void getSalary(int month) {
        System.out.println("SalesEmployee:");
        System.out.println(month==getBirthMonth() ? salesVolume*royaltyRate+100 : salesVolume*royaltyRate);
        System.out.println("-------------------------------------");


    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值