java----Java 练习

Java 练习
练习
1、写出程序结果:
class Demo
{
	public static void func(){
		try{
			throw new Exception();
		}
		finally{
			System.out.println("B");
		}
	}
	public static void main(String []args){
		try{
			func();
			System.out.println("A");
		}
		catch(Exception e){
			System.out.println("C");
		}
		System.out.println("D");
	}
}
:编译失败,因为func()方法中抛出了异常,但却没有做处理动作,要么抛出去,要么catch内部解决。
================================================================================================================================
2、写出程序结果
class Test
{
	Test(){
		System.out.println("Test");
	}
}
class Demo extends Test
{
	Demo(){
		System.out.println("Demo");
	}
	public static void main(String []args){
		new Demo();
		new Test();
	}
}
:在子类的每个构造函数中默认都有个隐式的super();,调用父类的空参数构造函数。所以答案是:
Test
Demo
Test
================================================================================================================================
3、写出程序结果
interface A{}
class B implements A
{
	public String func(){
		return "func";
	}
}
class Demo
{
	public static void main(String []args){
		A a = new B();
		System.out.println(a.func());
	}
}
:编译失败,因为a.func();,a是父类的引用,父类中没有定义func()方法。所以编译不通过,提示找不到符号。

================================================================================================================================

4、写出程序结果
class Fu
{
	boolean show(char a){
		System.out.println(a);
		return true;
	}
}
class Demo extends Fu
{
	public static void main(String []args){
		int i = 0;
		Fu f = new Demo();
		Demo d = new Demo();
		for(f.show('A');f.show('B')&&(i<0);f.show('C')){
			i++;
			d.show('D');
		}
	}
	boolean show(char a){
		System.out.println(a);
		return false;
	}
}
:Demo继承Fu类,show()方法被子类复写了,所以f.show();调用的是子类的show方法,也就返回false。所以for循环也就不会再循环下去了。那么就只打印A,B。
================================================================================================================================
5、写出程序的结果
interface A{}
class B implements A
{
	public String test(){
		return "yes";
	}
}
class Demo{
	static A get(){
		return new B;
	}
	public static void main(){
		A a = get();
		System.out.println(a.test());
	}
}
:编译失败,父类中没有定义test()方法。

================================================================================================================================
6、写出程序结果
class Super
{
	int i = 0;
	public Super(String a){
		System.out.println("A");
		i = 1;
	}
	public Super(){
		System.out.println("B");
		i+=2;
	}
}
class Demo extends Super
{
	public Demo(String a){
		System.out.println("C");
		i = 5;
	}
	public static void main(String []args){
		int i = 4;
		Super d = new Demo("A");
		System.out.println(d.i);
	}
}
Super d = new Demo("A");new的是子类对象,调用子类构造方法,因为子类的构造方法中第一句都有个默认的super();,所以会先运行父类的空参数super()。先打印B,然后打印C,接着把5赋值给i。然后打印5。

================================================================================================================================
7、补足代码
interface Inter
{
	void show(int a,int b);
	void func();
}
class Demo
{
	public static void main(String []args){
		//补足代码,调用两个函数,要求用匿名内部类
	}
}
interface Inter
{
	void show(int a,int b);
	void func();
}
class Demo
{
	public static void main(String []args){
		//补足代码,调用两个函数,要求用匿名内部类
		Inter in = new Inter(){
			public void show(int a,int b){
				System.out.println("show");
			}
			public void func(){
				System.out.println("func");
			}
		};
		in.show(1,2);
		in.func();
	}
}
运行

================================================================================================================================
8、写出程序结果
class TD
{
	int y = 6;
	class Inner
	{
		static int y = 3;
		void show(){
			System.out.println(y);
		}
	}
}
class Demo
{
	public static void main(){
		TD.Inner ti = new TD().new Inner();
		ti.show();
	}
}
:编译失败,当内部类中有静态成员时,内部类也必须是静态的。

================================================================================================================================
9、选择题,写出错误答案错误的原因,用单行注释的方式。
class Demo
{
	int show(int a,int b){
		return 0;
	}
}
下面哪些函数可以存在于Demo的子类中
A.public int show(int a,int b){ return 0; }//可以,覆盖了。
B.private int show(int a,int b){ return 0; }//不可以,权限小于了父类的。
C.private int show(int a,long b){ return 0; }//可以,相当于重载。
D.public short show(int a,int b){ return 0; }//不可以,虚拟机不知道该返回什么类型的参数。
E.static int show(int a,int b){ return 0; }//不可以,静态只能覆盖静态。
================================================================================================================================
10、写出this关键字的含义,final有哪些特点。
this:代表本类对象,哪个对象调用this所在函数,this就代表哪个对象。
final
1、修饰类,变量(成员变量,静态变量,局部变量),函数。
2、修饰的类不可以被继承。
3、修饰的函数不可以被覆盖。
4、修饰的变量是个常量,只能赋值一次。
5、内部类只能访问局部类中final修饰的变量。
================================================================================================================================
11、写出程序结果
class Fu
{
	int num = 4;
	void show(){
		System.out.println("show Fu");
	}
}
class Zi extends Fu
{
	int num = 5;
	void show(){
		System.out.println("show Zi");
	}
}
class Demo
{
	public static void main(String []args){
		Fu f = new Zi();
		Zi z = new Zi();
		System.out.println(f.num);
		System.out.println(z.num);
		f.show();
		z.show();
	}
}
:继承关系中,不存在变量覆盖。f.num调用的是父类的num。z.num调用的是子类的num。因为子类覆盖了父类的show()方法,所以打印的都是子类覆盖后的show()方法。

================================================================================================================================
12、补充程序代码
interface A
{
	void show();
}
interface B
{
	void add(int a,int b);
}
class C implements A,B
{
	//程序代码
}
class Demo
{
	public static void main(String []args){
		C c = new C();
		c.add(4,2);
		c.show();//通过该函数打印a,b两数的和。
	}
}
interface A
{
	void show();
}
interface B
{
	void add(int a,int b);
}
class C implements A,B
{
	//*****************************
	int sum;
	public void add(int a,int b){
		sum = a+b;
	}
	public void show(){
		System.out.println(sum);
	}
	//*****************************
}
class Demo
{
	public static void main(String []args){
		C c = new C();
		c.add(4,2);
		c.show();
	}
}
================================================================================================================================
13、写出程序结果
class Demo
{
	public static void main(String []args){
		try{
			showExce();
			System.out.println("A");
		}
		catch(Exception e){
			System.out.println("B");
		}
		finally{
			System.out.println("C");
		}
		System.out.println("D");
	}
	public static void showExce() throws Exception{
		throw new Exception();
	}
}
:程序执行到showExce();时,抛出异常不再往下执行,然后直接被catch捕捉所以打印了B,接着finally执行,打印C,因为问题得到了解决,所以打印D。

================================================================================================================================
14、写出程序结果
class Super
{
	int i = 0;
	public Super(String s){
		i = 1;
	}
}
class Demo extends Super
{
	public Demo(String s){
		i = 2;
	}
	public static void main(String []args){
		Demo d = new Demo("yes");
		System.out.println(d.i);
	}
}
:编译失败,子类的构造函数中,默认第一行都有一个super();,所以在创建子类对象时会访问父类的空参数的构造函数,然而父类中没有空参数的构造函数,所以会报异常。

================================================================================================================================
15、写出程序结果
class Super
{
	public int get(){
		return 4;
	}
}
class Demo extends Super
{
	public long get(){
		return 5;
	}
	public static void main(String []args){
		Super s = new Demo();
		System.out.println(s.get());
	}
}
:编译失败,因为子父类中的get方法没有覆盖,子类在调用get();方法的时候,虚拟机不明确该返回什么类型的参数。所以这类情况不能出现在程序中。
================================================================================================================================
16、写出程序结果
class Demo
{
	public static void func(){
		try{
			throw new Exception();
			System.out.println("A");
		}
		catch(Exception e){
			System.out.println("B");
		}
	}
	public static void main(String []args){
		try{
			func();
		}
		catch(Exception e){
			System.out.println("C");
		}
		System.out.println("D");
	}
}
:编译失败, throw下面不能有其他语句,因为执行不到。同样的还有 returnbreak

================================================================================================================================
17、选择题
class Demo
{
	public void func(){
		//位置1
	}
	class Inner{}
	public static void main(String []args){
		//位置2
	}
}
A.在位置1写new Inner();。可以。
B.在位置2写new Inner();。不可以,因为Inner不是静态的。
C.在位置2写new d.Inner();。不可以,因为new new Demo.Inner();这种方式不存在。
D.在位置2写new Demo.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要放在最下面。

19、补足代码
interface Test
{
	void func();
}
class Demo
{
	public static void main(String []args){
		//补足代码(要求使用匿名内部类)
	}
	void show (Test t){
		t.func();
	}
}
:代码如下
new Demo().show(new Test(){
			public void func(){
				System.out.println("Test 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);
	}
}
:当将0传入foo()方法时,try中output里面有了1,catch不运行,finally中ouput变为了13,接着又变为了134。然后打印134。继续传值1,这时候抛了异常,try不再执行。catch中output变为了1342,因为output是静态变量。然后return,finally最后执行。foo()方法结束了。所以打印13423。
================================================================================================================================
21、建立一个图形接口,声明一个面积函数,圆形和矩形都实现这个接口,并得出两个图形的面积。
:体现面向对象特征,对数据进行判断,用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。
class NoValueException extends RuntimeException
{
	NoValueException(){
		super("这个数值是非法的");
	}
}
interface Graph
{
	public abstract double getArea();
}
//圆
class Circle implements Graph
{
	//全局静态常量PI
	public static final double PI = 3.14;
	//半径
	private double round;
	Circle(double round){
		//如果半径小于0,就抛异常,由于该异常是继承RuntimeException,所以可以不用声明或者try
		if(round<0)
			throw new NoValueException();
		this.round = round;
	}
	public double getArea(){
		return round*round*PI;
	}
}
//矩形
class Rec implements Graph
{
	private double high;
	private double wide;
	Rec(double high,double wide){
		//如果长和宽其中一个小于0,就抛异常,由于该异常是继承RuntimeException,所以可以不用声明或者try
		if(high<0||wide<0)
			throw new NoValueException();
		this.high = high;
		this.wide = wide;
	}
	public double getArea(){
		return high*wide;
	}
}
class Demo
{
	public static void main(String []args){
		Circle c = new Circle(3);
		print(c);
		Rec r = new Rec(9,-3);
		print(r);
	}
	//打印对象的面积
	public static void print(Graph g){
		System.out.println(g.getArea());
	}
}
================================================================================================================================
22、补充compare函数的代码,不许添加其他语句。
class Circle
{
	private static 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));
	}
}
int max = 0;
for(int x=1;x<cir.length;x++){
	if(cir[max].radius<cir[x].radius){
		max = x;
	}
}
return cir[max].radius;
================================================================================================================================
23、写出程序结果
public class Demo
{
	private static int i = 0;
	private static boolean methodB(int k){
		i += k;
		return true;
	}
	private 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(i);
	}
}
|||的区别是,前者是两边都判断,后者是如果左边为false才判断右边,否则只判断左边。所以答案为4。
================================================================================================================================
24、假如我们在开发一个系统时,需要对员工进行建模,员工包含3个属性:姓名,工号以及工资。经理也是员工,除了含有员工的属性外,另外还有一个奖金属性。请使用继承的思想设计出员工和经理类,要求类中提供必要的方法进行属性访问。
//员工抽象类
abstract class Employee
{
	private String name;
	private String id;
	private double pay;
	Employee(String name,String id,double pay){
		this.name = name;
		this.id = id;
		this.pay = pay;
	}
	public void show(){
		System.out.println(this.getInfo());
	}
	public String getInfo(){
		return "name = "+name+","+"id = "+id+","+"pay = "+pay;
	}
}
//经理类
class Manager extends Employee
{
	private int bouns;
	Manager(String name,String id,double pay,int bouns){
		super(name,id,pay);
		this.bouns = bouns;
	}
	public void show(){
		System.out.println(this.getInfo()+"bouns = "+bouns);
	}
}
//普通员工类
class Pro extends Employee
{
	Pro(String name,String id,double pay){
		super(name,id,pay);
	}
}
class Demo
{
	public static void main(String []args){
		Employee m = new Manager("lisi","x-001",5000,200);
		m.show();
		Employee p = new Pro("wangwu","x-012",2000);
		p.show();
	}
}
================================================================================================================================
25、在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符数组中第一次出现的位置(序号从0开始计算)。传入的数组为null,应该抛出 IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写是否正确,例如字符不存在,字符存在,传入的数组为null等。
class Demo
{
	public static void main(String []args){
		char[] arr = {'a','c','e','d'};
		System.out.println(find(arr,'z'));
	}
	//***************************************************************
	//方法体
	public static int find(char[] arr,char key){
		if(arr==null)
			throw new IllegalArgumentException("出现空参数");
		for(int i=0;i<arr.length;i++){
			if(arr[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的半径比较大。");
	}
}

//**************************************
//添加的代码
return this.radius>cir.radius?this:cir;
//**************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值