黑马程序员------毕老师视频笔记第十天------面向对象(面向对象练习题下)

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

-----------------------------------------------------------------------------------------------------------------------

17.选择题

 

class Demo
{
	public void func()
<span style="white-space:pre">	</span>{
	//位置1
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>class Inner{}
<span style="white-space:pre">	</span>public static void main (String [] args)
<span style="white-space:pre">	</span>{
	Demo d = new Demo();
	//位置2
<span style="white-space:pre">	</span>}
}

A.在位置1写new Inner();              //可以

B.在位置2写new Inner();              //不可以,主函数是静态的,如果要访问,Inner需静态

C.在位置2写newd.Inner();           //不可以,格式错误,newnew Demo().Inner

D.在位置2写newDemo.Inner(); //不对,Inner不是静态的

-----------------------------------------------------------------------------------------------------------------------

18.写出程序结果

class Exc0 extends Exception{}
class Exc1 extends Exc0{}

class Demo 
{
	public static void main (String [] args)
	{
		try
		{
			throw new Exc1();
		}
		catch (Exception e)
		{
			System.out.println("Exception");
		}
		catch (Exc0 e)
		{
			System.out.println("Exc0");
		}
	}
}

结果:编译失败,多个catch的时候,父类的catch要放到后面

-----------------------------------------------------------------------------------------------------------------------

19.补足代码

interface Test
{
	void func();
}
class Demo
{
	public static void main (String [] args)
	{
		//补足代码(匿名内部类)
	}
	void show(Test t)
	{
		t.func();
	}
}

结果:

interface Test
{
	void func();
}
class Demo
{
	public static void main (String [] args)
	{
		//补足代码(匿名内部类)
		new Demo().show(new Test(){public void func(){}});
	}
	void show(Test t)
	{
		t.func();
	}
}

注意show方法不是静态的,要建立一个对象调用

-----------------------------------------------------------------------------------------------------------------------

20.写出程序结果

class Test
{
	public static String output = "";
	public static void foo(int i)
	{
		try
		{
			if (i == 1)
				throw new Exception();
			output += "1";
		}
		catch (Exception e)
		{
			output += "2";
			return;
		}
		finally
		{
			output += "3";
		}
		output += "4";
	}
	public static void main (String [] args)
	{
		foo(0);
		System.out.println(output);
		foo(1);
		System.out.println(output);
	}
}

结果:

134

13423

-----------------------------------------------------------------------------------------------------------------------

21.建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两

个图形的面积。注:体现面向对象的特征,对数值进行判断,用异常处理,不合法的

数值要出现“这个数值是非法的”提示,并结束程序。

interface MyShape
{
	void getArea();
}
class NoValueException extends RuntimeException
{
	NoValueException(String msg)
	{
		super(msg);
	}
}
class Rec implements MyShape
{
	private double len,wid;
	Rec(double len, double wid)
	{
		if (len <=0 || wid <=0)
			throw new NoValueException("出现非法值");
		this.len = len;
		this.wid = wid;
	}
	public void getArea()
	{
		System.out.println("rectangle的面积是:"+len*wid);
	}
}
class Circle implements MyShape
{
	private double radius;
	public static final double PI = 3.14;
	Circle(double radius)
	{
		if (radius <= 0)
			throw new NoValueException("出现非法值");
		this.radius = radius;
	}
	public void getArea()
	{
		System.out.println("circle的面积是:"+PI*radius*radius);
	}
}
class Demo
{
	public static void main (String [] args)
	{
			Rec r = new Rec(1,3);
			Circle c = new Circle(-5);
			r.getArea();
			c.getArea();
	}
}

-----------------------------------------------------------------------------------------------------------------------

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

class Circle
{
	private static final double PI = 3.14;
	private double radius;
	public Circle(double r)
	{
		radius = r;
	}
	public static double compare(Circle[] cir)
	{
		//程序代码//其实就是求数组中的最大值
	}
}
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 static final double PI = 3.14;
	private double radius;
	public Circle(double r)
	{
		radius = r;
	}
	public static double compare(Circle[] cir)
	{
		//程序代码//其实就是求数组中的最大值
		double max = 0;
		for (int i=0; i<cir.length; i++)
		{
			if (cir[i].radius > max)
			{
				max = cir[i].radius;
			}
		}
		return max;
	}
}
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));
	}
}

-----------------------------------------------------------------------------------------------------------------------

23.写出程序结果

public class Demo
{
	private static int j = 0;
	private static boolean methodB(int k)
	{
		j += k;
		return true;
	}
	public static void methodA(int i)
	{
		boolean b;
		b = i < 10 | methodB(4);
		b = i < 10 || methodB(8);
	}
	public static void main (String [] args)
	{
		methodA(0);
		System.out.println(j);
	}
}

结果:4

-----------------------------------------------------------------------------------------------------------------------

24.加入我们在开发一个系统时需要对员工进行建模,员工包含3个属性:姓名、工

号以及工资。经理也是员工,除了含有员工的属性外,另外还有一个奖金属性。请使

用继承的思想设计出员工类和经理类。要求类中提供必要的方法进行属性访问。

abstract class Employee
{
	private String name;
	private String id;
	private double wage;

	Employee(String name, String id, double wage)
	{
		this.name = name;
		this.id = id;
		this.wage = wage;
	}

	public String getName()
	{
		return name;
	}
	public String getID()
	{
		return id;
	}
	public double getWage()
	{
		return wage;
	}
}
class Staff extends Employee
{
	Staff(String name, String id, double wage)
	{
		super(name, id, wage);
	}
}
class Manager extends Employee
{
	private double bonus;
	public double getBonus()
	{
		return bonus;
	}
	Manager(String name, String id, double wage, double bonus)
	{
		super(name, id, wage);
		this.bonus = bonus;
	}
}
class Demo
{
	public static void main (String [] args)
	{
		Staff s = new Staff("张三","0000011",10000);
		Manager m = new Manager("李四","1000003",20000,10000);

		System.out.println("******* 员工对象 *********");
		System.out.println("姓名:"+s.getName());
		System.out.println("工号:"+s.getID());
		System.out.println("工资:"+s.getWage());
		System.out.println("******* 经理对象 *********");
		System.out.println("姓名:"+m.getName());
		System.out.println("工号:"+m.getID());
		System.out.println("工资:"+m.getWage());
		System.out.println("奖金:"+m.getBonus());
	}
}


-----------------------------------------------------------------------------------------------------------------------

25.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,

则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1.要搜

索的字符数组和字符都以参数形式传递给该方法,如果传入的数组为null,应该抛出

IllegalArgumentException异常。在类的main方法中以各种可能出现的情况验证该方法编写

的是否正确,例如,字符不存在,字符存在,传入的数组为null等。

class Demo
{
	public static void main (String [] args)
	{
		char[] chs = {'a','b','c','d','e','f','g','h'};

		System.out.println("d:"+searchChar(chs,'d'));
		System.out.println("k:"+searchChar(chs,'k'));
		System.out.println("null:"+searchChar(null,'k'));
	}

	public static int searchChar(char[] chs, char key)throws IllegalArgumentException
	{
		if (chs == null)
			throw new IllegalArgumentException();
		for (int i=0; i<chs.length; i++)
		{
			if (chs[i] == key)
				return i;
		}
		return -1;
	}
}

-----------------------------------------------------------------------------------------------------------------------

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

class Circle
{
	private double radius;
	public Circle(double r)
	{
		radius = r;
	}
	public Circle compare(Circle cir)
	{
		//补足代码
	}
}
class TC
{
	public static void main (String [] args)
	{
		Circle cir1 = new Circle(1.0);
		Circle cir2 = new Circle(2.0);
		Circle cir;
		cir = cir1.compare(cir2);
		if (cir1 == cir)
			System.out.println("圆1的半径比较大");
		else
			System.out.println("圆2的半径比较大");
	}
}

补后代码

class Circle
{
	private double radius;
	public Circle(double r)
	{
		radius = r;
	}
	public Circle compare(Circle cir)
	{
		//补足代码
		return this.radius>cir.radius?this:cir;
	}
}
class TC
{
	public static void main (String [] args)
	{
		Circle cir1 = new Circle(1.0);
		Circle cir2 = new Circle(2.0);
		Circle cir;
		cir = cir1.compare(cir2);
		if (cir1 == cir)
			System.out.println("圆1的半径比较大");
		else
			System.out.println("圆2的半径比较大");
	}
}

-----------------------------------------------------------------------------------------------------------------------


---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值