java-day13-面向对象练习题

习题1

建立一个圆形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得到两个圆形的面积。
注意:体现面向对象的特性,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。、

package day13;

class NotValueException extends Exception{
    NotValueException(){
        super();
    }
    NotValueException(String message){
        super(message);
    }
}

//建立一个图形接口
interface Graph{
    double getArea();//
}

// 圆形实现图形接口
class Cicle implements Graph{
    private double radius; //半径
    public Cicle(double radius) throws NotValueException{
        if(radius<0)
            throw new NotValueException("这个数值是非法的");
        this.radius = radius;
    }
    public double getArea(){
        return Math.PI * radius * radius;
    }
}

class Rectangle implements Graph{
    private double length; //长
    private double width;// 宽
    public Rectangle(double length, double width) throws NotValueException{
        if(length<0 || width<0)
            throw new NotValueException("这个数值是非法的");
        this.length = length;
        this.width = width;
    }
    public double getArea(){
        return width*width;
    }
}

public class Test1 {
    public static void main(String[] arg){

        try{
            Cicle cicle = new Cicle(-1);
            System.out.println("圆的面积为:"+cicle.getArea());
        }catch (NotValueException e){
            System.out.println(e.toString());
        }
        try{
            Rectangle rectangle = new Rectangle(1,2);
            System.out.println("矩形的面积为:"+rectangle.getArea());
        }catch (NotValueException e){
            System.out.println(e.toString());
        }

    }
}

结果输出:

day13.NotValueException: 这个数值是非法的
矩形的面积为:4.0

习题2

  1. 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1.
  2. 要搜索的字符数组和字符都以参数形式传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。要搜索的字符数组和字符都以参数形式传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。
  3. 在类main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。 在类main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。
package day13;

class Demo{
    public int searchChar(char[] str, char key){
        if(str == null || key == ' '){
            throw new IllegalArgumentException("非法传值");
        }
        for(int i=0; i<str.length; i++){
            if(str[i] == key) return i;
        }
        return -1;
    }
}
public class Test2 {
    public static void main(String[] args){

        Demo demo = new Demo();
        char[] str = null;
        System.out.println(demo.searchChar(str,'1'));

    }
}

输出:

Exception in thread "main" java.lang.IllegalArgumentException: 非法传值
	at day13.Demo.searchChar(Test2.java:6)
	at day13.Test2.main(Test2.java:19)

习题3

补足compare函数内的代码,不许添加其他函数

package day13;

class Cicle1{
    private double radius;
    public Cicle1(double radius){
        this.radius = radius;
    }
    public static double compare(Cicle[] cir){
        //程序代码
        
    }
}
public class Test3 {
    public static void main(String[] args){
        Cicle1 cir[] = new Cicle1[3];
        cir[0] = new Cicle1(1.0);
        cir[1] = new Cicle1(2.0);
        cir[2] = new Cicle1(4.0);
        System.out.println("最大的半径是:"+Cicle1.compare(cir));
        
    }
}

补充代码如下:

package day13;

class Circle{
    private double radius;
    public Circle(double radius){
        this.radius = radius;
    }
    public static double compare(Circle[] cir){
        //程序代码
        if(cir == null)
            throw new IllegalArgumentException("非法传值");
        double maxR = cir[0].radius;
        for(int i=0; i<cir.length; i++){
            if(cir[i].radius>maxR)
                maxR = cir[i].radius;
        }
        return maxR;
    }
}
public class Test3 {
    public static void main(String[] args){
        Circle cir[] = new Circle[3];
        cir[0] = new Circle(1.0);
        cir[1] = new Circle(2.0);
        cir[2] = new Circle(4.0);
        System.out.println("最大的半径是:"+Circle.compare(cir));

    }
}

输出结果为:

最大的半径是:4.0

习题4

描述Person

  1. 属性:姓名和年龄
  2. 行为:
    说出姓名和年龄
    判断是否是同一个人(同姓名、同年龄视为同一个人)
  3. 提示:判断姓名相同的方法到AP文档String类中查找
  4. 注意:对象判断是否相同用equals,String判断是否相同也是用equals。
package day13;

import java.util.Objects;

class Person{
    private String name;
    private int age;
    public Person(){}
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName(){
        return name;
    }

    public int getAge() {
        return age;
    }

    /*
    既然要判断对象是否相同,直接使用Object类中的方法。
    但是还是要依据子类的特征来判断,直接覆盖Object类中的方法equals
     */
    public boolean equals(Object obj){
        //判断姓名和年龄是否相同,这些都是Person的属性,所以必须向下转型。
        if(!(obj instanceof Person))
            throw new ClassCastException("类型错误");

        Person p = (Person)obj;
        return this.name.equals(p.name) && this.age == p.age;
    }
}
public class Test4 {
    public static void main(String[] args){

        Person p1 = new Person("huanhuan",1);
        System.out.println("姓名:"+p1.getName()+",年龄:"+p1.getAge());
        System.out.println(p1.equals(new Person("huanhuan",2)));
    }
}

输出

姓名:huanhuan,年龄:1
false

习题5

补足以下代码:

package day13;

class Circle1{
    private double radius;
    public Circle1(double radius){
        this.radius = radius;
    }
    //请定义功能:比较两个圆是否一样大
}
public class Test5 {
    
    public static void main(String[] args){
        Circle1 c1 = new Circle1(2.0);
        Circle1 c2 = new Circle1(2.0);
        boolean b = c1.equals(c2);
        System.out.println("两个圆是否一样大:"+b);
    }     
}

对象判断是否相同用equals,补充代码如下:

package day13;

import java.util.Objects;

class Circle1{
    private double radius;
    public Circle1(double radius){
        this.radius = radius;
    }
    //请定义功能:比较两个圆是否一样大
    public boolean equals(Object obj){
        if(!(obj instanceof Circle1))
            throw new ClassCastException(obj.getClass().getName()+"类型错误");
        Circle1 c = (Circle1)obj;
        return this.radius == c.radius;
    }
}
public class Test5 {

    public static void main(String[] args){
        Circle1 c1 = new Circle1(2.0);
        Circle1 c2 = new Circle1(2.0);
        boolean b = c1.equals(c2);
        System.out.println("两个圆是否一样大:"+b);
    }


}

输出:

两个圆是否一样大:false

习题6

class OuterClass{
    private double d1 = 1.0;
    //insert code here
}

把下面选项放在指定的code位置上,哪个答案是正确的,阐述原因:

A
class InnerClass{
        static double method(){
            return d1;
        }
}

B
public Class InnerOne{
        double method(){
            return d1;
        }
}

C
private class InnerOne{
        protected double method(){
            return d1;
        }
}

D
static class InnerOne{
        protected double method(){
            return d1;
        }
}

E
abstract class InnerOne{
        public abstract double method();
}

分析:
A错误:因为非静态的内部类,不可以定义静态方法
B错误:因为非静态的内部类,不可以定义静态方法
C正确
D错误,静态内部类只能访问,外部类中的静态成员
E正确

习题7

写出下面代码的执行结果(需写出分析过程)

package day13;

class A{
    void fun1(){
        System.out.println(fun2());
    }
    
    int fun2(){
        return 123;
    }
}
class B extends A{
    int fun2(){
        return 456;
    }
}

public class Test7 {
    public static void main(String[] args){
        A a;
        B b = new B();
        b.fun1();
        a = b;
        a.fun1();
    }
}

输出结果:

456
456

分析:

B b = new B();
b.fun1();

B中没有fun1,所以这里的fun1是调用父类的。再看父类中的fun1:

void fun1(){
        System.out.println(fun2());
    }

这里实际省略了一个this:

void fun1(){
        System.out.println(this.fun2());
        // this是本类型,但是指向子类
        //实际指的是this = new B();也可以看成 A a = new B(); a.fun2();
        //所以输出456
    }
a = b;
a.fun1();

这其实也是一样:
a = b就是A a = new B();
再看a.fun1();

void fun1(){
        System.out.println(fun2());
    }

这里实际省略了一个this:

void fun1(){
        System.out.println(this.fun2());
        // this是本类型引用,即A,但是它指向子类
        //实际指的是this = new B();也可以看成 A a = new B(); a.fun2();
        //所以输出456
    }

习题8

写出下面代码的执行结果,(需写出分析过程)

import java.io.IOException;

public class Test8 {
    public static void main(String[] args){
        try{
            new Test8().methodA(5);
        }catch (IOException e){
            System.out.println("caught Exception");
        }catch (Exception e){
            System.out.println("caught Exception");
        } finally {
            System.out.println("no Exception");
        }
    }

    void methodA(int i) throws IOException{
        if(i%2 != 0){
            throw new IOException("methodA IOException");
        }
    }
}

输出:

caught IOException
no Exception

分析:
多catch的时候,只有一个catch输出,谁合适谁输出,这里抛出的是IOException,所以就是第一个catch捕获异常。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值