您的位置:首页 > 编程语言 > Java开发

第三届全国ITAT教育工程就业技能大赛复赛试题 Java程序设计(A卷)答案

2011-11-02 01:16 447 查看
1、 编写一个Java应用程序,计算并输出一维数组(9.8,12,45,67,23,1.98,2.55,45)中的最大值和最小值。(本题20分)

package programming;

public class Array {

public double getMax(double[] a)
{
double max=a[0];
for(int i=1;i<a.length;i++)
if(max<a[i])
max=a[i];
return max;

}
public double getMin(double[] a)
{
double min=a[0];
for(int i=1;i<a.length;i++)
if(a[i]<min)
min=a[i];
return min;
}
public static void main(String[] args) {
doublea[]={9.8,12,45,67,23,1.98,2.55,45};
Array A=new Array();
System.out.println(A.getMax(a));
System.out.println(A.getMin(a));

}
}


2、 编写一个Java应用程序,该程序使用FileInputStream类,实现从磁盘读取本应用程序源代码文件,并将文件内容显示在屏幕上。(本题20分)

package programming;
import java.io.*;

public class ReadFile {

public static void main(String[] args){
String s;
try{
//1、通过使用FileInputStream类实现
FileInputStreamin=
new FileInputStream
("D:\\Java\\javaCompetition\\src\\programming\\ReadFile.java");
byte[] b=new byte[in.available()];
while(in.read(b)!=-1)
System.out.println(new String(b));
in.close();
//2、通过使用BufferedReader类实现
/*BufferedReader br=newBufferedReader
(new InputStreamReader
(new FileInputStream("D:\\Java\\javaCompetition\\src\\programming\\ReadFile.java")));
while((s=br.readLine())!=null)
System.out.println(s);*/
}
catch(FileNotFoundException fe)
{fe.printStackTrace();}
catch(IOException ie)
{ie.printStackTrace();}
}

}


3、 编写一个Java应用程序,利用RandomAccessFile类,把几个int型整数(1,2,3,4,5,6,7,8,9,10)写入到一个名字为tom.dat文件中,然后按相反顺序读出这些数据并显示在屏幕上。(注意,一个int型数据占4个字节)(本题30分)

package programming;
import java.io.*;
public class RandomAccess {

public static void main(String[] args) {

try{
RandomAccessFile raf=new RandomAccessFile("tom.dat","rw");
raf.setLength(0);
for(int i=1;i<=10;i++)
raf.writeInt(i);
System.out.println("文件长度(注意,以字节为单位):"+raf.length());

for(int i=9;i>=0;i--)
{
raf.seek(i*4);
System.out.print(raf.readInt()+" ");
}
System.out.print("\n");
raf.seek(0);//文件指针移动至文件开头
System.out.println("第一个数字:"+raf.readInt());
raf.seek(raf.length());//文件指针移动至文件末尾
raf.writeInt(90);//在文件末尾添加一个新的int值
raf.seek(40);//文件指针移动至最后一个int值处
System.out.println("最后一个数字:"+raf.readInt());
raf.close();
}catch(FileNotFoundException fe)
{fe.printStackTrace();}
catch(IOException e)
{e.printStackTrace();}
}
}


4、 编写一个JavaGUI应用程序,采用Java多线程技术,模拟自由落体和平抛运动:一个球自由落下,一个球水平抛出。(本题30分)

(自由落体物理公式:h= g *t2/2 ;平抛运动物理公式:h= g *t2/2 ,x=26*t ;

h代表高度,t代表时间,g代表重力加速度=9.8 m/s2 )

package programming;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class GuiMultiThread extends JFrame {

public GuiMultiThread()
{
MultiThreadPanel mtp=newMultiThreadPanel();
this.add(mtp);
this.setSize(500, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
Thread thread=new Thread(mtp);
thread.start();
}

public static void main(String[] args) {
new GuiMultiThread();
}

}
class MultiThreadPanel extends JPanelimplements Runnable
{
double x1,x2=0,y=0,t=0;

public void paintComponent(Graphics g)
{
super.paintComponent(g);
y=9.8*t*t/2;
x2=26*t;
g.fillOval((int)x1,(int)y,40,40);
g.fillOval((int)x2, (int)y, 40, 40);

}
public void run()
{
try{
while(true){
Thread.sleep(50);
t+=0.5;
this.repaint();
}

}catch(InterruptedException i)
{i.printStackTrace();}

}

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