JAVA程序设计机试题目复习大纲.doc
JAVA程序设计机试题目复习大纲1. 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身。public class lianxi03 public static void main(String args) int b1, b2, b3; for(int m=101; m<1000; m+) b3 = m / 100; b2 = m % 100 / 10; b1 = m % 10; if(b3*b3*b3 + b2*b2*b2 + b1*b1*b1) = m) System.out.println(m+"是一个水仙花数"); 2. 判断101-200之间有多少个素数,并输出所有素数。 public class lianxi02 public static void main(String args) int count = 0; for(int i=101; i<200; i+=2) boolean b = false; for(int j=2; j<=Math.sqrt(i); j+) if(i % j = 0) b = false; break; else b = true; if(b = true) count +;System.out.println(i ); System.out.println( "素数个数是: " + count);3. 一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=123.编程 找出1000以内的所有完数。public class lianxi09 public static void main(String args) System.out.println("1到1000的完数有: "); for(int i=1; i<1000; i+) int t = 0; for(int j=1; j<= i/2; j+) if(i % j = 0) t = t + j; if(t = i) System.out.print(i + " "); 4. 有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? public class lianxi11 public static void main(String args) int count = 0; for(int x=1; x<5; x+) for(int y=1; y<5; y+) for(int z=1; z<5; z+) if(x != y && y != z && x != z) count +; System.out.println(x*100 + y*10 + z ); System.out.println("共有" + count + "个三位数"); 5. 输出9*9乘法表。public class lianxi16 public static void main(String args) for(int i=1; i<10; i+) for(int j=1; j<=i; j+) System.out.print(j + "*" + i + "=" + j*i + " " ); if(j*i<10)System.out.print(" "); System.out.println(); 6. 利用菜单和窗口编写一个简单的文本编辑器。 import java.io.*;import java.awt.*;import java.awt.event.*;public class my extends Frame implements ActionListenerprivate static final long serialVersionUID = 1L;FileDialog fileDlg;String str,fileName;byte byteBuf=new byte10000;TextArea ta = new TextArea();MenuBar mb = new MenuBar();Menu ml = new Menu("Menu");MenuItem open = new MenuItem("open");MenuItem close = new MenuItem("clear");MenuItem save = new MenuItem("save");MenuItem exit = new MenuItem("exit");SuppressWarnings("deprecation")my()setTitle("my");setSize(600,400);add("Center",ta);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););ml.add(open);ml.add(close);ml.add(save);ml.addSeparator();ml.add(exit);open.addActionListener(this);save.addActionListener(this);close.addActionListener(this);exit.addActionListener(this);mb.add(ml);setMenuBar(mb);show();SuppressWarnings("deprecation")Overridepublic void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getSource()=exit)System.exit(0);/else if(e.getSource()=close)ta.setText(null);else if(e.getSource()=open)fileDlg = new FileDialog(this,"OpenFile");fileDlg.show(); fileName = fileDlg.getFile(); try FileInputStream in = new FileInputStream(fileName);in.read(byteBuf); in.close(); str = new String (byteBuf); ta.setText(str); setTitle("my-"+fileName); catch(IOException ioe)else if(e.getSource()=save) fileDlg= new FileDialog(this,"saveFile",FileDialog.SAVE);fileDlg.show();fileName = fileDlg.getFile();str = ta.getText();byteBuf = str.getBytes();tryFileOutputStream out = new FileOutputStream(fileName);out.write(byteBuf);out.close();catch (IOException ioe)public static void main (String args)new my();7. 编写一个可以计算面积与体积的接口,以及实现这个接口的四边形类(包括矩形,平行四边形和梯形)。 import java.awt.*; interface Area /可计算面积接口 public abstract double area(); /计算面积 interface Volume /可计算体积接口 public abstract double volume(); /计算体积 class Rectangle implements Area /矩形类,实现可计算面积接口 protected double length, width; /长度和宽度 public Rectangle(double length, double width)/构造方法 this.length = length; this.width = width; public double area() /计算矩形面积,实现Area接口中的抽象方法 return this.width * this.length; public String toString() return "一个矩形,长度"+this.length+",宽度"+this.width+ ",面积为"+this.area(); class Reg extends Rectangle implements Volume /长方体类继承矩形类实现可计算体积接口 protected double height; /高度 public Reg(double length, double width, double height) /构造方法 super(length, width); this.height = height; public double volume() /计算长方体的体积,实现Volume接口中的抽象方法 return super.area() * this.height; public String toString() return "一个长方体,长度"+this.length+",宽度"+this.width+",高度"+this.height +",表面积为"+this.area()+",体积为"+this.volume(); public class a7 public static void main(String args) System.out.println(new Reg(10,20,30).toString(); System.out.println(new Rectangle(10,20).toString(); 8. 利用文本框、标签、按钮等完成一个界面:输入一个平时成绩,一个实验成绩,一个期末成绩。按照一定的比例自动出现一个综合成绩。import java.applet.*;import java.awt.*;import java.awt.event.*;public class a8 extends Applet implements ActionListenerTextField txtSY;TextField txtPS;TextField txtQM;TextField txtSum;Button bt;public void init()txtSY = new TextField(15);txtPS = new TextField(15);txtQM = new TextField(15);txtSum = new TextField(15);txtSum.setEditable(false);bt=new Button("计算");add(new Label(" 实验成绩");add(txtSY);add(new Label(" 平时成绩");add(txtPS);add(new Label(" 期末成绩");add(txtQM);add(new Label("总成绩");add(txtSum);add(bt);bt.addActionListener(new ButtonAct();this.setSize(250, 150);this.setVisible(true);class ButtonAct implements ActionListenerpublic void actionPerformed(ActionEvent e)int op1,op2,op3;double op4;if(e.getSource()=bt)op1=Integer.valueOf(txtSY.getText();op2=Integer.valueOf(txtPS.getText();op3=Integer.valueOf(txtQM.getText();op4=op1*0.2+op2*0.2+op3*0.6;txtSum.setText(String.valueOf(op4);9. 利用文本框、标签、按钮等完成一个界面:选择输入的是“人民币”、“美元”、“欧元”:转换成另外两种币种的货币转换界面。10. 编写一个能够进行简单四则混合运算的计算器。 import java.applet.*;import java.awt.*;import java.awt.event.*;public class a10 extends Applet TextField txt1,txt2,txt3;Button bt1,bt2;CheckboxGroup select;Checkbox a,s,m,d;public void init()txt1 = new TextField(15);txt2 = new TextField(15);txt3 = new TextField(15);bt1=new Button("计算");bt2=new Button("重置");select=new CheckboxGroup();a=new Checkbox("加",true,select);s=new Checkbox("减",true,select);m=new Checkbox("乘",true,select);d=new Checkbox("除",true,select);add(bt1);add(bt2);add(txt1);add(txt2);add(txt3);add(a);add(s);add(m);add(d);txt3.setEditable(false);bt1.addActionListener(new ButtonAct();bt2.addActionListener(new ButtonAct();this.setVisible(true);class ButtonAct implements ActionListenerpublic void actionPerformed(ActionEvent e)int op1,op2,op3;if(e.getSource()=bt1)op1=Integer.valueOf(txt1.getText();op2=Integer.valueOf(txt2.getText();if(a.getState()op3=op1+op2;else if(s.getState()op3=op1-op2;else if(m.getState()op3=op1*op2;else op3=op1/op2;txt3.setText(Integer.toString(op3);elsetxt1.setText("");txt1.setText("");txt1.setText("");a.setState(true);11. 编写一个求平均值的接口与实现该接口的类。 import java.awt.*; import java.awt.event.*; interface Average public abstract double getAverage(double data);class avg1 implements Average public double getAverage(double data) double sum = 0;/总和 double result = 0;/结果 int length = data.length; for(int i=0;i<length;i+) sum += datai; result = sum / length; return result; public class a public static void main(String arg) Method1 h=new Method1(); double arr=2,3,4,5,6; double x= h.getAverage(arr); System.out.print(x); 12. 利用窗体和文本域以及上题中设计的类设计一个“模拟裁判评分”的程序。import java.awt.*; import java.awt.event.*; interface Average public abstract double getAverage(double data);class avg2 implements Average public double getAverage(double data) double sum = 0;/总和 double result = 0;/结果 int length = data.length; for(int i=0;i<length;i+) sum += datai; sum = sum - this.getMax(data) - this.getMin(data);/总和减去最大值、最小值 length -=2;/个数减去2 result = sum / length; return result; /获得数组的最大值 public double getMax(double data) double max = 0; for(int i=0;i<data.length;i+) if(datai >= max) max = datai; return max; /获得数组的最小值 public double getMin(double data) double min = 0; for(int i=0;i<data.length;i+) if(datai <= min) min = datai; return min; public class a public static void main(String arg) avg2 h=new avg2(); double arr=2,5,4,6,10; double avg= h.getAverage(arr); System.out.print(avg); 13. 设计一个数字时钟。 import java.awt.*;import java.util.*;import javax.swing.*;/数字时钟public class m11 extends JFrame implements RunnableThread clock;public m11()super("数字时钟");/调用父类构造函数setFont(new Font("Times New Roman",Font.BOLD,60);/设置时钟的显示字体start(); /开始进程setSize(280,100); /设置窗口尺寸setVisible(true); /窗口可视this.setLocation(440,330);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /关闭窗口时退出程序public void start() /开始进程if (clock=null) /如果进程为空值clock=new Thread(this); /实例化进程clock.start(); /开始进程public void run() /运行进程while (clock!=null) repaint(); /调用paint方法重绘界面tryThread.sleep(1000); /线程暂停一秒(1000毫秒)catch (InterruptedException ex)ex.printStackTrace(); /输出出错信息public void stop() /停止进程clock=null;public void paint(Graphics g) /重载组件的paint方法Graphics2D g2=(Graphics2D)g; /得到Graphics2D对象Calendar now=new GregorianCalendar(); /实例化日历对象String timeInfo="" /输出信息int hour=now.get(Calendar.HOUR_OF_DAY); /得到小时数int minute=now.get(Calendar.MINUTE); /得到分数int second=now.get(Calendar.SECOND); /得到秒数if (hour<=9) timeInfo+="0"+hour+":" /格式化输出else timeInfo+=hour+":"if (minute<=9)timeInfo+="0"+minute+":"elsetimeInfo+=minute+":"if (second<=9)timeInfo+="0"+second;elsetimeInfo+=second;g.setColor(Color.orange); /设置当前颜色为白色Dimension dim=getSize(); /得到窗口尺寸g.fillRect(0,0,dim.width,dim.height); /填充背景色为白色g.setColor(Color.black); /设置当前颜色为橙色g.drawString(timeInfo,20,80); /显示时间字符串public static void main(String args)new m11();14. 在页面中指定位置绘制矩形,椭圆,直线和曲线等组成一定的图案。 15. 设计一个界面,单击鼠标画线,双击鼠标擦除所有画的线。import java.awt.*;import java.awt.event.*; / Must add thisimport java.util.Vector;class a public static void main(String args) myFrame f1 = new myFrame(); f1.setSize(400, 400); f1.setVisible(true);class myFrame extends Frame Vector m_Locs = new Vector(); Point m_CursorLoc=new Point(0,0) ;myFrame() addMouseListener(new click(); addMouseMotionListener(new move();addWindowListener(new wd(); public void paint(Graphics g) int x = m_CursorLoc.x;int y = m_CursorLoc.y;g.drawLine(x-10,y,x+10,y); /在光标处画十字g.drawLine(x,y-10,x,y+10);Point From;Point To;int nSize = m_Locs.size(); /取向量元素个数for(int i=0;i<nSize-1;i+) /对向量中全部保留点,每两点间连线From = (Point)m_Locs.elementAt(i);for(int j=i+1;j<nSize;j+)To = (Point)m_Locs.elementAt(j);g.drawLine(From.x,From.y,To.x,To.y); class click extends MouseAdapter public void mouseClicked(MouseEvent e) /单、双击鼠标事件处理 if(e.getClickCount()=2) m_Locs.removeAllElements(); /双击清空向量元素 else m_Locs.addElement(e.getPoint(); /单击保留坐标点 repaint(); class move extends MouseMotionAdapter public void mouseMoved(MouseEvent e) /移动鼠标事件处理 m_CursorLoc = e.getPoint(); repaint(); class wd extends WindowAdapter public void windowClosing(WindowEvent e) System.exit(0);