JavaSE核心技术——面向对象高级特性练习题

1、 创建一个球员类,并且该类最多只允许创建十一个对象。提示利用 static 和 封装性来完成。 [必做题]
类图如下:
这里写图片描述
效果如下:
这里写图片描述

Player 类 代码:
public class Player {

//  sum变量用来统计当前创建的对象个数,static修饰的变量与对象无关
//  只与类有关,为所有对象共享
    static int sum = 0;

//  因为只能创建11个对象,所以构造器不能被外界随便拿到,因此设为private
    private Player(){
//      每创建一个对象都需要将sum加1
        sum++;
    }

//  为了让create调用是不用创建player类型的对象,所以将方法设为Static.
//  num用来控制创建对象的数量,可以在控制台输入。
    public static Player Create( int num){

//      定义一个player类型的变量p,这个变量有两种可能取值,
//      一种是player类型的对象,还有一种是null,具体取值由控制语句决定
//      最后方法结束时,将p变量返回出去。
        Player p = null;

//      因为有两个分支,无论走哪都需要但会一个结果
        if(sum< num){
            p =  new Player();
        }else{
            p =  null;
        }
        return p;
    }
}
测试类Test 代码:

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        System.out.println("请输入需要创建的球员对象数量……");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();

//      定义一个p变量用于接收调用create()方法的结果;
            Player p ;
//      设定一个死循环,多次调用player中的create()方法;
        while(true){

//          由于create方法是Static,所以直接用雷鸣,方法即可调用。
             p = Player.Create(num);
//               对p变量接收到的结果进行判断
             if(p != null){
//                   如果不为空,打印语句后再次执行循环
                 System.out.println("创建了球员"+Player.sum+"号");
                 }else{
//                   如果为空,打印语句后跳出循环
                 System.out.println("您已经创建了"+num+"个球员对象,不可以再继续创建");
                 break;
             }
        }
    }
}
执行结果:

这里写图片描述


2、设计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人的轿车。

Vehiclel类代码:

public class Vehicle {
    private String brand;
    private String color;
    private double speed;

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

    public Vehicle(){

    }

    public String getBrand() {
        return 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(double num){
        while(true){

//          用来显示循环间隔,车辆提速间隔时间
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(this.getSpeed()<num){
                System.out.println("车辆正在提速,当前车速为"+this.getSpeed());
            }else{
                break;
            }
            this.setSpeed(this.getSpeed()+10);
        }
        System.out.println(this.getColor()+"色的"+this.getBrand()+"品牌的的汽车以"+this.getSpeed()+"的速度在行驶。");
    }
}
Car类 代码:
class Car extends Vehicle {

    private int loader;

    public int getLoader() {
        return loader;
    }

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

    public Car(String brand, String color, int loader) {
        super(brand,color);
        this.loader = loader;
    }

     public void run(double num){
        while(true){

//          用来显示循环间隔,车辆提速间隔时间
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if(this.getSpeed()<num){
                System.out.println("车辆正在提速,当前车速为"+this.getSpeed());
            }else{
                break;
            }
            this.setSpeed(this.getSpeed()+10);
        }
        System.out.println(this.getColor()+"色的,载着"+this.getLoader()+"人的"+this.getBrand()+"品牌的的汽车以"+this.getSpeed()+"的速度在行驶。");
    }
}
测试类VehicleTest类代码:
import java.util.Scanner;

public class VehicleTest {
    public static void main(String[] args) {
        System.out.println("请输入汽车行驶的速度……");
        Scanner in = new Scanner(System.in);
        double speed = in.nextDouble();

        Vehicle v = new Vehicle("benz", "红");
        v.run(speed);

        Car car = new  Car("honda","红",2);
        car.run(speed);
    }
}
执行结果:

这里写图片描述


3、设计三个类,分别如下:(知识点:抽象类及抽象方法) [必做题]
• 3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
• 3.2 设计 2个子类:
• 3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
• 3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
• 3.3 在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。

Shape 类  代码:

public abstract class Shape {

     double area;
     double per;
     String color;



     public Shape(){

     }

     public Shape(String color){
         this.color = color;
     }

     public abstract double getArea();
     public abstract double getPer();
     public abstract void showAll();

     public String getColor(){
         return this.color;
     }


}
Rectangle 子类 代码:

public class Rectangle extends Shape {

    int weidth;
    int height;

    public Rectangle() {

    }

    public Rectangle(int weidth,int height,String color){
        super(color);
        this.weidth = weidth;
        this.height = height;
    }


    public double getArea() {

        this.area = weidth * height; 
        return this.area;
    }

    public double getPer() {
        this.per = 2 * (weidth + height);
        return this.per;
    }

    public void showAll() {
        System.out.println("该矩形长为"+this.weidth+",宽为"+this.height+",周长为"+this.getPer()+",面积为"+this.getArea()+",颜色为"+this.getColor());

    }

}
Circle 子类 代码:

public class Circle extends Shape {
    double radius;
    double pi = Math.PI;

    public Circle(){

    }
    public Circle(double radius,String color){
            super(color);
            this.radius = radius;
        }

    public double getArea() {
          double area =pi * radius *radius;
        return area;
    }

    public double getPer() {
        double per = 2 * pi * radius;
        return per;
    }

    public void showAll() {
        System.out.println("半径为"+this.radius+"的圆,周长为"+this.getPer()+",面积为"+this.getArea()+",颜色为"+this.getColor());

    }
}

测试类Test 代码:

public class Test {
    public static void main(String[] args) {
        Rectangle rect=new Rectangle(2,4,"red");
        rect.showAll();

        Circle cir=new Circle(2,"green");
        cir.showAll();
    }
}
执行结果:

这里写图片描述


4、 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 数组里,并单元出数组中每个员工当月的工资。

ColaEmployee 类 代码:

class ColaEmployee {
    String name;
    int mouth;



    public ColaEmployee(String name, int mouth) {
        // TODO Auto-generated constructor stub
        this.name = name;
        this.mouth = mouth;
    }



    public String getName() {
        return name;
    }

    public int getMouth() {
        return mouth;
    }

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

}
 SalariedEmployee 类 代码:

public class SalariedEmployee extends ColaEmployee {
    double salary;

    public SalariedEmployee(String name,int mouth,int salary){
        super(name,mouth);
        this.salary = salary;
    }

    public double getSalary(int mouth){
        if(this.mouth == mouth){
            return salary+100;
        }else{
            return salary;
        }
    }
}
HourlyEmployee 子类 代码:

public class HourlyEmployee extends ColaEmployee {
    double salaryHour;
    int hour;

    public HourlyEmployee(String name,int mouth,double salaryHour,int hour){
        super(name,mouth);
        this.salaryHour = salaryHour;
        this.hour = hour;
    }

    public double getSalary(int mouth) {
        if(this.mouth == mouth){
            if(hour<=160){
                return salaryHour*hour+100;
            }else{
                return 160*salaryHour + (hour - 160)*1.5*salaryHour + 100;
            }
        }else{
            if(hour<=160){
                return salaryHour*hour;
            }else{
                return 160*salaryHour + (hour - 160)*1.5*salaryHour;
            }
        }
    }

}
SalesEmployee 子类 代码:
public class SalesEmployee extends ColaEmployee {
    double sales;
    double rate;

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

    public double getSalary(int mouth) {
        if(this.mouth == mouth){
            return sales * rate + 100;
        }else{
            return sales * rate + 100;
        }
    }
}
Company 子类 代码:

public class Company {
    public void getSalary(ColaEmployee c,int mouth){
        System.out.println(c.name+"的在"+mouth+"月的月薪为"+c.getSalary(mouth));
    }
}
测试类 Test 类 代码:
public class Test {
    public static void main(String[] args) {
        ColaEmployee[] c = {new SalariedEmployee("靳东", 1, 3000),
                            new HourlyEmployee("王凯", 2, 20,180),
                            new SalesEmployee("胡歌",3,300000,0.01)};

        for(int i =0;i<c.length;i++ ){
            new Company().getSalary(c[i], 3);
        }
    }
}

执行结果:
这里写图片描述


5、利用接口实现动态的创建对象[选做题]
• 5.1 创建4个类:
• 苹果
• 香蕉
• 葡萄
• 园丁
• 5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例

class apple{
    public apple(){
        System.out.println("创建了一个苹果类的对象");
    }
}

类图如下:
这里写图片描述
5.3 要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象
效果图如下:
这里写图片描述

Furit 接口 代码:
interface Fruit {

}
Apple 类 代码:
class Apple implements Fruit {

    public Apple() {
        System.out.println("创建了一个苹果对象");

    }

}
Pear 类 代码:
class Pear implements Fruit{

    public Pear() {
        System.out.println("创建了一个梨对象");

    }

}
Oranges 类 代码:
class Oranges implements Fruit {

    public Oranges() {
        System.out.println("创建了一个橘子对象");

    }

}
Grander 类 代码:
import java.util.Scanner;

class Gardener {
    Fruit Create(){
        System.out.println("请输入需要创建的对象名……");
        Scanner in = new Scanner (System.in);
        String f = in.next();

        Fruit fruit = null;
        if("苹果".equals(f)){
            fruit =  new Apple();
        }
        if("梨".equals(f)){
            fruit =  new Pear();
        }
        if("橘子".equals(f)){
            fruit = new Oranges();
        }
        return fruit;
    }
}
测试类Test 代码:
public class Test {
    public static void main(String[] args) {
        new Gardener().Create();
    }
}

6、编写三个系别的学生类:英语系,计算机系,文学系(要求通过继承学生类) [选做题]
• 6.1各系有以下成绩:
• 英语系: 演讲,期末考试,期中考试;
• 计算机系:操作能力,英语写作,期中考试,期末考试;
• 文学系: 演讲,作品,期末考试,期中考试;
• 6.2各系总分评测标准:
• 英语系: 演讲 50%
• 期末考试 25%
• 期中考试 25%
• 计算机系: 操作能力 40%
• 英语写作 20%
• 期末考试 20%
• 期中考试 20%
• 文学系: 演讲 35%
• 作品 35%
• 期末考试 15%
• 期中考试 15%
• 6.3定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:
• 学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX

Student 类 代码:
class Student {
    int no;
    String name;
    char sex;
    int age;
    double score;
    public Student(int no, String name, char sex, int age, double score) {
        this.no = no;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.score = score;
    }

    public void Show(){
        System.out.println("学号:"+this.no+" 姓名:"+this.name+" 性别:"+this.sex+" 年龄:"+this.age+" 综合成绩:"+this.score);
    }
}
EnglishStudent 类 代码:
public class EnglishStudent extends Student{

        public EnglishStudent(int no, String name, char sex, int age, double speechScore,double middleScore,double finalScore){
        super(no,name,sex,age,(0.5*speechScore+0.25*middleScore+0.25*finalScore));

    }
}
ComputerStudent 类 代码:
public class ComputerStudent extends Student{

    public ComputerStudent(int no, String name, char sex, int age,double operateScore,double writeScore,double middleScore,double finalScore) {
        super(no, name, sex, age, (operateScore*0.4+writeScore*0.2+0.2*middleScore+0.2*finalScore));
    }
}
ChineseStudent 类 代码:
public class ChineseStudent extends Student{

    public ChineseStudent(int no, String name, char sex, int age,double speechScore,double articleScore,double middleScore,double finalScore) {
        super(no, name, sex, age, (0.35*speechScore+0.35*articleScore+0.15*middleScore+0.15*finalScore));
    }
}
测试类Test 代码:
public class Test {
    public static void main(String[] args) {
        Student[] stu = {new EnglishStudent(100, "李雷", '男', 20,  80, 90, 90),
                         new ComputerStudent(101, "马化腾", '男', 40, 20, 20, 20, 20),
                         new ChineseStudent(102, "周树人", '男', 30, 80, 100, 95, 95)};
        Student[] stu1  = new Student[stu.length];


        int[] t = {1,2,3};

        for(int i = 0;i<stu1.length;i++){
            int num = (int)(Math.random()*stu.length);
            while(stu[num] == null){
                num = (int)(Math.random()*stu.length);
            }
            stu1[i] = stu[num];
            stu[num] = null ;

        }

        for(int i = 0 ;i<stu1.length;i++){
            stu1[i].Show();
        }
    }

}
执行结果:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_37067955/article/details/81782571