java考试参考题.doc_第1页
java考试参考题.doc_第2页
java考试参考题.doc_第3页
java考试参考题.doc_第4页
java考试参考题.doc_第5页
已阅读5页,还剩26页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1) 利用随机函数定义10对(x,y)值,由此创建的Point类实例存入一个数组中,按与原点(0,0)的距离由小到大的顺序输出所有的点及到原点的距离。【参考程序】public class point private int x; private int y; private double dic; public point() public point(int x1,int y1) this.x=x1; this.y=y1; public void distance() double temp=Math.pow(this.x,2)+Math.pow(this.y,2); this.dic=Math.sqrt(temp); public static void main(String args) point s=new point10; for(int i=0;i10;i+) int x,y; si=new point(x=(int)(Math.random()*10),y=(int)(Math.random()*10); si.distance(); output(s); for(int i=0;i9;i+) for(int j=i+1;jsj.dic) point temp=si; si=sj; sj=temp; System.out.println(); output(s); static void output(point s) for(int i=0;ic&a-bc) double p=(a+b+c)/2; double ans=p*(p-a)*(p-b)*(p-c); area=Math.sqrt(ans); else double temp=Math.min(a,b); temp=Math.min(temp, c); area=(temp*temp*(Math.sqrt(3)/4; public static void main(String args) try System.out.println(输入三个实数:); BufferedReader br=new BufferedReader(new InputStreamReader(System.in); String s=br.readLine(); double x=Double.parseDouble(s); String q=br.readLine(); double y=Double.parseDouble(q); String w=br.readLine(); double z=Double.parseDouble(w); triangle ans=new triangle(x,y,z); System.out.println(a=+x+,b=+y+,c=+z); ans.trianglearea(); System.out.println(ans.area); catch(IOException e) 1) 编程根据一个已存在的链表创建一个倒序链表。利用如下方法实现一个链表的倒序。static Link reverse( Link list ) Link rev = null; Link runner = list; while (runner != null) Link newNode = new Link; newNode.data = runner.data; newNode.next = rev; rev = newNode; runner = runner.next; return rev; 【参考程序】class Node int data;/存放数据; Node next; /存放下一个节点;public class Link Node header; public Link()/构造无参数的构造方法; /构造带参数的构造方法,构造新的链表; public Link(int n) Node p,q; /定义头节点; header =new Node(); header.data=(int)(Math.random()*100); /使p指向头节点; p=header; for(int k=1;k; if(p!=null)System.out.print(-); System.out.println(); public static void main(String args) Link x=new Link(10); /输出原链表; x.outputLink(); Link y=null;/System.out.println(*); y=x.reverse(x); /输出原链表的倒叙的链表; y.outputLink(); /* * 1设有一个由10个英文单词构成的字符串数组,要求:(1)统计以字母w开头的单词数;(2)统计单词中含“or”字符串的单词数;(3)统计长度为3的单词数。 */import java.io.*;public class Count public static String input() throws IOException BufferedReader br=new BufferedReader(new InputStreamReader(System.in);String s=new String10;for(int i=0;is.length;i+) System.out.println(请输入第+(i+1)+个单词:);si=br.readLine();return s;public static int countW(String s) int count=0;for(int i=0;is.length;i+) if(si.charAt(0)=w)count+;return count;public static int countOr(String s) int count=0;for(int i=0;is.length;i+) if(si.contains(or)count+;return count;public static int count3(String s) int count=0;for(int i=0;is.length;i+) if(si.length()=3)count+;return count;public static void main(String args) throws IOException String s=input();System.out.println(以字母w开头的单词数:+countW(s);System.out.println(单词中含“or”字符串的单词数:+countOr(s);System.out.println(长度为3的单词数:+count3(s);(2)输入一个算术表达式,例如:45*2+23*(234-24),求出其中有多少整数常数。import java.io.*;public class biaodashi public static String input() throws IOException BufferedReader br=new BufferedReader(new InputStreamReader(System.in); String s=; System.out.println(请输入一个算术表达式:);s=br.readLine();return s; public static int countInt(String s) int count=0; char ch; /获取串中的单个字符 ch=s.charAt(0); /if(Character.isDigit(ch) /count=1; boolean flag=false;/标记当前字符是否为数字 for(int i=0;is.length();i+) ch=s.charAt(i); if(Character.isDigit(ch) /如果当前字符是数字 flag=true; else if(flag) /当前不为数字,但前一个为数字 count+; flag=false; return count; public static void main(String args) throws IOException String s=input();System.out.println(算术表达式:+s+中有+countInt(s)+个整常数);1)根据Applet大小绘制若干同心圆,相邻圆之间的间距为10个象素。import java.applet.*;import java.awt.*;SuppressWarnings(serial)public class ConcentricCircles extends Appletint x,y;/最外层同心圆所在外切矩形的左上角坐标int width,height;/最外层同心圆所在外切矩形的宽、高public void paint(Graphics g) x=y=10;width=getWidth()-20;height=width;g.setColor(Color.magenta);for(;width-=20,height-=20) g.drawOval(x, y, width, height);x+=10;y+=10;if(width10)break;(3)棋盘。import java.awt.*;import java.applet.*; public class checkboard extends Applet public void paint(Graphics g) int row,col; int x,y,white,black; for(row=0;row19;row+) for(col=0;col19;col+) white=(int)Math.floor(Math.random()*256); black=(int)Math.floor(Math.random()*256); /orange=(int)Math.floor(Math.random()*256); / pink=(int)Math.floor(Math.random()*256); x=col*20; y=row*20; g.setColor(new Color(white,black,white,black); g.fillRect(x,y,20,20); * * 4)利用鼠标事件实现一个拉橡皮筋方式绘制直线的程序。鼠标按下开始算始点,拖动鼠标至 * 终点,在始点和终点之间绘制直线,在拖动鼠标的过程中,总在始点和鼠标位置绘制直线, * 但只有最后释放鼠标时直线为最终需要的直线。 */import java.awt.*;import java.awt.event.*;SuppressWarnings(serial)public class DrawLine extends Frameint x,y;/记住鼠标开始pressed时的坐标位置!int posx,posy;public DrawLine() / TODO Auto-generated constructor stubthis.addMouseListener(new MouseAdapter() Overridepublic void mousePressed(MouseEvent e) / TODO Auto-generated method stubx=e.getX();y=e.getY();Overridepublic void mouseReleased(MouseEvent e) / TODO Auto-generated method stubposx=e.getX();posy=e.getY();repaint(););this.addMouseMotionListener(new MouseMotionAdapter() public void mouseDragged(MouseEvent e) posx=e.getX();posy=e.getY();repaint(););this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dispose(););setSize(500,400);setVisible(true); public void paint(Graphics g) g.setColor(Color.red); /g.drawLine(lastx, lasty, posx, posy); g.drawLine(x, y, posx, posy); /* * param args */public static void main(String args) / TODO Auto-generated method stub new DrawLine();/* * 1) 输入若干行文本,以end作为结束行,统计该文本由多少行组成,字符总数,有多少 * “the”,将整个文本中所有单词首字母为小写的改为大写输出。 */import java.io.*;import java.util.*;public class End private int line,charCount,countThe;private StringBuffer sb;private Vector v;private Vector vsb;public End()sb=new StringBuffer(); v=new Vector();vsb=new Vector();public void input() throws IOException String s=;System.out.println(请输入若干行文本,以end作为结束行:);BufferedReader br=new BufferedReader(new InputStreamReader(System.in);s=br.readLine();while(!s.equals(end) v.add(s);StringBuffer ssb=new StringBuffer(s);vsb.add(ssb);s+=n;line+;sb.append(s);s=br.readLine();public void count() for(int i=0;iv.size();i+) String s=v.get(i).split( );String ss=;for(int j=0;js.length;j+) if(sj.contains(the)countThe+;ss+=sj;charCount+=ss.length();public void translate() for(int i=0;ivsb.size();i+) for(int j=0;jvsb.get(i).length();j+)if(j=0|vsb.get(i).charAt(j-1)= )vsb.get(i).setCharAt(j,Character.toUpperCase(vsb.get(i).charAt(j);public void output() System.out.println(该文本由+line+行组成,字符总数为:+charCount+,有+countThe+ 个“the”);System.out.println(将整个文本中所有单词首字母为小写的改为大写输出:);System.out.println(vsb.toString();public static void main(String args) throws IOException End e=new End();e.input();e.count();e.translate();e.output();/* * 4)编写一个鼠标位置跟踪程序,在鼠标所在位置为中心绘制一个红色十字架。 */import java.awt.*;import java.awt.event.*;SuppressWarnings(serial)public class MouseFollowing extends Frame implements MouseMotionListenerint x,y;public MouseFollowing() / TODO Auto-generated constructor stubthis.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dispose(););/add(p);this.addMouseMotionListener(this);setSize(500,400);setVisible(true); /* * param args */public void paint(Graphics g) g.setColor(Color.red);g.drawLine(x-10, y, x+10, y);g.drawLine(x, y-10, x, y+10);public static void main(String args) / TODO Auto-generated method stub new MouseFollowing();public void mouseDragged(MouseEvent e) Overridepublic void mouseMoved(MouseEvent e) / TODO Auto-generated method stubx=e.getX();y=e.getY();repaint();/* * 2)通过Applet参数提供两个间距在100以内的整数,找出这两个整数之间的所有素数, * 按每行5个在Applet画面上输出。 */import java.applet.*;import java.awt.*;SuppressWarnings(serial)public class PrimeNumber extends Appletint a=100,b=3;/100以内的整数int x=10,y=10;/坐标位置public void paint(Graphics g) int count=1;for(int i=b;i=a;i+) int j=2;for(;j(Math.sqrt(i) g.drawString(+i, x, y);if(count%5=0) x=10;/换行y+=20;/ifif(count%5!=0)x+=20;count+;/if/for创建带有标签和文本域的窗体。当用户在文本域中输入其姓名后,显示欢迎用户使用 java 编程的消息。例如用户输入姓名 flyhorse ,则显示消息“你好, flyhorse, 欢迎你使用 java 编程!”,并能正常关闭窗口。【参考程序】 import java.awt.*;import java.awt.event.*;class closeWin extends WindowAdapterpublic void windowClosing(WindowEvent e)Window w=e.getWindow();w.dispose();public class myFrame1 extends Frame implements ActionListenerLabel welcome;TextField input;public myFrame1() super(welcome); input=new TextField(15); welcome=new Label(); Button b=new Button(sure); setLayout(new GridLayout(3,1); add(input); add(b); add(welcome); b.addActionListener(this); addWindowListener(new closeWin(); public void actionPerformed(ActionEvent e) String s=input.getText();welcome.setText(你好,+s+,欢迎你使用java编程!); public static void main(String args) Frame x=new myFrame1(); x.setSize(400,200); x.setVisible(true); 2)在界面中安排一个文本框和一个标签,在文本框输入一个整数按回车后,在标签中显示该数最高位数字。【参考程序】import java.awt.*;import java.awt.event.*;class closeWin extends WindowAdapterpublic void windowClosing(WindowEvent e)Window w=e.getWindow();w.dispose();public class myFrame2 extends Frame implements ActionListenerLabel topDigit;TextField num;public myFrame2()super(求最高位数);topDigit=new Label(最高位数为: );Button b=new Button(输入);num=new TextField(20);setLayout(new FlowLayout();add(num);add(b);add(topDigit);b.addActionListener(this);addWindowListener(new closeWin();Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubString s=num.getText();int a=Integer.parseInt(s);int ans = 0;while(a0)ans=a;a/=10;topDigit.setText(+ans);public static void main(String args)Frame x=new myFrame2();x.setSize(200, 100);x.setVisible(true);3)设有一批英文单词存放在一个数组中,编制一个图形界面程序浏览单词。在界面中安排一个标签显示单词,另有“上一个”、“下一个”两个按钮实现单词的前后翻动。【参考程序】import java.awt.*;import java.awt.event.*;class closeWin extends WindowAdapterpublic void windowClosing(WindowEvent e)Window w=e.getWindow();w.dispose();public class myFrame3 extends Frame implements ActionListenerLabel letter;Button up,down;int i=0;public myFrame3()letter=new Label(单词: );up=new Button(up);down=new Button(down);setLayout(new FlowLayout();add(letter);add(up);add(down);up.addActionListener(this);down.addActionListener(this);addWindowListener(new closeWin();public void actionPerformed(ActionEvent e) String word=one,zero,ten,six,five;letter.setText(word0);if(up.getActionCommand().equals(up)i-;if(i=-1)i=4;letter.setText(wordi);else i+;if(i=5)i=0;letter.setText(wordi);public static void main(String args) Frame x=new myFrame3(); x.setSize(200,200); x.setVisible(true);样例1: 创建5个学生对象给一个学生数组赋值,每个学生属性有:学号、姓名、年龄(1)将学生按学号排序输出;(2)给所有学生年龄加1;(3)统计大于20岁的学生人数。【参考程序】public class Student int num; int age; String name; public String toString( ) String s = 学号: + num + ,; s += 姓名: + name + ,; s += 年龄: + age; return s; public Student(int Num,int Age,String Name) num = Num; age = Age; name = Name; public static void main(String args) Student s1 = new Student(3,18,张三); Student s2 = new Student(1,21,小路); Student s3 = new Student(33,20,John); Student s4 = new Student(13,20,Lucy); Student s5 = new Student(8,17,Jack); Student s = s1,s2,s3,s4,s5; System.out.println(班级学生名单如下:); output(s); /第1次调用output方法输出数组 /* 将学生按学号排序 */ for (int i=0;is.length-1;i+) for (int j=i+1;jsj.num) Student tmp=si; si=sj; sj=tmp; System.out.println(按学号由小到大排序.); output(s); /第2次调用output方法输出数组 for (int i=0;is.length;i+) /将所有学生年龄加1 si.age+; System.out.println(所有学生年龄加1后.); output(s); /第3次调用output方法输出数组 /* 以下统计大于20岁的学生个数 */ int count=0; for (int i=0;i=20) count+; System.out.println(大于20岁人数是: + count);/* 以下方法输出学生数组的所有元素 */ static void output(Student s) for (int i=0;is.length;i+) System.out.println(si); 样例1:编写一个方法统计一个字符在某字符串中的出现次数;从命令行参数获取一个算术表达式,利用前面方法判断该表达式的左右括号数量是否一致,如果一致,输出“YES”,否则输出“NO”。【参考程序】public class Count public static void main(String args) String s = args0; if ( count(s,( ) = count(s,) /看左括号与右括号的出现次数是否一样多 System.out.println( YES ); else System.out.println( NO ); /* 统计字符c在串s中的出现次数 */public static int count(String s ,char c) int x=0; /计数变量for (int k=0;ks.length();k+) if (s.charAt(k)=c) x+; /在第k个位置出现,则计数增1return x; 样例2:四位同学中一位做了好事,校长问这四位是谁做的好事。A说:不是我。B说:是C。C说:是D。D说:C胡说。已知三个人说的是真话,一个人说的是假话。根据这些信息,找出做了好事的人。【参考程序】public class good public static void main(String a) char man; /做好事的人 String x=ABCD; /A、B、C、D中的一个 for (int k=0;k=3;k+) /用循环去测试各种情形 int co

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论