2010年秋季JAVA考试编程题总结_第1页
2010年秋季JAVA考试编程题总结_第2页
2010年秋季JAVA考试编程题总结_第3页
2010年秋季JAVA考试编程题总结_第4页
2010年秋季JAVA考试编程题总结_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

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

文档简介

1编写一个程序,程序能在命令行中输出“早上好,good Morning”。 public class Hello public static void main (String args) System.out.println(早上好,goodMoeing); 2编写一个 Java Applet 程序,程序能在浏览器中显示你好,hello。 import java.applet.*; import java.awt.*; public class Boy extends Applet public void paint(Graphics g) /g.setColor(Color.blue); g.drawString(你好,hello,12,30); 3.编写一个程序,输出 ASICII 字母表。 public class java1 public static void main(String args) char c=!; System.out.println(字母+c+在ASICII表中的顺序位置:+(int)c); System.out.println(输出ASICII字母表:); for(int i=(int)c;ic+94;i+) System.out.print( +(char)i); 4. 用户从键盘只能输入整数,程序输出这些整数的乘积。 import java.util.*; public class ZuoYe2_1 public static void main(String args) Scanner reader=new Scanner(System.in); long s=1; int m=0; while(reader.hasNextInt() int x=reader.nextInt(); m=m+1; s=s*x; System.out.println(m+个数的乘积为+s); 5.有一函数:从键盘输入一个 X 值,程序输出 Y 值。 import java.util.*; public class ZuoYe2_1 public static void main(String args) Scanner reader=new Scanner(System.in); double y=0,x=0; x=reader.nextDouble(); if(x0) y=-1+3*x; System.out.println(int)y); 6. 使用 while 循环计算 11000 之间能被 3 和 7 同时整除的整数之和。 public class ZuoYe2_1 public static void main(String args) int sum=0,m=3,n=7,a=1; while(a=1000) if(a%m=0 a+; System.out.println(sum=+sum); 7. 编写一个 Java 应用程序,使用 for 循环计算 8+88+888+8888+88888+的前 10 项之和。 public class ZuoYe2_1 public static void main(String args) long sum=0,a=8,item=a,i=1; for(i=1;i=10;i+) sum=sum+item; item=item*10+a; System.out.println(sum); 8. 编写一个 Java 应用程序,计算 1-1/3+1/5-1/7+1/9-1/11+的前 10000 项之和。 public class ZuoYe2_1 public static void main(String args) double sum=0,a=1,b=1,f=1,item=a/b; int i=1; while(i=1000) sum=sum+f*item; i+; f=f*(-1); b=b+2; item=a/b; System.out.println(sum=+sum); 9. 编写一个 Java 应用程序,计算 1+2!+3!+4!+从 100 项到 200 项之和。 public class ZuoYe2_1 public static void main(String args) double sum=0,a=1; int i=1; while(i=10) sum=sum+a; i+; a=a*i; System.out.println(sum=+sum); 10. 编写一个类,该类创建的对象可以计算等差数列的和。 class DengCha int start,d; DengCha() DengCha(int start,int d) this.start=start; this.d=d; void setStart(int s) start=s; void setD(int d) this.d=d; int getSum(int n) int sum=0,i=1; while(i=n) sum=sum+start; start=start+d; i+; return sum; public class ZuoYe2_1 public static void main (String args ) DengCha shulie=new DengCha(2,3); System.out.println(shulie.getSum(100); shulie.setStart(10); shulie.setD(5);System.out.println(shulie.getSum(9); 11. 编一个类,该类创建的对象可以输出英文字母表。 class Letter public void printLetter() for(char c=a;c=0) root1=(-b+Math.sqrt(disk)/(2*a);root2=(-b-Math.sqrt(disk)/(2*a); System.out.printf(方程的根:%f,%fn,root1,root2); else System.out.printf(方程没有实根n); else System.out.println(不是一元2次方程); public void setCoefficient(double a,double b,double c) this.a=a; this.b=b; SquareEquation.c=c; if(a!=0)boo=true; elseboo=false; public class ZuoYe2_1 public static void main(String args ) SquareEquation equation1=new SquareEquation(4,5,1); SquareEquation equation2=new SquareEquation(3,5,-7); equation1.getRoots(); equation2.getRoots(); 13. 编写两个类,A 和 B,类 A 创建的对象可以计算两个正整数的最大公约数,类 B 创建的对象可以计算两个 书的最小公倍数。要求:类 B 中有一个成员变量是用类 A 的声明对象。 import java.util.Scanner; class A int f(int m,int n) if(m*n0) System.out.println(有负数,程序退出); System.exit(0); if(mn) int temp=m; m=n; n=temp; int a=m,b=n; int r=m%n; while(r!=0) m=n; n=r; r=m%n; return n; class B A a; B() a=new A(); int g(int m,int n) int temp=a.f(m,n); return m*n/temp; public class ZuoYe2_1 public static void main (String args ) Scanner reader=new Scanner(System.in); System.out.println(输入2个正整数,程序计算出它们的最大公约数和最小公倍数); System.out.print(输入第一个整数:); int m=reader.nextInt(); System.out.print(输入第二个整数:); int n=reader.nextInt(); A a=new A(); B b=new B(); System.out.println(m+和+n+的最大公约数是+a.f(m,n); System.out.println(m+和+n+的最小公倍数是+b.g(m,n); 14.编写使用了包语句的类,然后在应用程序中用 import 语句引入该类,并用该类创建对象。 import java.applet.Applet; import java.awt.*; public class ZuoYe2_1 extends Applet Button redbutton; public void init() redbutton=new Button(我是一个红色的按钮);redbutton.setBackground(Color.red); redbutton.setForeground(Color.white); add(redbutton); 15.编写一个类,该类有一个方法 Public int f(int a,int b) /要求该方法返回 a 和 b 的最大公约数 然后编写一个该类的子类,要求子类重写方法 f() ,而且重写的方法将返回两个整数的最小公倍数。要求: 在成功写的方法的方法体中首先调用被隐藏的方法返回 a 和 b 的最大公约数 m,然后将(a*b)/m 返回;在应 用的程序的主类中分别使用父类和子类创建对象,并分别调用方法 f()计算两个正整数的最大公约数和最小公倍 数。 import java.util.Scanner; class A public int f(int m,int n) if(mn) int temp=m; m=n; n=temp; int r=m%n; while(r!=0) m=n; n=r; r=m%n; return n; class B extends A public int f(int m,int n) int division=super.f(m,n); return (m*n)/division; public class ZuoYe2_1 public static void main (String args ) A a=new A(); B b=new B(); Scanner reader=new Scanner(System.in); System.out.println(输入2个整数,程序计算出它们的最大公约数和最小公倍数); System.out.print(输入第一个整数:); int m=reader.nextInt(); System.out.print(输入第二个整数:); int n=reader.nextInt(); if(m*n=160|age=0) throw new IntegerException(age); else this.age=age; public int getAge() System.out.println(年龄+age+合理); return age; public class ZuoYe2_1 public static void main(String args) People wang=new People(), zhang=new People(); trywang.setAge(189);System.out.println(wang.getAge(); catch(IntegerException e) System.out.println(e.toString(); tryzhang.setAge(28);System.out.println(zhang.getAge(); catch(IntegerException e)System.out.println(e.toString(); 20. 编写一个应用程序,用户从键盘输入一行字符串,程序输出该字符串中与模式”24680A135792”匹配的 子字符串。 import java.util.regex.*; import java.util.*; public class ZuoYe2_1 public static void main(String args ) Scanner reader=new Scanner(System.in); String s1=reader.nextLine(); Pattern p; Matcher m;p=Ppile(24680A135792); m=p.matcher(s1); while(m.find() String str=m.group(); System.out.print(从+m.start()+到+m.end()+匹配模式子序列:); System.out.println(str); 21.编写一个应用程序, 用户从键盘输入一行含有数字字符的字符串, 程序仅仅输出字符串中的全部数字字符。 import java.util.regex.*; import java.util.*; public class ZuoYe2_1 public static void main(String args ) Scanner reader=new Scanner(System.in); String s1=reader.nextLine(); Pattern p; Matcher m; p=Ppile(d+); m=p.matcher(s1); while(m.find() String str=m.group(); System.out.print(str); 22.设计5个人排队买票,并规定卖票规则和排队顺序。/设计如下: 一张电影票5元。卖票员有一张10元。买 票人:A-5元,B-5元,C-10元,D-10元,E-20元。 public class ZuoYe2_1 public static void main(String args) String s1=A,s2=B,s3=C, s4=D,s5=E; Cinema canema=new Cinema(s1,s2,s3,s4,s5); Thread a,b,c,d,e; a=new Thread(canema); b=new Thread(canema); c=new Thread(canema); d=new Thread(canema); e=new Thread(canema); a.setName(s1); b.setName(s2); c.setName(s3); d.setName(s4); e.setName(s5); a.start(); b.start(); c.start(); d.start(); e.start(); class Cinema implements Runnable TicketSeller seller; String name1,name2,name3,name4,name5; Cinema(String s1,String s2,String s3,String s4,String s5) seller=new TicketSeller(); name1=s1; name2=s2; name3=s3; name4=s4; name5=s5; public void run() if(Thread.currentThread().getName().equals(name1)seller.sellTicket(5); else if (Thread.currentThread().getName().equals(name2)seller.sellTicket(5); else if (Thread.currentThread().getName().equals(name3)seller.sellTicket(10); else if (Thread.currentThread().getName().equals(name4)seller.sellTicket(10); else if (Thread.currentThread().getName().equals(name5)seller.sellTicket(20); class TicketSeller int fiveNumber=0,tenNumber=1, twentyNumber=0; public synchronized void sellTicket(int receiveMoney) String s=Thread.currentThread().getName(); if(receiveMoney=5) fiveNumber=fiveNumber+1; System.out.println(s+给售票员5元钱,售票员卖给+s+一张票,不必找赎); else if(receiveMoney=10) while(fiveNumber1) try System.out.println(s+给售票员10元 钱); System.out.println(售票员请+s+靠边等一 会); wait(); System.out.println(s+结束等待,继续买 票); catch(InterruptedException e) fiveNumber=fiveNumber-1; tenNumber=tenNumber+1; System.out.println(s+给售票员10元钱,售票员卖给+s+一张票,找赎5元); else if(receiveMoney=20) while(fiveNumber1|tenNumber1) try System.out.println(s+给售票员20元钱); System.out.println(售票员请+s+靠边等一 会); wait(); System.out.println(s+结束等待,继续买 票); catch(InterruptedException e) fiveNumber=fiveNumber-1; tenNumber=tenNumber-1;twentyNumber=twentyNumber+1;System.out.println(s+给 售票员20元钱,售票员卖给+s+一张票,找赎15元); notifyAll(); 23.编写一个应用程序,读取一个文本文件的内容。 import java.io.*; import java.awt.*; import java.awt.event.*; public class ZuoYe2_1 public static void main(String args) int b; byte tom=new byte25; tryFile f=new File(Example.java); FileInputStream in= new FileInputStream(f); while(b=in.read(tom,0,25)!=-1) String s=new String (tom,0,b); System.out.print(s); in.close(); catch(IOException e) System.out.println(File read Error+e); 24.编写一个应用程序,将用户从键盘输入的 10 行文字存入文件。 import java.io.*; import java.util.*; public class ZuoYe2_1 public static void main(String args) Scannerreader=new Scanner(System.in); int b; try FileOutputStream writefile= new FileOutputStream(line.txt); int line=1,n=10; System.out.println(输入+n+行文本,并存入磁盘:); while(line=n) String s=reader.nextLine(); byte buffer=s.getBytes(); writefile.write(buffer,0,buffer.length); line+; writefile.close(); catch(IOException e) System.out.println(Error +e); 25.使用数组字符流将俄文字母写入内存,然后再从内存读出。 import java.io.*; public class ZuoYe2_1 public static void main(String args ) int n=-1; CharArrayWriter out= new CharArrayWriter(); for(char c=;c=;c+) out.write(c); CharArrayReader in= new CharArrayReader(out.toCharArray(); trywhile(n=in.read()!=-1) if(n%2=0) System.out.printf(n); System.out.printf(t位置%d,字符%c,n,(char)n); catch(IOException e) 26. 编写一个应用程序,将用户从键盘输入的 10 个整数存入文件,然后按顺序读出。 import java.io.*; import java.util.*; public class ZuoYe2_1 public static void main(String args) try FileOutputStream fos=new FileOutputStream(jerry.dat); DataOutputStream out_data= new DataOutputStream(fos); Scanner reader=new Scanner(System.in); for(int i=1;i=10;i+) int x=reader.nextInt(); out_data.writeInt(x); out_data.close(); catch(IOException e) try FileInputStream fis= new FileInputStream(jerry.dat); DataInputStream in_data= new DataInputStream(fis); for(int i=1;i=10;i+) int m=in_data.readInt(); System.out.print(+m); in_data.close(); catch(IOException e) 27.编写一个应用程序,要求将 LinkedList创建的对象写入到文件,然后读出一个 LinkedList对象,并 遍历 LinkedList节点中的数据。 import java.io.*; import java.util.*; class Studentimplements Serializable String name ; int number; Student(String name,int number) =name; this.number=number; public class ZuoYe2_1 publicstatic void main(String args) List list= new LinkedList(); List cloneList=null; for(int k=1;k=0) m=m-1; random.seek(m); int c=random.readByte(); if(c=0) System.out.print(char)c); else m=m-1;random.seek(m); byte cc=new byte2; random.readFully(cc); System.out.print(new String(cc); random.close(); catch(IOException ee) 29.编写一个应用程序,在应用程序中有一个按钮和一个文本框。当单击按钮时,文本框显示按钮的名字。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 public static void main(String args) MathWindow win=new MathWindow(); class MathWindow extends JFrame JTextField inputText; JButton button; MathWindow() inputText=new JTextField(10); button=new JButton(hello); button.addActionListener(new ActionListener() public void actionPerformed (ActionEvent e) inputText.setText(button.getText();); setLayout(new FlowLayout(); add(inputText); add(button); setBounds(100,100,260,190); setVisible(true); validate();setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 30.编写一个有两个文本框和一个按钮的应用程序,在一个文本框输入一个字符串按回车键或单击按钮,另一 个文本框都显示字符串中每个字符在 Unicode 表中的顺序位置。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 public static void main(String args) MathWindow win=new MathWindow(); class MathWindow extends JFrame implements ActionListener JTextField inputText,showUnicode; JButton button; MathWindow() inputText=new JTextField(10); showUnicode=new JTextField(10); button=new JButton(enter); button.addActionListener(this); inputText.addActionListener(this); setLayout(new FlowLayout(); add(inputText); add(button); add(showUnicode); setBounds(100,100,260,190); setVisible(true); validate(); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); public void actionPerformed(ActionEvent e) String s=inputText.getText(); StringBuffer str=new StringBuffer(); for(int i=0;is.length();i+) char c=s.charAt(i); str.append(int)c); str.append(,); showUnicode.setText(new String(str); 31.编写一个应用程序,设计 4 个按钮,分别命为: “加” 、 “差” 、 “乘” 、 “除” ;还有 3 个文本框,单击相应的 按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 public static void main(String args) ComputerFrame fr= new ComputerFrame(); fr.setTitle(计算); class ComputerFrame extends JFrame implements ActionListener JTextField text1,text2,text3; JButton button1,button2,button3,button4; JLabel label; public ComputerFrame() setLayout(new FlowLayout(); text1=new JTextField(10); text2=new JTextField(10); text3=new JTextField(10); label=new JLabel( ,JLabel.CENTER); label.setBackground(Color.green); add(text1); add(label); add(text2); add(text3); button1=new JButton(加); button2=new JButton(减); button3=new JButton(乘); button4=new JButton(除); add(button1); add(button2); add(button3); add(button4); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); setSize(300,320); setVisible(true); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); validate(); public void actionPerformed(ActionEvent e) double n; if(e.getSource()=button1) double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1+n2; text3.setText(String.valueOf(n); label.setText(+); catch(NumberFormatException ee) text3.setText(请输入数字字符); else if(e.getSource()=button2) double n1,n2; tryn1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1-n2; text3.setText(String.valueOf(n); label.setText(-); catch(NumberFormatException ee) text3.setText(请输入数字字符); else if(e.getSource()=button3) double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1*n2; text3.setText(String.valueOf(n); label.setText(*); catch(NumberFormatException ee) text3.setText(请输入数字字符); else if(e.getSource()=button4) double n1,n2; try n1=Double.parseDouble(text1.getText(); n2=Double.parseDouble(text2.getText(); n=n1/n2; text3.setText(String.valueOf(n); label.setText(/); catch(NumberFormatException ee) text3.setText(请输入数字字符); validate(); 32.编写一个应用程序,要求有一个含有菜单的窗口,窗口中有文本区组件。菜单有“打开文件”的菜单项, 当单击该菜单项时,使用输入流将一个名字为“hello.txt”文件的内容读入到文本中。 import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.io.*; public class ZuoYe2_1 public static void main(String args) new ReadFileWindow(); class ReadFileWindow extends JFrame implements ActionListener JMenuBar menubar; JMenu menu; JMenuItem openFile; JTextArea showText; ReadFileWindow() menubar=new JMenuBar(); menu=new JMenu(文件); openFile=new JMenuItem(打开文件); menu.add(openFile); menubar.add(menu); setJMenuBar(menubar); showText=new JTextArea(12,12); add(new JScrollPane(showText); validate(); openFile.addActionListener(this); setBounds(120,120,500,370); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); public void actionPerformed(ActionEvent e) String fileName=hello.txt; File readFile=new File(fileName); showText.setText(null); tryFileReader inOne= new FileReader(readFile); BufferedReader inTwo= new BufferedReader(inOne); String s=null; int i=0;while(s=inTwo.readLine()!=null) showText.append(n+s); inOne.close(); inTwo.close(); catch(IOException ex) showText.setText(ex.toString(); 33.编写有两个文本区的应用程序。当我们在一个文本区中输入若干个数时,另一个文本区同时对输入的数进 行求和运算并求出平均值,也就是说随着输入的变化,另一个文本区不断地更新求和及平均值。 34.编写一个应用程序,有8个按钮,用户通过按动键盘上的方向键移动这些按钮。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ZuoYe2_1 public static void main(String args) Win win=new Win(); class Win extends JFrame implements KeyListener JButton b=new JButton8; int x,y; Win() setLayout(new FlowLayout(); for(int i=0;i8;i+) bi=new JButton(+i); bi.addKeyListener(this); add(bi); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0);); setBounds(10,10,300,300); setVisible(true); validate(); public void keyPressed(KeyEvent e) int moveDistance=1; Component com=(Component)e.getSource(); int x=(int)com.getBounds().x; int y=(int)com.getBounds().y; if(e.getKeyCode()=KeyEvent.VK_UP) y=y-moveDistance; com.setLocation(x,y); Rectangle comRect=com.getBounds(); for(int k=0;kb.length;k+) Rectangle orthRect=bk.getBounds(); if(comRersects(orthRect) com.setLocation(x,y); break; if(y=0) y=10; else if(e.

温馨提示

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

评论

0/150

提交评论