您的位置:首页 > 其它

基础篇 _练习 小练习5~14

2016-04-30 23:43 387 查看
/*
小练习5 完成class C的代码部分
*/

interface A
{
void show();
}
interface B
{
void add(int a,int b);
}
class C implements A,B
{
//写吧。
}

public class lianxi_5 {

public static void main(String[] args)
{
C c=new C();
c.add(4, 2);
c.show();//通过该函数打印以上两个数的和。
}
}


答案:

/*
小练习5
*/
package com.test2;

interface A
{
void show();
}
interface B
{
void add(int a,int b);
}
class C implements A,B
{
<span style="color:#ff0000;">//int c;
int a,b;
public void add(int a, int b)
{
//c=a+b;
this.a=a;
this.b=b;
}

public void show()
{
System.out.println(a+b);
}</span>

}

public class lianxi_5 {

public static void main(String[] args)
{
C c=new C();
c.add(4, 2);
c.show();//通过该函数打印以上两个数的和。
}
}


/*
<span style="white-space:pre">	</span>小练习6:写出运行结果:
*/
package com.test2;

class Super
{
int i=0;
Super(String s)
{
i=1;
}
}
public class lianxi_8 extends Super{

lianxi_8(String s)
{
super(s);
i=2;
}

public static void main(String[] args)
{
lianxi_8 xi=new lianxi_8("yes");
System.out.println(xi.i);答案:2<pre name="code" class="java">/*
写出运行结果:
*/
/*小练习7:写出运行结果
*/
public class lianxi_9 {

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");
}

}


}}



答案:



/*
<span style="font-family: Arial, Helvetica, sans-serif;">小练习8: </span><span style="font-family: Arial, Helvetica, sans-serif;">补足代码</span>
*/
package com.test3;

interface Test
{
void func();
}

public class lianxi_10
{

public static void main(String[] args)
{
//补足代码,匿名内部类

}

void show (Test t)
{
t.func();
}
}


答案:

/*
<span style="white-space:pre">	</span><span style="font-family: Arial, Helvetica, sans-serif;">小练习8:</span><span style="font-family: Arial, Helvetica, sans-serif;">补足代码:</span>
*/
package com.test3;

interface Test
{
void func();
}

public class lianxi_10
{

public static void main(String[] args)
{
//补足代码,匿名内部类
<span style="color:#ff0000;">lianxi_10 lx=new lianxi_10();
lx.show(new Test()
{
public void func()
{
System.out.println("func run");
}
});</span>

}

void show (Test t)
{
t.func();
}
}


/*
小练习9:写出程序运行结果。
*/
package com.test3;

public class llianxi_11 {

public static String output="";

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);//134
foo(1);
System.out.println(output);//13423
}

}


/*
小练习10:建立一个接口图形,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。
注:体现面向对象的特征,对数值进行判断。用异常处理,不合法的数值要出现“这个数值是非法的”提示,
不再进行运算。
*/
class NoException extends RuntimeException
{
NoException(String msg)
{
super(msg);
}
}

interface A
{
void getArea();
}
class Rect implements A
{
private int width,length;
Rect(int width,int length) throws NoException
{
if(width<=0||length<=0)
throw new NoException("这个数值是非法的");

this.width=width;
this.length=length;
}
public void getArea()
{
System.out.println(width*length);
}

}
class Circle implements A
{
private int radius;
private static final double PI=3.14;
Circle(int radius) throws NoException
{
if(radius<=0)
throw new NoException("这个数值是非法的");
this.radius=radius;
}
public void getArea()
{
System.out.println(radius*radius*PI);
}
}
public class lianxi_12 {

public static void main(String[] args)
{

Rect r = new Rect(3,-4);
r.getArea();

//		Circle c=new Circle(-10);
//		c.getArea();

System.out.println("over");
}

}


/*
小练习11:补足compare函数内的代码,不许添加其他函数。
*/
package com.test4;

class Circle
{
private static double pi=3.14;
private  double radius;
public Circle(double r)
{
this.radius=r;
}

public static double compare(Circle[] cir)
{

//程序代码//其实就是在求数组中的最大值。
<span style="color:#ff0000;">int max=0;//数组角标
for(int i=1;i<cir.length;i++)
{
if(cir[i].radius>cir[max].radius)
{
max=i;
}
}

return cir[max].radius;</span>
}
}

public class lianxi_13 {

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));

}

}


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

class ZiFu
{
//char[] a;
//char b;
//	ZiFu(char[] a,char b)
//	{
//		this.a=a;
//		this.b=b;
//	}
public int getIndex(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;
}
}

public class lianxi_14 {

public static void main(String[] args) {

char[] arr=new char[]{'0','r','2','b','1','2'};

ZiFu z=new ZiFu();
z.getIndex(null, 'a');
}

}


/*
<span style="font-family: Arial, Helvetica, sans-serif;">练习13:</span><span style="font-family: Arial, Helvetica, sans-serif;">补足compare函数内的代码,不许添加其他函数。</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space:pre">		</span>结合</span><span style="font-family: Arial, Helvetica, sans-serif;">小练习11:补足compare函数。一起看,对应的。</span><span style="font-family:Arial, Helvetica, sans-serif;">

</span>*/
package com.test5;

class Circle
{
private double radius;

Circle(double radius)
{
this.radius=radius;
}
public Circle Compare(Circle cir)
{
//程序代码

return (this.radius>cir.radius)?this:cir;
}
}

public class lianxi_13_1 {

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(cir==cir1)
System.out.println("圆1的半径比较大");
else
System.out.println("圆2的半径比较大");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: