2014JAVA复习题.doc_第1页
2014JAVA复习题.doc_第2页
2014JAVA复习题.doc_第3页
2014JAVA复习题.doc_第4页
2014JAVA复习题.doc_第5页
已阅读5页,还剩11页未读, 继续免费阅读

下载本文档

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

文档简介

一、选择题1. Java语言的各种分隔符中,非法的是()A. 空白符B. 分号C. 逗号D. 问号2. 下列表达式中,非法的是()Int a=5,b=6;double c=1.1,d=2.2;A. a+c+dB. (a+c)-C. C0)System.out.println(older than +); else if(d=0)System.out.println(same as +); else System.out.println(younger than +); public void print() System.out.println(The object of +this.getClass().getName()+:+name+,+age); /this.getClass().getName()返回对象所属类的名字 class Student2 extends Per String address;String department;public Student2(String na ,int ag,String ad,String de) /用super调用超类的构造方法address = ad;department = de; public void print() super.print(); System.out.println(The other information of student: +address+,+department); public class Yuedu public static void main(String args ) Person2 pe = /用name=Tom,age=20实例化对象pe;Student2 st = new Student2(John,19,336 West Street,Computer);pe.print();st.print();pe.olderoryounger(st); 答案1. /用构造函数为成员属性赋初值public Person2(String na,int ag)name=na; age=ag;/为d赋值 d=this.age-p.age; /用super调用超类的构造方法super(na,ag);/用name=Tom,age=20实例化对象pe;new Per(Tom,20);2. 在java程序根目录下,有一个包含英文的文本文件“file.txt”,读取这个文件中的英文单词,并将文件中所有字符都转变为小写,然后写回到原文件中。import java.io.*;public class E5 public static void main(String args) StringBuffer buf=new StringBuffer();File f= /新建一个File类的对象,指向要读写的文本文件。 ; if( )System.out.println(文件不存在); return;try FileReader red= /新建一个从文件中读数据的FileReader对象int c;while( /从流中读数据 ) buf.append(char)c); red.close(); String s=buf.toString().toLowerCase(); FileWriter wri= /新建一个向文件中写数据的对象 ; wri.write(s); wri.close(); System.out.println(finish!); catch (IOException e) 答案2.: new File(file.txt) : !f.exists(): new FileReader(f) : (c=red.read()!=-1: new FileWriter(f)3. 利用文件输入流把Exa.txt里的内容写到屏幕上。import java.io.*;public class Example10_4 public static void main(String args) int b; /创建一个长度为25的字节数组tom try /创建一个以Exa.txt为文件的File对象 /创建一个FileInputStream类的对象in while(b= )!=-1) /读文件中的内容保存到tom数组中 String s=new String (tom,0,b); System.out.print(s); in.close(); catch(IOException e) System.out.println(File read Error+e); 答案3. : byte tom=new byte25; :File f=new File(Exa.txt);:FileInputStream in=new FileInputStream(f); :in.read(tom,0,25)4. 实现一个求矩形周长和面积的程序,其中给出了父类和接口,要求完善程序,写出子类和接口实现类:Square,并要求打印结果为:结果: The Area of Square(3,4) is 12.0The Perimenter of Square(3,4) is 14.0(1空:10分,2空:5分,3空:5分,共20分)interface Shapesdouble getArea();double getPerimenter();class Fatherpublic int weight,height;public Father(int x,int y )weight=x;height=y;public class T public static void main(String args) Square sq=new Square(3,4);System.out.println(The Area of Square(3,4) is + ;System.out.println(The Perimenter of Square(3,4) is + );答案 public class Square extends Father implements Shapes-4分public Square (int x, int y)super(x,y);-2分public double getArea() return this.weight*this.height;-2分public double getPerimenter() return 2*(this.height+this.weight);-2分 sq.getArea() sq.getPerimenter()5. 实现显示C:java目录下所有文件的名称(1空:5分,2空:5分,共10分)import java.io.File;public class F public static void main(String args)String dirname = c:java;File f1= new File(dirname);if(f1.isDirectory()String s = ;for(int i = 0; is.length; i+)File f = new File(dirname+si);if( )System.out.println(si);elseSystem.out.println(dirname+ is not a directory.);答案 f1.list(); f.isFile()6. 功能描述:本程序点击按钮实现求两个操作数的加减乘除操作。(1空:5分,2空:5分,3空:10分,共20分)import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JTextField;public class GUI extends JFrame implements ActionListener JTextField tOprand1,tOprand2,result;JComboBox op;JButton compute;public GUI()tOprand1=new JTextField(,10);tOprand2=new JTextField(,10);result=new JTextField(,10);compute = new JButton(计算);op = new JComboBox();op.addItem(+);op.addItem(-);op.addItem(*);op.addItem(/); 设置布局管理器为流式布局 this.add(tOprand1);this.add(op);this.add(tOprand2);this.add(compute);this.add(result); 设置注册监听器 this.pack();this.setSize(500,300);this.setVisible(true); this.addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e) System.exit(0);); public void actionPerformed(ActionEvent e) public static void main(String args) GUI mainFrame = new GUI();答案 this.setLayout(new FlowLayout();compute.addActionListener(this);int operator = op.getSelectedIndex();-2分double ret;double op1 = Double.parseDouble(tOprand1.getText();double op2 = Double.parseDouble(tOprand2.getText();-2分if(operator=0)ret = op1 + op2;else if(operator=1)ret = op1 - op2;else if(operator=2)ret = op1 * op2;else ret = op1/op2;-4分result.setText(ret+);-2分7. 编写一个应用程序,实现如下界面,要求在文本框中输入有关信息,单击OK按钮,在下面的文本域中显示信息;单击Clear按钮,将文本框中的数据清除。(1空:5分,2空:5分,共10分)import java.awt.*;import java.awt.event.*;import javax.swing.*;public class InputDemo extends JFrame implements ActionListener JButton jbtn1 = new JButton(OK), jbtn2 = new JButton(Cancel); JTextArea jta =new JTextArea(); JScrollPane jsp = new JScrollPane(jta); JTextField jtf1 = new JTextField(20), jtf2 = new JTextField(20), jtf3 = new JTextField(20); JPanel jp = new JPanel(), jp1 = new JPanel(), jp2 = new JPanel(), jp3 = new JPanel(), jp4 = new JPanel(); public InputDemo() jp1.add(new JLabel(姓名);jp1.add(jtf1); jp2.add(new JLabel(性别);jp2.add(jtf2); jp3.add(new JLabel(年龄);jp3.add(jtf3); jp4.setLayout(new FlowLayout(FlowLayout.CENTER); jp4.add(jbtn1);jp4.add(jbtn2); jp.setLayout(new GridLayout(4,1); jp.add(jp1);jp.add(jp2);jp.add(jp3);jp.add(jp4); add(jp,BorderLayout.NORTH); add(jsp,BorderLayout.CENTER); setTitle(Input Demo); setSize(300,250); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public void actionPerformed(ActionEvent e) String s = ;if(e.getSource() = jbtn1 ) s = s+ jtf1.getText()+n; s = s+ jtf2.getText()+n; s = s+ jtf3.getText()+n; else if(e.getSource() = jbtn2) jtf1.setText(); jtf2.setText(); jtf3.setText(); jta.setText(); public static void main(String args) InputDemo id = new InputDemo(); 答案. jbtn1.addActionListener(this); jta.setText(s);三、阅读程序写结果1. public class Yuedu1 public static void main(String args) int x,i; System.out.println(x1=+add(3,7); System.out.println(x2=+add(3,add(7,2);static int add(int x,int y)return x+y;1. 结果:x1=10x2=122. class leve1String name=;int height,width;public leve1();public leve1(leve1 le)name=;height=le.height;width=le.width;public leve1(String na,int h,int w)name=na;height=h;width=w;public int higherthen(leve1 t)return this.height-t.height;public void print()System.out.println(class leve1:);System.out.println(Name=:+name+,Height=:+height+,Width=:+width);class leve2 extends leve1 int width; public leve2(); public leve2(leve1 le,int h,int w) super(le); height=h; width=w; public int widerthen() return width-super.width; public void print() System.out.println(class leve2:); System.out.println(Name=:+name+,Height=:+height+,Width=:+width); public class lev_expublic static void main(String args) leve1 le1=new leve1(Tower_1,50,20);leve2 le2=new leve2(le1,40,25);le1.print();le2.print();System.out.println(leve1 is +le1.higherthen(le2)+ highter then leve2.);System.out.println(leve2 is +le2.widerthen()+ wider then leve1.); 2. 结果:class leve1:Name=:Tower_1,Height=:50,Width=:20class leve2:Name=:Tower_1,Height=:40,Width=:25leve1 is 10 highter then leve2.leve2 is 5 wider then leve1.3. import java.io.*;public class NumberEcho public static void main(String args)tryBufferedReader buffin=new BufferedReader(new InputStreamReader(System.in);System.out.print(请输入整数串:);String str=buffin.readLine();int i = Integer.parseInt(str);System.out.println(运算结果是:+(i*i+i);System.out.println(运算结果是:+i*i+i);catch(IOException e)System.out.println(Input error!);(假定用键盘输入整数串3)3. 结果为:请输入整数串:3运算结果是:12运算结果是:934. public class Yuedu public static void main(String args) System.out.println(fun(4); static int fun(int n)int t;if(n=0)|n=1)t=3;elset=n*fun(n-1);return

温馨提示

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

最新文档

评论

0/150

提交评论