java期末上机复习题目及其答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3.运行程序,分析并写出程序执行结果
public class less1_3 {
public static void main(String args[]){
boolean x , y , xx , yy;
int i = 10, j =10, ii = 10, jj = 10;
x = true || (++i >10);
y = true &&(++j >10);
xx=true | (++ii >10);
yy=true &(++jj >10);
System.out.println("\n i = " +i+"\t j = "+j);
System.out.println("\n x = " +x+"\t y = "+y);
System.out.println("\n ii = " +ii+"\t jj = "+jj);
System.out.println("\n xx = " +xx+"\t yy = "+yy);
} }
4.根据注释填空,并写出正确执行结果
public class less1_4
{ public static void main(String args[ ])
{ float fa=102.5f;
int ia=3;
String sfa , sia , sc;
double scd;
System.out.println(fa+ia);
sfa=Float.toString(fa); //将fa数值转换为字符串;
sia= Interger.toString(ia); //将ia数值转换为字符串;
sc= sfa+sia; //将字符串sfa和sia连接起来; System.out.println(“sc=”+sc);
scd= Double.parseDouble(sc); //将字符串sc转换为数值; System.out.println(scd);
} }
6.(选做)解释划线语句功能,并写出程序执行结果。
public class less3_1{
public static void main(String arg[]){
float fdata[]={98.5f,85.2f,100.0f},sum=0.0f;
String sdata[]={"","",""}, st=new String();
int i;
for(i=0;i<3;i++)
{ sum+=fdata[i];
sdata[i]=Float.toString(fdata[i]);
st+=sdata[i]+" ";
}
System.out.println(sum);
System.out.println(st);
} }
2下面程序:在圆类的基础上派生出了圆锥类,按照注释填空,并调试执行成功。class Circle
{ double r ;
Circle( ) { r=1.0; } //无参构造方法,默认半径为1.0
Circle(double a) //有参构造方法
{ r=a; }
double Area( ) { return 3.14*r*r; }//返回面积
double Girth( ) { return 2*3.14*r; }//返回周长
}
class Cone extends Circle
{
double h;
Cone(){ super();h=1.0;}
Cone(double rr,double hh)
{ super(rr); h=hh; }
double V() { return 1.0/3*area()*h; }//返回圆锥的体积
}
public class less2_3
{
public static void main(String args[])
{ Cone c1=new Cone();
System.out.println(c1.V());
Cone c2=new Cone(1.0,3.0);
System.out.println(c2.V());
}
}
4.综合题:根据注释填空,调试并写出执行结果。
abstract class Shape{
public abstract double area();
}
class Circle extends Shape{
double r;
Circle(double rr) { r = rr;}
public double area(){return 3.14*r*r; } //实现area()方法
}
class Rectangle extends Shape{
double width,height;
Rectangle(double w,double h)
{ width=w; height=h; }
public double area(){return width*heigth; } //实现area()方法
}
class less2_4{