面向对象基础的Java作业八

1.编写程序,设计一个Animal接口,要求如下:

(1)编写Animal接口,接口中定义sleep()方法。

(2)Cat类实现Animal接口的方法,并定义另一个方法catchMouse()

(3)编写测试类Test01,使Cat对象指向父类Animal的引用 ,并通过该引用调用sleep()方法。

1.代码

interface Animal {
    void sleep();
}

// 创建Cat类,实现Animal接口
class Cat implements Animal {
    // 实现sleep()方法
    public void sleep() {
        System.out.println("猫在睡觉");
    }

    // 定义catchMouse()方法
    public void catchMouse() {
        System.out.println("猫在抓老鼠");
    }
}

// 编写测试类Test01
public class Test01 {
    public static void main(String[] args) {
        // 创建Cat对象
        Cat cat = new Cat();
        // 通过父类Animal的引用调用sleep()方法
        ((Animal) cat).sleep();
    }
}

2.运行效果:

猫在睡觉

2.设计一个Shape接口和它的两个实现类Square和Circle,要求如下:

(1) Shape接口中有一个抽象方法area(),方法接收有一个double类型的参数,返回一个double类型的结果;

(2) Square和Circle中实现了Shape接口的area()抽象方法,分别求正方形和圆形的面积并返回;

(3)在Test02测试类中创建Square和Circle对象,计算边长为2的正方形面积和半径为3的圆形面积。

1.代码

interface Shape {
    double area(double parameter);
}

class Square implements Shape {
    public double area(double side) {
        return side * side;
    }
}

class Circle implements Shape {
    public double area(double radius) {
        return Math.PI * radius * radius;
    }
}

public class Test02 {
    public static void main(String[] args) {
        // 创建Square对象
        Square square = new Square();
        // 计算边长为2的正方形面积
        double squareArea = square.area(2);
        System.out.println("边长为2的正方形面积为:" + squareArea);

        // 创建Circle对象
        Circle circle = new Circle();
        // 计算半径为3的圆形面积
        double circleArea = circle.area(3);
        System.out.println("半径为3的圆形面积为:" + circleArea);
    }
}

2.运行效果:

边长为2的正方形面积为:4.0

半径为3的圆形面积为:28.274333882308138

3.编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。

(1) 编写一个圆类Circle,该类拥有:

1. 一个成员变量,radius(私有,浮点型);//存放圆的半径;

2. 两个构造方法

Circle()              //将半径设为0

Circle(double  r )    //创建Circle对象时将半径初始化为r

3. 三个成员方法

double getArea()      //获取圆的面积

double getPerimeter()  //获取圆的周长

void show()           //将圆的关径、周长、面积输出到屏幕

(2) 编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:

1.一个成员变量,double hight  (私有,浮点型);  //圆柱体的高;

2.构造方法//创建Cylinder对象时将半径初始化为r,高度初始化为h

Cylinder(double r,double h)     

3.成员方法

double getVolume()            //获取圆柱体的体积

void showVolume()             //将圆柱体的体积输出到屏幕

  1. 代码
class Circle {
    private double radius; // 存放圆的半径

    public Circle() {
        this.radius = 0;
    }

    public Circle(double r) {
        this.radius = r;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }

    public void show() {
        System.out.println("圆的半径:" + radius);
        System.out.println("圆的面积:" + getArea());
        System.out.println("圆的周长:" + getPerimeter());
    }

    public double area(int i) {
        return 0;
    }
}

// 圆柱体类
class Cylinder extends Circle {
    private double height; // 圆柱体的高

    public Cylinder(double r, double h) {
        super(r);
        this.height = h;
    }

    public double getVolume() {
        return getArea() * height;
    }

    public void showVolume() {
        System.out.println("圆柱体的体积:" + getVolume());
    }
}

public class Test03 {
    public static void main(String[] args) {
        Circle circle = new Circle();
        circle.show();

        Cylinder cylinder = new Cylinder(5, 10);
        cylinder.show();
        cylinder.showVolume();
    }
}

2.运行效果:

圆的半径:0.0

圆的面积:0.0

圆的周长:0.0

圆的半径:5.0

圆的面积:78.53981633974483

圆的周长:31.41592653589793

圆柱体的体积:785.3981633974483

4.编写程序实现乐手弹奏乐器。乐手可以弹奏不同的乐器从而发出不同的声音。可以弹奏的乐器包括二胡、钢琴和琵琶。

(1) 定义乐器类Instrument,包括方法makeSound()

(2) 定义乐器类的子类:二胡Erhu、钢琴Piano和小提琴Violin

(3) 定义乐手类Musician,可以弹奏各种乐器play(Instrument i)

(4) 定义测试类,给乐手不同的乐器让他弹奏

1.代码

abstract class Instrument {
    public abstract void makeSound();
}

class Erhu extends Instrument {
    @Override
    public void makeSound() {
        System.out.println("二胡发出声音");
    }
}

class Piano extends Instrument {
    @Override
    public void makeSound() {
        System.out.println("钢琴发出声音");
    }
}

class Violin extends Instrument {
    @Override
    public void makeSound() {
        System.out.println("小提琴发出声音");
    }
}

class Musician {
    public void play(Instrument i) {
        i.makeSound();
    }
}

public class Test04 {
    public static void main(String[] args) {
        Musician musician = new Musician();
        Instrument erhu = new Erhu();
        Instrument piano = new Piano();
        Instrument violin = new Violin();

        musician.play(erhu);
        musician.play(piano);
        musician.play(violin);
    }
}

2.运行效果:

二胡发出声音

钢琴发出声音

小提琴发出声音

5.编写程序描述影视歌三栖艺人,需求说明:请使用面向对象的思想,设计自定义类,描述影视歌三梄艺人。

(1)分析影视歌三栖艺人的特性

1.可以演电影

2.可以演电视剧

3.可以唱歌

(2)定义多个接口描述特性

1.演电影的接口-----方法:演电影

2.演电视剧的接口-----方法:演电视剧

3.唱歌的接口-----方法:唱歌

(3)定义艺人类实现多个接口

1.代码

interface MovieActor {
    void actInMovie();
}

interface TVSeriesActor {
    void actInTVSeries();
}

interface Singer {
    void sing();
}

class MultiRoleArtist implements MovieActor, TVSeriesActor, Singer {
    @Override
    public void actInMovie() {
        System.out.println("演电影");
    }

    @Override
    public void actInTVSeries() {
        System.out.println("演电视剧");
    }

    @Override
    public void sing() {
        System.out.println("唱歌");
    }
}

public class Test05 {
    public static void main(String[] args) {
        MultiRoleArtist artist = new MultiRoleArtist();
        artist.actInMovie();
        artist.actInTVSeries();
        artist.sing();
    }
}

2.运行效果:

演电影

演电视剧

唱歌

6.编写 Java 程序,定义Test07类进行测试。自定义一个异常类NoThisSoundException和Player类,在Player的play()方法中使用自定义异常,要求入下:

(1) NoThisSongException继承Exception类,类中有一个无参和一个接收一个String类型参数的构造方法,构造方法中都使用super关键字调用父类的构造方法。

(2) Player类中定义一个play(int index)方法,方法接收一个int类型的参数,表示播放歌曲的索引,当index>10时,paly()方法用throw关键字抛出NoThisSongException异常,创建异常对象时,调用有参的构造方法,传入“您播放的歌曲不存在”。

(3)在测试类中创建Player对象,并调用play()方法测试自定义的NoThisSongException异常,使用try…catch语句捕获异常,调用NoThisSongException的getMessage()方法打印出异常信息。

1.代码

// 自定义异常类NoThisSoundException
class NoThisSoundException extends Exception {
    public NoThisSoundException() {
        super();
    }

    public NoThisSoundException(String message) {
        super(message);
    }
}

// Player类
class Player {
    public void play(int index) throws NoThisSoundException {
        if (index > 10) {
            throw new NoThisSoundException("异常信息:您播放的歌曲不存在");
        }
    }
}

// 测试类
public class Test07 {
    public static void main(String[] args) {
        Player player = new Player();
        try {
            player.play(11);
        } catch (NoThisSoundException e) {
            System.out.println(e.getMessage());
        }
    }
}

2.运行效果:

三、思考题1,编写程序,根据下图所示的类族,分别定义员工类、研发部员工类、维护部员工类等,并写出相关测试结果。

注:掌握定义类的语法结构、掌握抽象类的使用、掌握类的继承。

1.代码

class Employee {
    private String name;
    private int age;
    private String department;

    public Employee(String name, int age, String department) {
        this.name = name;
        this.age = age;
        this.department = department;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

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

 class RDEmployee extends Employee {
    private String position;

    public RDEmployee(String name, int age, String department, String position) {
        super(name, age, department);
        this.position = position;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    @Override
    public String toString() {
        return "RDEmployee{" +
                "name='" + getName() + '\'' +
                ", age=" + getAge() +
                ", department='" + getDepartment() + '\'' +
                ", position='" + position + '\'' +
                '}';
    }
}

 class MWEmployee extends Employee {
    private String skill;

    public MWEmployee(String name, int age, String department, String skill) {
        super(name, age, department);
        this.skill = skill;
    }

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }

    @Override
    public String toString() {
        return "MWEmployee{" +
                "name='" + getName() + '\'' +
                ", age=" + getAge() +
                ", department='" + getDepartment() + '\'' +
                ", skill='" + skill + '\'' +
                '}';
    }
}

public class TestEmployee {
    public static void main(String[] args) {
        Employee employee = new Employee("张三", 30, "研发部");
        System.out.println(employee);

        RDEmployee rDEmployee = new RDEmployee("李四", 28, "研发部", "高级工程师");
        System.out.println(rDEmployee);

        MWEmployee mwEmployee = new MWEmployee("王五", 35, "维护部", "网络工程师");
        System.out.println(mwEmployee);
    }
}

2.运行效果:

Employee{name='张三', age=30, department='研发部'}

RDEmployee{name='李四', age=28, department='研发部', position='高级工程师'}

MWEmployee{name='王五', age=35, department='维护部', skill='网络工程师'}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你二舅ya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值