1. 程序填空题:请写出该函数实现功能.
public class Sum
{ public static void main( String args[ ])
{ double sum = 0.0 ;
for ( int i = 1 ; i <= 100 ; i + + )
sum += 1.0/(double) i ;
System.out.println( "sum="+sum );
}
}
程序功能是:( )
答案
计算 1/1+1/2+1/3+…+1/100 的值
2. 读程序,写出下列程序的输出结果。
import java.io.*;
public class ATest{
public static void main(String args[]) {
Sub sb = new Sub( );
System.out.println(sb.method1( )); }
}
class Super{
int x=1 , y=2 ;
int method1(){ return xy)?x:y); }
}
程序运行结果是:( )
答案
1
3. 编写一个程序,求1!+2!+…+10!的值。
答案
public class test {
public static void main(String[] args) {
double s = 0, fact = 1;
int i;
for (i = 1; i <= 10; i++) {
fact = fact * i;
s += fact;
}
System.out.println("s=" + s);
}
4. 一网页中嵌入一个Applet,Applet在网页中长为300,宽为400,Applet上有两个Jlabel对象lblName及lblWelcome,一个JTextField对象txtName和一个Jbutton对象bttnWelcome,lblName显示“请输入你的姓名”,txtName为客户输入姓名,例如:Zhang san,当点击bttnWelcome,lblWelcome将要显示“Welcome Zhang san !”。
答案
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Welcom extends JApplet
{
JPanel panel;
JLabel lblName;
JLabel lblWelcome;
JTextField txtName;
JButton bttnWelcome;
GridLayout gl;
public void init()
{
panel=new JPanel();
panel=(JPanel)getContentPane();
gl=new GridLayout(2,2);
panel.setLayout(gl);
lblName=new JLabel("请输入你的姓名");
txtName=new JTextField(10);
bttnWelcome=new JButton("Welcome");
lblWelcome=new JLabel();
panel.add(lblName);
panel.add(txtName);
panel.add(bttnWelcome);
panel.add(lblWelcome);
WelcomeAction welcome = new WelcomeAction();
bttnWelcome.addActionListener(welcome);
}
class WelcomeAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if(obj == bttnWelcome)
{
String str = txtName.getText();
lblWelcome.setText("Welcome" + str);
}
}
}
}
5. 以下程序的功能是求3×3矩阵对角元素之和,补充完成程序。
public class test
{
public static void main(String[] args)
{
int i, j, sum = 0;
int a[][] = ~~(1)~~ ;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
a[i][j] = i+j;
}
}
//输出3×3矩阵
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
System.out.print(a[i][j] + " ");
}
~~(2)~~ ;
}
//显示对角线之和
for (i = 0; i < 3; i++)
{
sum =~~(3)~~ ;
}
System.out.println("对角线之和:" + sum);
}
}
答案
代码1: new int[3][3]
代码2: System.out.println()
代码3: a[i][i]+a[i][3-i-1]
6. 写出下列程序在控制台窗口中的输出结果。
public class J_Hanoi
{
public static void mb_hanoi(int n, char start, char temp, char end)
{
if (n<=1)
System.out.println("将盘从" + start + "移到" + end);
else
{
mb_hanoi(n-1, start, end, temp);
System.out.println("将盘从" + start + "移到" + end);
mb_hanoi(n-1, temp, start, end);
} // if-else结构结束
} // 方法mb_hanoi结束
public static void main(String args[ ])
{
mb_hanoi(3, 'S', 'T', 'E');
}
}
程序运行结果是:( )
答案
将盘从S移到E
将盘从S移到T
将盘从E移到T
将盘从S移到E
将盘从T移到S
将盘从T移到E
将盘从S移到E
7. 从键盘输入数据,保存到f:/student.txt上并在屏幕上显示。
答案
import java.lang.*;
import java.io.*;
public class readWrite
{
public static void main(String ar[]) throws IOException
{
InputStreamReader inReader=new InputStreamReader(System.in);
BufferedReader bStream=new BufferedReader(inReader);
FileOutputStream fWrite=new FileOutputStream("lx.txt",true);
String rString;
do
{
System.out.println("\n Please enter :");
System.out.flush();
rString=bStream.readLine();
System.out.println("Hello User>This is:");
fWrite.write(rString.getBytes());
System.out.println("> "+rString);
}while(rString.length()!=0);
}
}
8. 编程计算三角形的面积和周长。要求如下:
(1)、设计一个接口InterfaceShape,其中有计算三角形的面积和周长的抽象方法;
(2)、设计一个点类Point,其中有必要的构造方法和一般的访问器方法(getXXX())和修改器方法(setXXX());
(3)、通过实现InterfaceShape接口类的方式,并使用Point类,设计三角形Triangle类,其中有必要的构造方法和计算三角形的面积和周长的方法;
(4)、设计一个主类Shape,对使用Point类,设计三角形Triangle类进行测试,计算三角形的面积和周长,并在屏幕上输出。
答案
InterfaceShape 接口:
public interface InterfaceShape
{
public abstract double getArea();
public abstract double getFerence();
}
Point 类:
public class Point
{
double x;
double y;
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public void setX(double x)
{
this.x = x;
}
public double getY()
{
return y;
}
public void setY(double y)
{
this.y = y;
}
}
Triangle 类
public class Triangle implements InterfaceShape
{
Point a;
Point b;
Point c;
public double s1;
public double s2;
public double s3;
public Triangle(Point A, Point B, Point C)
{
this.a = A;
this.b = B;
this.c = C;
}
//三角形面积
public double getArea()
{
double p=(this.getS1()+this.getS2()+this.getS3())/2;
return Math.pow(p*(p-this.getS1())*(p-this.getS2())*(p-this.getS3()),0.5);
}
//三角形周长
public double getFerence()
{
return this.getS1()+this.getS2()+this.getS3();
}
public double getS1()
{
return Math.sqrt(Math.pow(a.getX()-b.getX(),2)+Math.pow(a.getY()-b.getY(), 2));
}
public double getS2()
{
return Math.sqrt(Math.pow(a.getX()-c.getX(),2)+Math.pow(a.getY()-c.getY(), 2));
}
public double getS3()
{
return Math.sqrt(Math.pow(b.getX()-c.getX(),2)+Math.pow(b.getY()-c.getY(), 2));
}
}
Shape 测试类:
public class Shape
{
public static void main(String[] args)
{
Point a=new Point(0,0);
Point b=new Point(3,0);
Point c=new Point(0,4);
Triangle tri = new Triangle(a,b,c);
System.out.println("三角形面积:"+tri.getArea());
System.out.println("三角形周长:"+tri.getFerence());
}
9. 下面程序是定义一个长度为10的整形数组,输出元素的内容,再对数组元素进行赋值。在下面空白处填入适当的语句。
public class LinearA{
public static void main(String args[])
{
int i;
int[]a ;//声明一个整型数组a
~~(1)~~ ; //分配内存空间给数组a,元素个数为10个
for( ~~(2)~~ )//i从0到a.length-1,每次递增1
{
~~(3)~~ ;//将i赋值给a[i]
System.out.println(a[i]+“ ”);
}
}
}
答案
(1)a=new int[10]
(2)i=0; i
10. 以下程序段的输出结果为 ( ) 。
public class TestArray
{
public static void main(String args[ ]){
int i , j ;
int a[ ] = { 5,9,6,8,7};
for ( i = 0 ; i < a.length-1; i ++ ) {
int k = i;
for ( j = i ; j < a.length ; j++ )
if ( a[j]
答案
5 6 7 8 9
11. 编写一个程序,求1!+2!+…+10!的值。
答案
public class test {
public static void main(String[] args) {
double s=0,fact=1;
int i;
for(i=1;i<=10;i++)
{
fact=fact*i;
s+=fact;
}
System.out.println("s="+s);
}
}
12. 编写一个完整的Java Application 程序。包含接口ShapeArea,MyRectangle类及Test类,具体要求如下:
1.接口ShapeArea: double getArea():求一个形状的面积 double getPerimeter ():求一个形状的周长
2.类 MyRectangle: 实现ShapeArea接口,另有以下属性和方法: (1)属性 width: double类型,表示矩形的长 height: double类型,表示矩形的高 (2)方法 MyRectangle(double w,
double h):构造函数 toString()方法 :输出矩形的描述信息,如“width=1.0,height=2.0,
perimeter=6.0, area=2.0”3.Test类作为主类要完成测试功能 (1)生成MyRectangle对象 (2)调用对象的toString方法,输出对象的描述信息
答案解析 答案
public class TestShape //主类定义
{public static void main(String args[])
{MyRectangle r=new MyRectangle(1.0,2.0);
System.out.println(r.toString());
}
}
interface ShapeArea //接口定义
{
public abstract double getPerimeter();
public abstract double getArea();
}
class MyRectangle implements ShapeArea{
double width,height;
MyRectangle(double w,double h){width=w;height=h; } //构造方法
public double getPerimeter(){return 2*(width+height); } //求周长方法
public double getArea(){return width*height; } // 求面积方法
public String toString() // toString()方法
{
return "width="+width+",height="+height+",perimeter="
+getPerimeter()+",area="+getArea();
}