JAVA程序设计复赛A卷
- 格式:doc
- 大小:105.00 KB
- 文档页数:7
操作题:(请将编写好的源程序以题号命名,例如第1题的源程序保存为“1.java”)1、编写一个Java应用程序,计算并输出一维数组(9.8,12,45,67,23,1.98,2.55,45)中的最大值和最小值。
(本题20分)public class 1{public static void main(String[] args){float max,min;float[] a={9.8f,12f,45f,67,23f,1.98f,2.55f,45f};max=a[0];min=a[0];for(int i=0;i<=a.length-1;i++){if(max<=a[i]) {max=a[i];}}for(int i=0;i<=a.length-1;i++){if(min>=a[i]) {min=a[i];}}System.out.println("该书中最大的树是"+max+'\t'+"该书中最小的数是"+min);}}2、编写一个Java应用程序,该程序使用FileInputStream类,实现从磁盘读取本应用程序源代码文件,并将文件内容显示在屏幕上。
(本题20分)import java.io.*;public class 2{public static void main (String[] args) throws IOException{FileInputStream fis=new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\a1.java");int n;while((n=fis.read())!=-1)System.out.print((char)n);fis.close();}}3、编写一个Java应用程序,利用RandomAccessFile类,把几个int型整数(1,2,3,4,5,6,7,8,9,10)写入到一个名字为tom.dat文件中,然后按相反顺序读出这些数据并显示在屏幕上。
(注意,一个int型数据占4个字节)(本题30分)import java.io.*;public class 3{public static void main(String[] args) throws IOException{int[]bytes={1,2,3,4,5,6,7,8,9,10};RandomAccessFile r=new RandomAccessFile("c:\\a.txt","rw");for(int item:bytes) r.writeInt(item);long p = r.getFilePointer();for(int i=(int)p;i>0;i=i-4){r.seek(i-4);System.out.print(r.readInt());}}}4、编写一个Java GUI应用程序,采用Java多线程技术,模拟自由落体和平抛运动:一个球自由落下,一个球水平抛出。
(本题30分)(自由落体物理公式:h= g *t2/2 ;平抛运动物理公式:h= g *t2/2 ,x=26*t ;h代表高度,t代表时间,g代表重力加速度=9.8 m/s2 )import java.awt.BorderLayout;import java.awt.Button;import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Panel;import java.awt.Point;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.List;public class MyFrame extends Frame implements Runnable,ActionListener{double x1 = 100,y1 = 0;//A的起始位置double x2 = 100,y2 = 0;//B的起始位置double s2 = 26;//B的水平速度double g = 9.8;//Glong time = 10000;//模拟10秒钟double py = 1;//y轴比例尺List<Point> list1 = new ArrayList<Point>();List<Point> list2 = new ArrayList<Point>();MPanel p1 = new MPanel();Button b1 = new Button("启动");Thread t ;public MyFrame(){b1.addActionListener(this);this.add(b1,BorderLayout.NORTH);this.add(p1,BorderLayout.CENTER);this.setSize(800,600);this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e) {System.exit(0);}});this.setVisible(true);}public void run(){list1 = new ArrayList<Point>();list2 = new ArrayList<Point>();double xx1 = x1,yy1 = y1,xx2 = x2,yy2 = y2;//本时刻位置long start = System.currentTimeMillis();long end = start;while(end-start<=time){end = System.currentTimeMillis();double t = (end - start)/1000.0;yy1 = (y1 + g*t*t/2)*py;xx2 = x1 + s2*t;yy2 = (y2 + g*t*t/2)*py;int sx1 = (int)xx1;int sy1 = (int)yy1;int sx2 = (int)xx2;int sy2 = (int)yy2;list1.add(new Point(sx1,sy1));list2.add(new Point(sx2,sy2));p1.repaint();try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}}public void actionPerformed(ActionEvent e) {if (t==null||!t.isAlive()){t = new Thread(this);t.start();}}public class MPanel extends Panel{BufferedImage im = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); public void paint(Graphics gg){Graphics g = im.getGraphics();g.setColor(Color.white);g.fillRect(0,0,800,600);g.setColor(Color.blue);Point ppp1 = null;Point ppp2 = null;for (int i=0;i<list1.size();i++){Point pp1 = list1.get(i);Point pp2 = list2.get(i);if (ppp1!=null&&ppp2!=null){g.drawLine(pp1.x,pp1.y,ppp1.x,ppp1.y);g.drawLine(pp2.x,pp2.y,ppp2.x,ppp2.y);}ppp1 = pp1;ppp2 = pp2;}if (ppp1!=null&&ppp2!=null){g.fillOval(ppp1.x-5,ppp1.y-5,10,10);g.fillOval(ppp2.x-5,ppp2.y-5,10,10);}g.dispose();gg.drawImage(im,0,0,800,600,p1);}}public static void main(String[] args) {new MyFrame();}}附加题:5、编写一个Java应用程序,实现如下功能:1)判断两个字符串是否相同,s1=”you are a student”,s2=”how are you”;2)判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同;3)按字典顺序比较两个字符串”你”和”我”的大小关系;4)将数字型字符串”100”和”123.678”转换为数字;5)将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上。
(本题20分)public class Test {public static void main(String[] args) {String s1="you are a student";String s2="how are you";if(s1.equals(s2)){System.out.println(s1+"和"+s2+"相同");}else{System.out.println(s1+"和"+s2+"不同");}//判断字符串”22030219851022024”的前缀、后缀是否和某个字符串”220302”相同; String s3="22030219851022024";String s4="220302";if(s3.startsWith(s4)){System.out.println(s3+"的前缀为"+s4);}else{System.out.println(s3+"的前缀不为"+s4);}if(s3.endsWith(s4)){System.out.println(s3+"的后缀为"+s4);}else{System.out.println(s3+"的后缀不为"+s4);}//按字典顺序比较两个字符串”你”和”我”的大小关系;String s5="你";String s6="我";if(pareTo(s6)<0){System.out.println("\""+s5+"\"比\""+s6+"\"大");}else if(pareTo(s6)==0){System.out.println("\""+s5+"\"和\""+s6+"\"等");}else{System.out.println("\""+s5+"\"比\""+s6+"\"小");}//将数字型字符串”100”和”123.678”转换为数字String s7="100";String s8="123.678";int num1=Integer.parseInt(s7);Double num2=Double.parseDouble(s8);System.out.println(num1);System.out.println(num2);//将字符串”FEDCBA”存放到数组中,调用for循环读出数组数据显示在屏幕上String s9="FEDCBA";char [] arr=s9.toCharArray();for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}}}6、编写客户/服务器程序,客户端Client.java使用DatagramSocket对象将数据包发送到服务器,请求获取服务器端的图像(考生可自选图像文件)。