资源描述
电大Java语言与WWW技术形成性考评程序题
一、写出以下程序完成功效
1、public class Sum
{
public static void main(String args[])
{
int sum =0;
for ( int i=1;i<=100;i++)
sum+=i;
System.out.println("sum="+sum);
}
}
上面这段程序所完成功效是:求 1至100之间整数和,并在屏幕上显示出来。
2、
import java.io.*;
public class Class1
{
public static void main(String args[]){
int i,Max,Min;
int a[ ]={ 12,67,8,98,23,56,124,55,99,100 };
Max=Min=a[0];
for (i=1;i<a.length;i++){
if(a[i]<min)Min=a [i];
if(a[i]>Max)Max=a [i];
}
System.out.println(Max+" "Min);
System.out.println();
}
}
上面这段程序所完成功效是:在数组中查找并输出最大值和最小值,并输出显示。
3、import java.io.*;
public class Class1{
public static void main(String args[] )
{
Fact N=new Fact(4);
System.out.println(N.fact() );
}
}
class Fact{
int n;
Fact(int nn){n=nn;}
int fact(){
int i,f=1;
for(i=1;i<=1;i++ )
f=f*i;
return f ;
}
}
上面这段程序所完成功效是:定义一个计算n!(n阶阶乘)类Fact,然后主类中创建一个对象求解4!值。
4、import java.io.*;
public class abc
{
public static void main(String args[])
{
SubClass sb=new SubClass();
System.out.println(sb.max());
}
}
class SuperClass
{
int a=10,b=20;
}
class Subclass extends SuperClass
{
int max(){return((a>b)?a:b);}
}
上面这段程序所完成功效是:求两个数最大值。
5、
import java.awt.*;
import java.applet.Applet;
public class Applet1 extends Applet
{
public void paint(Graphics g )
{
g.drawLine(30, 5,100, 45 );
g.drawRect(30,50,50,20);
g.drawOval(30,80,50,40);
g.drawString("They are figures!",30,150);
}
}
上面这段程序所完成功效是:__在Applet界面中显示一些由直线、矩形框、椭圆框和文字组成图形。 。
6. import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class APPletl extends Applet implements ActionListener
{
Label prompt;
Button btn;
public void init()
{
prompt=new Label(" " );
btn=new Button ("开始");
add (btn);
add(prompt);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
prompt.setText("祝您考试及格");
}
}
上面这段程序所完成功效是:在一个Applet程序中创建一个标识为“开始”按钮,当鼠标单击“开始”按钮 时,就显示出标识有“祝您考试及格”字样标签。
7、import java.awt.*;
public class abc
{
public static void main(String args[])
{
new FrameOut();
}
}
class FrameOut extends Frame//Frame为系统定义窗框类
{
Button btn;
FrameOut()
{
super("我标题");
btn=new Button ("我按钮");
setLayout(new FlowLayout());
add(btn);
setSize(300,200);
show();
}
}
上面这段程序所完成功效是:创建一个标题为“我标题”窗框,窗框中显示有“我按钮”字样按钮。
二、写出以下程序运行得结果
1、
int x=0,y=4,z=5;
if(x>2){
if(y<5){
System.out.println("Message one");
}
else{
System.out.println("Message two");
}
}
else if(z>5){
System.out.println("Message three");
}
else{
System.out.println("Messaqe four");
}
运行结果:Message Four
2、import java.io.* ;
public class abc
{
public static void main(String args [])
{
int i,s=0;
int a[]={ 10,20,30,40,50,60,70,80,90 };
for(i=0; i< a.length;i++)
if(a [i]%3==0)s+=a[i];
System.out.println("s="+s);
}
}
运行结果:S=180
3、import java.io.*;
public class abc
{
public static void main(String args[])
{
SubSubClass x=new SubSubClass(10,20,30);
x.show();
}
}
SuperClass(int aa, int bb)
{a=aa;b=bb;}
void show()
{ System.out.println("a="+a+"\nb="+b);}
}
class SubClass extends SuperClass
{ int c;
SubClass(int aa,int bb,int cc)
{super(aa,bb);
c=cc;
}
}
class SubSubClass extends SubClass
{int a;
SubSubClass(int aa,int bb,int cc)
{ super(aa,bb,cc);
a=aa+bb+cc;
}
void show()
{System.out.println("a="+a+"\nb="+b+"\nc="+c);}
}
运行结果:a=60
b=20
C=30
4、class A
{ public int f(int x)
{ return x+1;
}
}
class B extends A
{ public int f(int x)
{ return x*x;
}
}
public class E
{ public static void main(String args[ ])
{ A a= new B();
int m=a.f(10);
System.out.println(m);
}
}
运行结果:100
5、class A
{ int x;
public void setx(int x)
{ this.x=x;
}
int getx()
{ return x;}
}
class B
{ public void f( A a )
{ a.setx(100);
}
}
public class E
{ public static void main(String args[ ])
{ A a= new A();
a.setx(8);
System.out.println(a.getx());
B b=new B();
B.f(a);
System.out.println(a.getx());
}
}
运行结果: 8 100
三、程序设计题
1、编写一个 Java Applet,使之能够在浏览器中显示 “Welcome to Java Applet World!”字符串信息。
参考程序以下:
import java.awt.Graphics;
import java.applet.*;
public class HelloApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("Welcome to Java Applet World!",50,50);
}
}
2、编写一个Java Application,显示“Welcome to Java Application World!”字符串信息。
参考程序以下:
public class HelloWorld//定义类HelloWorld
{
public static void main(String args [])//程序起始点
{
//控制台输出 Welcome to Java Application World!
System.out.println("Welcome to Java Application World!");
}
}
3、编程序计算1/1+1/2+1/3+……+1/100值。
参考程序以下:
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);
}
}
4编写一个日期类Date,要求:
(1)日期类Date 属性有:
year: int 型 ,代表年。
month: int 型,代表月。
(2)日期类 Date方法有:
Date() :结构函数,日期默认初值为5月1日。
Date(int y, int m, int d):结构函数、形参y,m,d分别为某个日期年月日初值。
int GetYear ( ):获取日期年份作为方法返回值。
int GetMonth ( ):获取日期月份作为方法返回值。
int GetDay ( ):获取日期日作为方法返回值 。
void Show Date ( ):以****年*****月*****日形式显示一个日期。
参考程序以下:
public class Date
{
int year,month,day;
Date(){year=;month=1;day=1;}
Date(int y,int m,int d){year=y;month=m;day=d;}
int GetYear( ){return year;}
int GetMonth( ){return month;}
int GetDay( ){return day;}
void ShowDate( )
{
System.out.println(year+"年"+month+"月"+day+"日");
}
}
5、定义抽象生来表示“学生”,它派生出“小学生”、“中学生”、“大学生”、“硕士” 四个大类,另外其中“大学生”类再派生出“一年级学生”、“二年级学生”、“三年级学生”、“四年级学生”四个子类,“硕士”类在派生出“硕士生”和“博士生”两个子类。
参考程序以下:
abstract class student //抽象类
{
//学生属性,如学号、姓名、性别、年纪
int studentNo;
String studentName;
boolean studentSex;
int studentAge;
student(int sno,boolean sex,int age)
{
studentNo=sno;
studentSex=sex;
studentAge=age;
}
void modifyage(int sno,int newage)
{
if(studentNo==sno)
studentAge=newage;
}
}
//小学生类
class pupil extends student
{
String studentkind;
}
//中学生类
class middleStudent extends student
{
String studentkind;
}
//大学生类
class academician extends student
{
String studentkind;
}
//硕士类
class graduatestudent extends student
{
String studentkind;
}
//大学一年级类
class freshman extends academician
{
String specialty;
}
//大学二年级类
class sophomore extends academician
{
String specialty;
}
//大学三年级类
class junior extends academician
{
String specialty;
}
//大学四年级类
class senior extends academician
{
String specialty;
}
//硕士硕士类
class master extends graduatestudent
{
String specialty;
}
//博士硕士类
class doctor extends graduatestudent
{
String specialty;
}
6、编写图形界面下 Java Applet程序,接收用户输人两个数据为上、下限,然后10个一行输出上、下限之间全部素数。
参考程序以下:
lb2=new Label("下限");
in1=new TextField(5);
in2=new TextField(5);
out1=new TextArea();
out1.setEditable(false); //设为不可编辑
btn1=new Button("输出素数");
btn2=new Button("关闭");
add(lb1);
add(in1);
add(lb2);
add(in2);
add(btn1);
add(btn2);
add(result);
add(out1);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
int k=0; //设计数器
if(e.getSource()==btn1) //响应按钮“输出素数”动作
{
out1.setText("\n"); //新行输出
a=Integer.parseInt(in1.getText());
b=Integer.parseInt(in2.getText());
//判断是否素数,是则计数并加入输出子串S
Loop:
for(int i=b;i<a;i++)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
continue Loop;
}
k++;
if(k%10==0) //每10个数加个回车
s=s+i+","+"\n";
else
s=s+i+","
}
result.setText(b+"与"+a+"之间共有素数"+k+"个,输出如
下:");
outl.setText(s);
}
else System.exit(0);//单击"关闭"退出程序
}
}
7、请创建简单程序ThreeThreads.java,它将创建三个线程,每个线程应该显示它们名字。
//Printme.java
Class printme implements Runnable {
Public void run()
{
for (int x=0;x<10;x++)
{
System.out.println(Thread.currentthread().getname());
try {
Thread.sleep();
}catch(Exception e){}
}
}
}
//TestthreeThreads.java
Public class TestThreeThreads{
Public static void main(String args[])
{
Runnable prog new Printme();
Thread T1= new Thread(prog);
Thread T2= new Thread(prog);
Thread T3= new Thread(prog);
T1.setName( “larry” );
T2.setName( “curly” );
T3.setName( “moe” );
T1.start.();
T2.start.();
T3.start.();
}
}
展开阅读全文