面向对象练习题(接口,异常,多态,equals,)

Java学习笔记之----面向对象练习题

练习一

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

package com.rqy.day13;

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

//创建自定义异常类,继承RuntimeException,因为当调用者传入错误的数值时,之间抛出异常给调用者,对代码进行修改
class NotValueException extends RuntimeException{
	NotValueException(){
		super();
	}
	NotValueException(String message){
		super(message);
	}
}
//定义接口
interface Areable{
	double getArea();
}
//矩形类,实现接口
class Res implements Areable{
	//定义矩形的长和宽
	private int length,width;
	//构造函数,初始化
	public Res(int length,int width) {
		//对不合法数值进行判断
		if(length <= 0 ) 
			throw new NotValueException(length+"这个数值是非法的");
		if(width <= 0 ) 
			throw new NotValueException(width+"这个数值是非法的");
		this.length = length;
		this.width = width;
	}
	//矩形面积函数
	public double getArea() {
		return length*width;
	}
}
//圆形类,实现接口
class Circle implements Areable{
	//定义Π和半径
	private static final double PI = 3.14;
	private double radius;
	//构造函数
	public Circle(int radius) {
		//对调用者可能输入的错误数值进行异常的抛出
		if(radius <= 0)
			throw new NotValueException(radius+"这个数值是非法的");
		this.radius = radius;
	}
	//圆形面积函数
	public double getArea() {
		return radius*radius*PI;
	}
}

public class GraphArea {
	public static void main(String[] args) {
		Res r = new Res(0,8);
		Circle c = new Circle(3);
		double num = r.getArea();
		double num1 = c.getArea();
		System.out.println("矩形的面积是"+num);
		System.out.println("圆形的面积是"+num1);
	}
}

在这里插入图片描述
在这里插入图片描述

练习二

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

package com.rqy.day13;

/**
 * 在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,
 * 如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。
 * 要搜索的字符数组和字符都以参数形式传递给该方法, 
 * 如果传入的数组为null,应抛出IllegalArgumentException异常。 
 * 在类的main方法中以各种可能出现的情况测试验证该方法是否编写正确。
*/
class Demo{
	//创建方法,搜索字符串中是否存在某个字符,有返回值,有参数,参数为
	public int search(char[] cha,char key) {
		
		if(cha == null) {
			throw new IllegalArgumentException("数组为空");
		}
		//判断该数组是否存在,若存在,则返回该字符第一次出现的位置,若不存在,返回-1
		for(int x = 0;x < cha.length;x++) { 
			if(key == cha[x]) { 
				return x;
			}
		}
		return -1;		
	}
}

public class SearchChar {
	public static void main(String args[]) {
		char[] cha = {'a','b','v','b','n'};
		//cha = null;
		Demo d = new Demo();
		int x = d.search(cha, 'b');
		System.out.print(x);
	}

}

在这里插入图片描述

练习三

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

class Circle{
	private double radius;
	public Circle(double r) {
		radius=r;
	}
	public static double compare(Circle[] cir) {
		//程序代码
	}
}

public class TC {
	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));
	}

}

class Circle{
	//定义属性
	private double radius;
	//构造函数
	public Circle(double r) {
		radius=r;
	}
	public static double compare(Circle[] cir) {
		//程序代码,求最大半径值
		int max = 0;
		for(int x = 0;x < cir.length;x++ ) {
			if(cir[x].radius>  cir[max].radius) {
				max = x;	
			}
		}
		return cir[max].radius;
	}
}

public class TC {
	public static void main(String args[]) {
		
		//int arr[] = new int[3];
		//创建一个Circle类的数组,长度为3
		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));
	}

}

在这里插入图片描述

练习四

  • 描述Person
  • 属性:姓名和年龄
  • 行为:说出姓名和年龄
  • 判断是否是同一个人(同年龄,同姓名)
/**
 * 描述Person
 * 属性:姓名和年龄
 * 行为:说出姓名和年龄
 * 判断是否是同一个人(同年龄,同姓名)
 */
class Person{
	//定义属性
	private String name;
	private int age;
	//构造函数
	Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	//行为,说出姓名和年龄
	public void speak() {
		System.out.println("name="+this.name+",age="+this.age);
	}
	//判断是否是一个人,使用object中的equals方法,但要按照子类中的特征去判断,所以覆盖
	public boolean equals(Object obj) {
		if(!(obj instanceof Person)) 
			throw new ClassCastException(obj.getClass().getName()+"类型错误");
		Person p = (Person)obj;				
		return this.age ==p.age && this.name.equals(p.name);
	}
}
public class DescriPerson {
	public static void main(String args[]) {
		Person p1 = new Person("张四",21);
		Person p2 = new Person("张三",21);
		p1.speak();
		p2.speak();
		boolean b1 = p1.equals(p2);
		System.out.println(b1);
	}

}

练习五

补足代码

//补足代码

class Circle{
	private double radius;
	public Circle(double r) {
		radius = r;
	}
	//请定义功能,比较两个圆是否一样大
	
}
public class TC1 {
	public static void main(String args[]) {
		Circle cir1 = new Circle(2.0);
		Circle cir2 = new Circle(2.0);
		boolean b = cir1.equals(cir2);
		System.out.print("两个圆是否一样大:"+b);
	}

}
//补足代码

class Circle{
	private double radius;
	public Circle(double r) {
		radius = r;
	}
	//请定义功能,比较两个圆是否一样大
	//根据代码可知,其实就是覆盖equals方法
	public boolean equals(Object obj) {
		//做判断
		if(!(obj instanceof Circle))
			throw new ClassCastException(obj.getClass().getName()+"类型错误");
		Circle c = (Circle)obj;
		return this.radius == c.radius;
	}
	
}
public class TC1 {
	public static void main(String args[]) {
		Circle cir1 = new Circle(2.0);
		Circle cir2 = new Circle(2.0);
		boolean b = cir1.equals(cir2);
		System.out.print("两个圆是否一样大:"+b);
	}

}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值