Java面向对象高级特性习题

/*

* 1、创建一个球员类,并且该类最多只允许创建十一个对象。提示利用 static 和 封装性来完成

*/

public class Players {

private static int sum=1;

private Players() {System.out.println("创建了Players对象");}

public static Players create() {

Players p=null;

if(sum<=11) {

p=new Players();

sum++;

}else {

System.out.println("不能再创建对象了");

}

return p;

}

public static void main(String[] args) {

for(int i=0;i<12;i++) {

Players players=Players.create();

}

}

}

运行图:

/*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”的汽车。*/

public class Vehicle {

protected String brand;

protected String color;

protected double speed=0;

public Vehicle() {}

public Vehicle(String brand,String color) {

this.brand=brand;

this.color=color;

}

void run() {

System.out.println("品牌:"+this.brand+"颜色:"+this.color);

}

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;

}

}

/*2.Car类定义*/

public 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) {

this.brand=brand;

this.color=color;

this.loader=loader;

}

void run() {

System.out.println("品牌:"+this.brand+"颜色:"+this.color+"载人数:"+loader);

}

}

在测试类的主函数中加入如下代码

public class Test {

public static void main(String[] args) {

Vehicle v=new Vehicle("benz","black");

v.run();

Car c=new Car("Honda","red",2);

c.run();

}

}

运行图:

/*3、设计三个类,分别如下:(知识点:抽象类及抽象方法)

3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。*/

public abstract class Shape {

protected String color;

protected double area;

protected double per;

public Shape() {}

public Shape(String color) {

this.color=color;

}

public abstract double getArea();

public abstract double getPer();

public abstract String showAll();

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

/*public double getArea() {

return area;

}

public void setArea(double area) {

this.area = area;

}

public double getPer() {

return per;

}

public void setPer(double per) {

this.per = per;

}*/

}

/*3.2 设计 2个子类:

3.2.1  Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。*/

public class Rectangle extends Shape {

private double width;

private double length;

public Rectangle() {}

public Rectangle(double width,double length,String color) {

this.width=width;

this.length=length;

this.color=color;

}

@Override

public double getArea() {

return width*length;

}

@Override

public double getPer() {

return (width+length)*2;

}

@Override

public String showAll() {

return "Rectangle [width:"+width+",length:"+length+",area:"+getArea()+",per:"+getPer()+",color:"+this.color+"]";

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getLength() {

return length;

}

public void setLength(double length) {

this.length = length;

}

}

/*3.2.2  Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)*/

public class Circle extends Shape {

private double radius;

public Circle() {}

public Circle(double radius,String color) {

this.radius=radius;

this.color=color;

}

@Override

public double getArea() {

return Math.PI*radius*radius;

}

@Override

public double getPer() {

return Math.PI*2*radius;

}

@Override

public String showAll() {

return "Circle [radius:"+radius+",area:"+getArea()+",per:"+getPer()+",color:"+this.color+"]";

}

}

/*3.3  在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。*/

public class TestShape {

public static void main(String[] args) {

Shape s1=new Rectangle(8.0,13.0,"blue");

System.out.println(s1.showAll());

Shape s2=new Circle(8.0,"green");

System.out.println(s2.showAll());

}

}

运行图:

/*4、 Cola公司的雇员分为以下若干类:(知识点:多态) 

4.1  ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份。方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。*/

public abstract class ColaEmployee {

protected String name;

protected int month;

protected double monthSalary;

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 getMonthSalary() {

return monthSalary;

}

public void setMonthSalary(double monthSalary) {

this.monthSalary = monthSalary;

}

public abstract double getSalary(int month);

}

/*4.2  SalariedEmployee :  ColaEmployee 的子类,拿固定工资的员工。属性:月薪**/

public class SalariedEmployee extends ColaEmployee {

private double monthsal;

public double getMonthsal() {

return monthsal;

}

public void setMonthsal(double monthsal) {

this.monthsal = monthsal;

}

@Override

public String toString() {

return "SalariedEmployee [name="+ name +",monthsal=" + monthsal

+",salary="+this.monthSalary +"]\n";

}

@Override

public double getSalary(int month) {

if(this.month==month)

{

this.monthSalary+=100;

}

this.monthSalary+=monthsal;

return this.monthSalary;

}

}

/*4.3  HourlyEmployee :ColaEmployee 的子类,按小时拿工资的员工,每月工作超出160 小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数*/

public class HourEmplyees extends ColaEmployee {

private double hourSalary;

private int hours;

public double getHourSalary() {

return hourSalary;

}

public void setHourSalary(double hourSalary) {

this.hourSalary = hourSalary;

}

public int getHours() {

return hours;

}

public void setHours(int hours) {

this.hours = hours;

}

@Override

public String toString() {

return "HourlyEmployee [name="+ name +",hourSalary=" + hourSalary + ", hours=" + hours

+",salary="+this.monthSalary +"]\n";

}

@Override

public double getSalary(int month) {

if(month==this.month)

{

this.monthSalary+=100;

}

if(this.hours>160)

{

this.monthSalary+=hourSalary*160+hourSalary*(this.hours-160)*1.5;

}else

{

this.monthSalary+=hourSalary*this.hours;

}

return this.monthSalary;

}

}

/*4.4  SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和提成率决定。属性:月销售额、提成率*/

public class SalesEmployee extends ColaEmployee {

private double monthSales;

private double rate;

public double getMonthSales() {

return monthSales;

}

public void setMonthSales(double monthSales) {

this.monthSales = monthSales;

}

public double getRate() {

return rate;

}

public void setRate(double rate) {

this.rate = rate;

}

@Override

public String toString() {

return "SalesEmployee [name="+ name +",monthSales=" + monthSales + ", rate=" + rate

+",salary="+this.monthSalary +"]\n";

}

@Override

public double getSalary(int month) {

if(this.month==month)

{

this.monthSalary+=100;

}

this.monthSalary+=this.monthSales*this.rate;

return this.monthSalary;

}

}

/*4.5  定义一个类Company,在该类中写一个方法,调用该方法可以打印出某月某个员工的工资数额,*/

import java.util.Arrays;

public class Company {

public void printSalary(ColaEmployee[] emps)

{

System.out.println(Arrays.toString(emps));

}

}

/*写一个测试类TestCompany,在main方法,把若干各种类型的员工放在一个ColaEmployee 数组里,并单元出数组中每个员工当月的工资。*/

public class TestCompany {

public static void main(String[] args) {

ColaEmployee[] emps1=new HourEmplyees[3];

for (int i = 1; i <= emps1.length; i++) {

HourEmplyees e=new HourEmplyees();

e.setHours(158+i);

e.setHourSalary(30.0);

e.setMonth(i);

e.setName("员工"+i);

e.getSalary(8);//都算8月份的工资

emps1[i-1]=e;

}

ColaEmployee[] emps2=new SalesEmployee[5];

for (int i = 1; i <= emps2.length; i++) {

SalesEmployee e=new SalesEmployee();

e.setName("员工"+(i+3));

e.setMonth(i);

e.setMonthSales(20000.00+i);

e.setRate(0.2);

e.getSalary(8);

emps2[i-1]=e;

}

ColaEmployee[] emps4=new SalariedEmployee[2];

for (int i = 1; i <= emps4.length; i++) {

SalariedEmployee e=new SalariedEmployee();

e.setName("员工"+(i+8));

e.setMonth(i);

e.setMonthsal(20000.00+i);

e.getSalary(8);

emps4[i-1]=e;

}

//把三个数组合成一个数组

ColaEmployee[] emps3=new ColaEmployee[20];

System.arraycopy(emps1, 0, emps3, 0, emps1.length);

System.arraycopy(emps2, 0, emps3, emps1.length, emps2.length);

System.arraycopy(emps4, 0, emps3, emps1.length+emps2.length, emps4.length);

new Company().printSalary(emps3);

}

}

运行图:

/*5、利用接口实现动态的创建对象*/

public interface Fruits {

}

/*5.1  创建4个类:

苹果

5.2  在三种水果的构造方法中打印一句话.

以苹果类为例*/

public class Apple implements Fruits {

public Apple() {

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

}

}

/*香蕉*/

public class Bananas implements Fruits {

public Bananas() {

System.out.println("创建了一个Bananas对象");

}

}

/*葡萄*/

public class Grape implements Fruits {

public Grape() {

System.out.println("创建了一个Grape对象");

}

}

/*园丁

5.3 要求从控制台输入一个字符串,根据字符串的值来判断创建三种水果中哪个类的对象*/

import java.util.Scanner;

public class Gardener {

public static void main(String[] args) {

Gardener g=new Gardener();

g.create();

}

public Fruits create() {

Scanner scan=new Scanner(System.in);

String idx=scan.next();

Fruits fruits=null;

switch(idx) {

case "apple":

fruits=new Apple();

break;

case "banana":

fruits=new Bananas();

break;

case "grape":

fruits=new Grape();

break;

}

return fruits;

}

}

运行图:

/*6、编写三个系别的学生类:英语系,计算机系,文学系(要求通过继承学生类) [选做题]

6.1各系有以下成绩:

  英语系: 演讲,期末考试,期中考试;

  计算机系:操作能力,英语写作,期中考试,期末考试;

  文学系: 演讲,作品,期末考试,期中考试;

 6.2各系总分评测标准:

    英语系:    演讲 50%

                 期末考试 25%

                 期中考试 25%

    计算机系:  操作能力 40%

                英语写作  20%

                期末考试  20%

                期中考试 20%

文学系:  演讲  35%

               作品  35%

               期末考试 15%

               期中考试  15%

6.3定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:

学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX*/

public 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);

}

}

/*英语系: 演讲,期末考试,期中考试;*/

public class English extends Student {

public English(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));

}

}

/*计算机系:操作能力,英语写作,期中考试,期末考试;*/

public class Computer extends Student {

public Computer(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));

}

}

/*文学系: 演讲,作品,期末考试,期中考试;*/

public class Chinese extends Student {

public Chinese(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));

}

}

/*6.3定义一个可容纳5个学生的学生类数组,使用随机数给该数组装入各系学生的对象,然后按如下格式输出数组中的信息:

学号:XXXXXXXX 姓名:XXX 性别:X 年龄:XX 综合成绩:XX*/

public class TestStu {

public static void main(String[] args) {

Student[] stu = { new English(100, "李四", '男', 20, 80, 90, 90), new Computer(101, "王五", '男', 40, 20, 20, 20, 20),

new Chinese(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();

}

}

}

运行图:

说明比较详细的答案可以参考:https://blog.csdn.net/qq_37067955/article/details/81782571

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容

  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔...
    开心的锣鼓阅读 3,276评论 0 9
  • 50道经典Java编程练习题,将数学思维运用到编程中来。抱歉哈找不到文章的原贴了,有冒犯的麻烦知会声哈~ 1.指数...
    OSET我要编程阅读 6,701评论 0 9
  • Java经典问题算法大全 /*【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子...
    赵宇_阿特奇阅读 1,789评论 0 2
  • DAY 05 1、 public classArrayDemo { public static void mai...
    周书达阅读 629评论 0 0
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,013评论 0 2