美文网首页
第七章课后作业

第七章课后作业

作者: 多啦A梦_0213 | 来源:发表于2019-04-17 09:18 被阅读0次

创建一个球员类,并且该类最多只允许创建十一个对象。提示利用 static 和 封装性来完成。 [必做题]
类的说明:


image.png

package bao;

public class kehouti1 {
// 1、创建一个球员类,并且该类最多只允许创建十一个对象。
// 提示利用 static 和 封装性来完成。 [必做题]
public static void main(String[] args) {
for(int i=1;i<13;i++) {
Players.create();
}
}
}
class Players{
static int sum;
Players(){}
static Players create() {
Players p = null;
if(sum++<11) {
p=new Players();
System.out.println("创建了第一个对象");
}else {
System.out.println("创建错误");
}
return p;
}
}
—————————————————————————————

第二种方法

package diqizhang.diqizhang;

public class Players {
private static int sum;
private Players(){}
public static Players create(){
sum = 1;
Players p = null;
while(sum<=11){
p = new Players();
sum++;
System.out.println("创建了一个对象");
}
System.out.println("对不起,已经创建了11个对象。不能再创建对象了");
return p;
}
public static void main(String[] args) {
//对象的创建
Players p=new Players();
p.create();
}
}
————————————————————

第三种方法
package com.neusoft.chapter07;

public class HWBZ01 {
public static void main(String[] args) {
while(true){
if(Players.create() != null){
System.out.println("创建了"+Players.getSum()+"号球员");
}else{
System.out.println("您已经创建了11个球员,不能再继续下去啦");
break;
}
}
}

}
class Players{
private static int sum;
public static int getSum(){
return sum;
}
private Players(){

}
public static Players create(){
    Players p = null;
    if(sum<11){
        sum++;
        p = new Players();
    }
    return p;
    
}

}

==========================================================================
package bao;

public class kehouti2 {
// 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人的轿车。
===========================================

第一种方法

package diqizhang.diqizhang;

public class Vehicle {
// 定义一个汽车类Vehicle,要求如下:
// (a)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
// (b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
// (c)为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
// (d)定义一个一般方法run(),用打印语句描述汽车奔跑的功能
// 定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
public static void main(String[] args) {
Vehicle v=new Vehicle();
v.run();
Car car =new Car("honda","red",2);
car.run();
}
private String brand;
private String color;
private double speed;
Vehicle(){
this.brand="benz";
this.color="black";
}
Vehicle(String brand,String color){
this.brand = brand;
this.color = color;
speed = 0;
}
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(getColor()+"的"+getBrand()+"的速度是"+getSpeed());
}
public String getBrand() {
    return brand;
}

}
// 定义一个Vehicle类的子类轿车类Car,要求如下:
// (a)轿车有自己的属性载人数loader(int 类型)。
// (b)提供该类初始化属性的构造方法。
// (c)重新定义run(),用打印语句描述轿车奔跑的功能。
// (d)定义测试类Test,在其main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。

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

public void run(){
    System.out.println(getColor()+"的"+getBrand()+"载车人数是:"+loader);
}
    }

第二种方法

package chongxie;

public class HWBZ02 {
public static void main(String[] args) {
Vechicle v=new Vechicle("benz", "白色");
v.run();
Car1 c=new Car1("honda", "红色", 2);
c.run();
}
}
class Vechicle{
private String brand;
private String color;
private double speed;
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 Vechicle(String brand, String color) {
super();
this.brand = brand;
this.color = color;
this.speed=0;
}
public void run(){
while(this.getSpeed()<100){
this.setSpeed(10);
System.out.println(this.getColor()+"色的"+this.getBrand()+"牌的以"+this.getSpeed()+"嗷嗷地跑");
}
}

}
class Car1 extends Vechicle{
private int loader;

public int getLoader() {
    return loader;
}

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

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

@Override
public void run() {
    while(this.getSpeed()<100){
        this.setSpeed(10);
        System.out.println(this.getColor()+"色的"+this.getBrand()+"牌的载着"+loader+"人"+this.getSpeed()+"嗷嗷地跑");
    }
}

}


第三种方法

    public static void main(String[] args) {
        Vehicle vh=new Vehicle("benz","black");
        vh.run();
        Car car=new Car(2,"Hongda","red",0.0);
        car.run();
    }
}
class Car extends Vehicle{
    int loader;
    Car(int loader, String brand,String color,double speed){
        super(brand,color);
        this.loader=loader; 
    }
    void run() {
        System.out.println("载车人数"+this.loader+"品牌是"+this.brand+"颜色是"+this.color+"速度是"+this.speed);
    }
    
}
class Vehicle{
    String brand;
    String color;
    double speed;
    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;
    }
    
    Vehicle(String brand,String color){
        this.brand=brand;
        this.color=color;
        this.speed=0;
    }
    void run() {
        System.out.println("品牌是"+this.brand+"颜色是"+this.color+"速度是"+this.speed);
    }
}

========================================================================
package bao;

public class kehouti3 {
// 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方法。


第一种方法

package diqizhang.diqizhang;

public class PolyDemo {

// (3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。
public static void main(String[] args) {
Rectangle r = new Rectangle(1,2,'蓝');
Circle c = new Circle(1.2,'红');
r.showAll();
System.out.println("-----------------");
c.showAll();
System.out.println("-----------------");
}

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

abstract class Shape {
double area;
double per;
char color;
Shape(){

}
Shape(char color){
    this.color = color;
}

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

public char getColor(){
    return color;
}

}
// (2)2个子类:1)Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,
// 重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
class Rectangle extends Shape {
double width;
double height;

Rectangle(){
}

Rectangle(double width, double height,char color){
    super(color);
    this.width = width;
    this.height = height;
}
@Override
public double getArea() {
    area = width*height;
    return area;
}

@Override
public double getPer() {
    per = 2*(width+height);
    return per;
}

@Override
public void showAll() {
    System.out.println("长:"+width);
    System.out.println("宽:"+height);
    System.out.println("面积:"+getArea());
    System.out.println("周长:"+getPer());
    System.out.println("颜色:"+getColor());
}

}
//2)Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,
//另外又增加两个构造方法(为半径、颜色赋值的)。
class Circle extends Shape {
final double pi = 3.14;
double radius;
Circle(){
}
Circle(double radius,char color){
super(color);
this.radius = radius;
}
@Override
public double getArea() {
area = piradiusradius;
return area;
}

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

@Override
public void showAll() {
    System.out.println("半径:"+radius);
    System.out.println("面积:"+getArea());
    System.out.println("周长:"+getPer());
    System.out.println("颜色:"+getColor());

 }

}

第二种方法


利用接口实现动态的创建对象:

(1)创建4个类

1苹果

2香蕉

3葡萄

4园丁

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

以苹果类为例

class apple

{

public apple()

{

System.out.println(“创建了一个苹果类的对象”);

}

}

(3)类图如下:

[图片上传失败...(image-9c9a83-1555327578488)]

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

运行结果如图:

[图片上传失败...(image-678786-1555327578488)]

答案

package bao;

import java.util.Scanner;

public class kehousi {
public static void main(String[] args) {
while(true) {
Gardener g=new Gardener();
g.creat();
}

}

}

abstract interface Fruit {

}

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

class Banana implements Fruit {
Banana() {
System.out.println("创建了一个香蕉类的对象");
}
}

class Putao implements Fruit {
Putao() {
System.out.println("创建了一个葡萄类的对象");
}
}

class Gardener {
public Fruit creat() {
Fruit f = null;
Scanner input = new Scanner(System.in);
String name = input.next();
if (name.equals("苹果")) {
f = new Apple();
} else if (name.equals("香蕉")) {
f = new Banana();
} else if (name.equals("葡萄")) {
f = new Putao();
} else {
System.out.println("不会种");
}
return f;
}
}

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

package bao;

import java.lang.Math;
import java.math.BigDecimal;

public class kehou5 {
    public static void main(String[] args) {
        String sex;
        Student[] a = new Student[5];
        for (int i = 0; i < a.length; i++) {
            sex = "男";
            if (Math.random() < 0.5) {
                sex = "女";
            }
            if (Math.random() * 3 > 2) {

                a[i] = new English((int) (Math.random() * 1000) + " ", (int) (Math.random() * 1000000) + " ", sex,
                        (int) ((Math.random() + 0.1) * 100), (Math.random() + 1) * 100, (Math.random() + 1) * 100,
                        (Math.random() + 1) * 100);

            } else if (Math.random() * 3 > 1) {
                a[i] = new Computer((int) (Math.random() * 1000) + " ", (int) (Math.random() * 1000000) + " ", sex,
                        (int) ((Math.random() + 0.1) * 100), (Math.random() + 1) * 100, (Math.random() + 1) * 100,
                        (Math.random() + 1) * 100, (Math.random() + 1) * 100);

            } else {

                a[i] = new Literature((int) (Math.random() * 1000) + " ", (int) (Math.random() * 1000000) + " ", sex,
                        (int) ((Math.random() + 0.1) * 100), (Math.random() + 1) * 100, (Math.random() + 1) * 100,
                        (Math.random() + 1) * 100, (Math.random() + 1) * 100);
            }
            a[i].show();
        }

    }
}

class Literature extends Student {
    double speekScore;
    double composition;

    public Literature(String id, String name, String sex, int age, double qzscore, double qmscore, double speekScore,
            double composition) {
        super(id, name, age, sex, qzscore, qmscore);
        this.speekScore = speekScore;
        this.composition = composition;
    }

    @Override
    public double getScore() {
        double f = speekScore * 0.35 + composition * 0.35 + qmscore * 0.15 + qzscore * 0.15;
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

}

class Computer extends Student {
    double makeScore;
    double engScore;

    public Computer() {
    }

    public Computer(String id, String name, String sex, int age, double qzscore, double qmscore, double makeScore,
            double engScore) {
        super(id, name, age, sex, qzscore, qmscore);
        this.makeScore = makeScore;
        this.engScore = engScore;
    }

    public double getScore() {
        double f = makeScore * 0.4 + engScore * 0.2 + qmscore * 0.2 + qzscore * 0.2;
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

}

class English extends Student {
    double speekScore;

    public English() {
    }

    public English(String id, String name, String sex, int age, double qzscore, double qmscore, double speekScore) {
        super(id, name, age, sex, qzscore, qmscore);
        this.speekScore = speekScore;
    }

    @Override
    public double getScore() {
        double f = speekScore * 0.5 + qmscore * 0.25 + qzscore * 0.25;
        BigDecimal b = new BigDecimal(f);
        double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }
}

abstract class Student {
    String id;
    String name;
    int age;
    String sex;
    double qzscore;// 期中考试
    double qmscore;// 期末考试

    public Student(String id, String name, int age, String sex, double qzscore, double qmscore) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.qzscore = qzscore;
        this.qmscore = qmscore;
    }

    Student() {
    }

    public abstract double getScore();// 获得综合成绩

    public void show() {
        System.out.println("学号:" + id + "姓名" + name + "性别:" + sex + "年龄:" + age + "综合成绩:" + getScore());
    }
}

相关文章

  • 第七章课后作业

    创建一个球员类,并且该类最多只允许创建十一个对象。提示利用 static 和 封装性来完成。 [必做题]类的说明...

  • 眼睛

    课后作业

  • W14L19-L20-电商法律问题

    课后作业 ...

  • W14L19-L20-电商法律问题

    课后作业 ...

  • 函数值域专题

    课后作业

  • python通用编程系列

    本系列用于python通用编程作业。 第七章函数对象深度之眼链接:第七章函数对象作业如下:

  • 有效教学

    5.设计课后作业,拓展互动空间 为了保证教学的有效性,教师还可以让互动交流的氛围延伸到课后。其方法就是借助课后作业...

  • 课后作业

    小明推开家门,冷冷清清,屋子里静的能听到自己的呼吸声,虽然每次回家都是这样,但还是有点小失望。 他瘦小的身躯用力的...

  • 课后作业

    1.我坐在电脑前边吃西瓜边看“长大后我要当太空人”的喜之郎广告,这时候我同学来问我书本上的问题,走过来的时候被电线...

  • 课后作业

    广联达服务管理部李维 老师,我不是写作,只是做课程宣传的!起初写一些内容,是为了让课程能够有更多人参加,人数也是我...

网友评论

      本文标题:第七章课后作业

      本文链接:https://www.haomeiwen.com/subject/jpshwqtx.html