Java面向对象练习题答案

//1、设计2个类,要求如下:(知识点:类的继承 方法的覆盖) [必做题]
//
//• 2.1 定义一个汽车类Vehicle,
//
//• 2.1.1 属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
//
//• 2.1.2 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
//
//• 2.1.3 为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
//
//• 2.1.4 定义一个一般方法run(),用打印语句描述汽车奔跑的功能
//
//• 2.1.5 在main方法中创建一个品牌为―benz‖、颜色为―black‖的汽车。
//
//
//• 2.2 定义一个Vehicle类的子类轿车类Car,要求如下:
//
//• 2.2.1 轿车有自己的属性载人数loader(int 类型)。
//
//• 2.2.2 提供该类初始化属性的构造方法。
//
//• 2.2.3 重新定义run(),用打印语句描述轿车奔跑的功能。
//
//• 2.2.4 在main方法中创建一个品牌为―Honda‖、颜色为―red‖,载人数为2人的轿车。

public class Test18 {
    public static void main(String[] args) {
        Vehicle vehicle = new Vehicle("benz", "black", 0);
        Car car = new Car("Honda", "red", 0, 2);
        vehicle.run();
        car.run();
    }
}
    class Vehicle {
        private String brand;
        private String color;
        private double speed;

        public Vehicle(String brand, String color, double speed) {
            this.brand = brand;
            this.color = color;
            this.speed = speed;
        }

        public String getBrand() {
            return brand;
        }

        public void setBrand(String brand) {
            this.brand = brand;
        }

        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public double getSpeed() {
            return speed;
        }

        public void setSpeed(double speed) {
            this.speed = speed;
        }

        public void run() {
            System.out.println(brand+color+"汽车奔跑"+speed);
        }
    }

    class Car extends Vehicle {
        private int loader;
        public Car(String brand, String color, double speed, int loader) {
            super(brand, color, speed);
            this.loader = loader;
        }

        public int getLoader() {
            return loader;
        }

        public void setLoader(int loader) {
            this.loader = loader;
        }

        @Override
        public void run() {
            System.out.println(getBrand()+getColor() +loader +"轿车奔跑"+getSpeed());
        }
    }

//Cola公司的雇员分为以下若干类:(知识点:多态) [必做题]
//
//• 4.1 ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
//• 4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工资的员工。属性:月薪
//
//• 4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
//
//• 4.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率
//
//• 4.5 定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,
// 写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。

public class Test19 {
    public static void main(String[] args) {
        Company company = new Company();
        ColaEmployee[] y=new ColaEmployee[6];//多态(子类可转换为父类数组)
        y[0]=new SalariedEmployee("tofm", 2);
        y[1]=new HourlyEmployee("tom", 4, 4, 170);
        y[2]=new SalariedEmployee("to", 6);
        y[3]=new SalariedEmployee("tm", 3);
        y[4]=new SalesEmployee("tosm", 7, 7, 0.13);
        y[5]=new SalesEmployee("tmu",8, 8, 0.13);
        for(int i=0;i<6;i++){
            company.getSalary(y[i],y[i].getMonth());
        }
    }
}
class ColaEmployee{
    private String name;
    private int month;

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

    public String getName() {
        return name;
    }

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

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public double getSalary(int month){
        return 0;
    }
}

class SalariedEmployee extends ColaEmployee{
    private int salary=3000;
    public SalariedEmployee(String name, int month) {
        super(name, month);
    }
    @Override
    public double getSalary(int month) {
        if(getMonth()==month){
            salary+=100;
            System.out.println("该员工的工资为"+salary);
        }
        else{
            System.out.println("该员工的工资为"+salary);
        }
        return salary;
    }
}

class HourlyEmployee extends ColaEmployee{
    private int salary;
    private int hour;
    public HourlyEmployee(String name, int month, int salary, int hour) {
        super(name, month);
        this.salary = salary;
        this.hour = hour;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }
    double s=0;
    @Override
    public double getSalary(int month) {
        if(hour>160){
            if(getMonth()==month){
                s=salary*160+salary*(hour-160)*1.5+100;
            }
            else{
                s=salary*160+salary*(hour-160)*1.5;
            }
        }
        else{
            if(getMonth()==month){
                s=salary*hour+100;
            }
            else{
                s=salary*hour;
            }
        }
        return s;
    }
}
class  SalesEmployee extends ColaEmployee{
    private int sale;
    private double rate;

    public SalesEmployee(String name, int month, int sale, double rate) {
        super(name, month);
        this.sale = sale;
        this.rate = rate;
    }

    public int getSale() {
        return sale;
    }

    public void setSale(int sale) {
        this.sale = sale;
    }

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    @Override
    public double getSalary(int month) {
        double s=0;
        if(getMonth()==month){
            s=sale*rate+100;
        }
        else{
            s=sale*rate;
        }
        return s;
    }
}
class  Company{
    //	打印出某月某个员工的工资数额
    public void getSalary(ColaEmployee c,int month) {
        System.out.println(c.getName() + "在" + month +
                "月的月薪为" + c.getSalary(month)+"元");
    }
}

//要求编写一个Java应用程序:
//
//(1)编写一个矩形类Rect,包含:
//
//两个protected属性:矩形的宽width;矩形的高height。
//
//两个构造方法:
//
//1.一个带有两个参数的构造方法,用于将width和height属性初化;
//
//2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。
//
//两个方法:
//
//求矩形面积的方法area()
//
//求矩形周长的方法perimeter()
//
//(2)通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用
//
//矩形的左上角坐标来标识,包含:
//
//添加两个属性:矩形左上角坐标startX和startY。
//
//两个构造方法:
//
//带4个参数的构造方法,用于对startX、startY、width和height属性初始化;
//
//不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0的矩形;
//
//添加一个方法:
//
//判断某个点是否在矩形内部的方法isInside(double x,double y)。如在矩
//
//形内,返回true, 否则,返回false。
//
// 提示:点在矩形类是指满足条件:
//
//x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)
//
//(3)编写PlainRect类的测试程序
//
//创建一个左上角坐标为(10,10),长为20,宽为10的矩形对象;
//
//计算并打印输出矩形的面积和周长;
//
//判断点(25.5,13)是否在矩形内,并打印输出相关信息。

public class Test20 {
    public static void main(String[] args) {
        PlainRect plainRect = new PlainRect(10, 10, 20, 10);
        System.out.println("矩形面积是:"+plainRect.area());
        System.out.println("矩形周长事:"+plainRect.perimeter());
        System.out.println("是否在矩形内:"+plainRect.isInside(25.5,13));
    }
}
class Rect{
    protected double width;
    protected double height;
    public void Rect(){
        this.width=10;
        this.height=10;
    }
    public void Rect(double width,double height){
        this.height=height;
        this.width=width;
    }
    public double area(){
        return height*width;
    }
    public double perimeter(){
        return 2*(width+height);
    }
}
class PlainRect extends Rect{
    double startX;
    double startY;
    public PlainRect(double startX, double startY,double width,double height) {
        this.startX = startX;
        this.startY = startY;
        this.width=width;
        this.height=height;
    }

    public PlainRect() {
        startX=0;
        startY=0;
        width=0;
        height=0;
    }
    public boolean isInside(double x,double y){
        if(x>=startX&&x<=(startX+width)&&y<startY&&y>=(startY-height)){
            return true;
        }
        else{
            return false;
        }
    }
}

//1、 请创建一个Animal动物类,要求有方法eat()方法,方法输出一条语句“吃东西”。
// 创建一个接口B,接口里有一个抽象方法fly()。
// 创建一个Bird类继承Animal类并实现 接口A里的方法输出一条有语句“鸟儿飞翔”,重写eat()方法输出一条语句“鸟儿 吃虫”。
// 在Test类中向上转型创建b对象,调用eat方法。然后向下转型调用eat()方 法、fly()方法。

public class Test41 {
    public static void main(String[] args) {
        Animal b=new Bird();//向上转型
        b.eat();
        Bird c=(Bird)b;//向下转型
        c.eat();
        c.fly();
    }
}
class Animal {
    public void eat() {
        System.out.println("吃东西");
    }
}
class Bird extends Animal implements B{

    @Override
    public void eat() {
        System.out.println("鸟儿 吃虫");
    }

    @Override
    public void fly() {
        System.out.println("鸟儿飞翔");
    }
}
//接口B
public interface B {
    void fly();
}
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值