Java复习指南.doc_第1页
Java复习指南.doc_第2页
Java复习指南.doc_第3页
Java复习指南.doc_第4页
Java复习指南.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

复习指南:复习重点:前13章 和22章出题思路:围绕吴老师理论课所讲内容出题,以基本概念的掌握为出题思路, 考试题型:1、选择题,14题 共21分(基本概念)2、填空题 14空 共21分(围绕基本概念PPT)3、简答题复习重点:(20分,4题)1) 什么是多态,java中如何实现多态2) 什么叫容器布局,布局都有哪些3)请从构造器的引入、构造器的名字和构造器的种类及构造器的特点进行介绍。初始化,与类名相同,带参数和不带参数(默认构造器),4)请解释初始化的第一原则和第二原则。第一原则:可以用构造器来进行初始化 但执行顺序是:先自动初始化,再指定 初始化,最后调用构造器进行初始化第二原则:初始化的顺序是先静态对象,而后是非静态对象5)this和super关键字指向对象本身的引用,通过this关键字让成员函数知道被那里对象调用,即在类的内部代表对象本身 构造器调用构造器使用super关键字来实现子类中需要调用父类的被覆盖的方法用于子类构造器调用父类构造器6)简述java的授权事件模型7)java中重载与覆盖的区别 重载的定义: 类对自身已有的同名方法的重新定义本质:利用不同的参数列表,包括形式参数的个数、类型、顺序的不同来区分重载覆盖的定义:子类对继承自父类方法的重新定义本质:是指在子类中定义了这样的一个方法: 该方法具有与父类完全相同的返回值 类型、方法名和参数列表(但是重新 编写了方法体)关键:发生的地点不同 重载发生在同一个类中 覆盖发生在基类与派生类中8)什么是匿名内部类及其使用原则。匿名内部类就是没有名字的内部类,原则:1、匿名内部类不能自行创建构造函数,2匿名内部类一定跟在new的后面,用以隐含实现一个接口或继承一个类 3、匿名内部类不能定义任何静态的成员、方法 4、因匿名内部类为内部类,所以内部类的所有限制都对其生效 9)设计和实现图形用户界面的工作主要有哪几方面?想好布局 安排好添加布局和控件的顺序 搭界面 创建组成界面的各个元素,指定它们的属性和位置关系,从而构成完整的GUI外观 写代码 定义好各个界面元素对不同事件的响应,从而实现GUI与用户的交互功能编程题的复习重点:(20分)1) 自己编写几个连续继承关系的自定义类,编写多个CATCH语句的代码来了解多CACTH语句的异常扑捉顺序/*自己编写几个连续继承关系的自定义类,编写多个CATCH语句的代码来了解多CACTH 语句的异常扑捉顺序*/此代码为师兄们所写,大家借鉴借鉴class Loferror extends Exception Loferror() super(Loferror);/调用Exception类的构造器并初始化class A public void f() throws Exception, Throwable throw new RuntimeException();class B extends A public void f() throws Exception, Throwable throw new Exception();public int rey() return 1 / 0;class C extends A public void f() throws Throwable throw new Throwable();public class Test1 public static void main(String args) B c1 = new B();try c1.rey();throw new Loferror(); catch (Loferror e) System.out.println(e); catch (ArithmeticException e) System.out.println(e); catch (RuntimeException e) System.out.println(e); catch (Exception e) System.out.println(e); catch (Throwable e) System.out.println(e); finally System.out.println(final);2) 创建一个Frame类,完成一个计算机的功能。package pack;import java.awt.*;import javax.swing.*;import javax.swing.border.*;import java.awt.event.*;public class Calculator implements ActionListener JFrame frame; JPanel panel; JTextField tfShow;/*定义显示文本框*/ JButton b1=new JButton10; /*数字按钮*/ JButton b2=new JButton6; /*操作按钮*/ boolean isNumber;/*判断是否输入多位数字的变量*/ double number;/*存储输入数值、显示结果的变量*/ double result;/*存储中间运算结果的变量*/ char operator;/*存储当前操作符的成员变量*/ public Calculator() frame=new JFrame(计算器); frame.setSize(300,300);/*指定框架窗口的大小*/ frame.setResizable(false);/*使框架窗口不可改变大小*/ JPanel contentPane=(JPanel)frame.getContentPane(); contentPane.setBorder(new EmptyBorder(20,20,20,20);/*绘制框架的指定大小的空透明边框*/ tfShow=new JTextField(0,25);/*指定属性的文本域*/ tfShow.setHorizontalAlignment(JTextField.RIGHT);/*设置文本域中文本的对齐方式*/ isNumber=true;/*初始值设置*/ number=0;/*初始值设置*/ result=0;/*初始值设置*/ operator= ;/*初始值设置*/ for(int i=0;ib1.length;i+) b1i=new JButton(Integer.toString(i);/*创建数字按钮*/ b1i.setActionCommand(Integer.toString(i); b1i.addActionListener(this); b1i.setForeground(Color.blue); String bs=/,*,-,C,+,=; for(int i=0;ib2.length;i+) b2i=new JButton(bsi);/*创建操作按钮*/ b2i.setActionCommand(bsi); b2i.addActionListener(this); b2i.setForeground(Color.red); panel=new JPanel(); panel.setLayout(new GridLayout(4,5); panel.add(b11); panel.add(b12); panel.add(b13); panel.add(b20); panel.add(b14); panel.add(b15); panel.add(b16); panel.add(b21); panel.add(b17); panel.add(b18); panel.add(b19); panel.add(b22); panel.add(b10); panel.add(b23); panel.add(b24); panel.add(b25); frame.add(tfShow,BorderLayout.NORTH);/*将文本框放置在框架上方*/ frame.add(panel,BorderLayout.CENTER);/*将装有按钮组的panel放在框架的中心*/ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/*设置框架窗口的默认窗口关闭操作*/ frame.setVisible(true);/*设置框架可见*/ public double getDisplay()/*返回要显示的结果*/ return number; public void reDisplay()/*刷新文本域的内容*/ tfShow.setText(+getDisplay(); /*对输入数字的处理*/ public void numberProcess(int num) if(isNumber&num!=0) String s1=Integer.toString(num); String s2=Integer.toString(int)(this.number); this.number=Double.parseDouble(s2+s1);/*对多位数字的处理*/ else this.number=num; isNumber=true;/*输入连续数字(即多位数字)时为真*/ public void operationProcess(char operator)/*根据输入的操作符改变当前操作符*/ switch(operator) case -: this.operator=-; break; case +: this.operator=+; break; case *: this.operator=*; break; case /: this.operator=/; break; result=number; isNumber=false;/*输入操作符时表示输入连续数字的标记变量为假*/ public void clear() number=0; result=0; public void equal()/*计算运算结果*/ switch(operator) case -: result=result-number; break; case +: result=result+number; break; case *: result=result*number; break; case /: result=result/number; break; case : result=number; break; number=result; /*把运算结果赋值给显示变量*/ isNumber=false; operator= ; public static void main(String args) Calculator cal=new Calculator();/*创建计算器*/ public void actionPerformed(ActionEvent e) String command=e.getActionCommand();/*获取按钮激发的操作事件的命令名称*/ char c=command.charAt(0);/*将按钮命令名称的第一个字符赋值给一个字符c*/ switch(c) case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 0: int number=Integer.parseInt(command); numberProcess(number);/*输入数字的处理*/ break; case +: case -: case *: case /: operationProcess(c);/*算数运算符的处理*/ break; case =:equal();break;/*计算运算结果*/ case C:clear();break;/*清零*/ reDisplay(); /*在文本域中显示信息*/ 3) 编写一个名为Outer的类,它包含一个名为Inter的类。在Outer中添加一个方法,它返回一个Inter类型的对象。在main( )中,创建并初始化一个指向某个Inter对象的引用。(p191-1)public class Outer1 class Inner Inner() System.out.println(Inner(); Outer1() System.out.println(Outer1(); / make an Inner from within a non-static method of outer class:Inner makeInner() return new Inner();public static void main(String args) Outer1 o = new Outer1();Inner i = o.makeInner();4)请编写一个Applet程序,该程序包含一个TextField对象,和一个TextArea对象,当用户在TextField对象中输入或修改文本时候,TextArea对象的文本区域中可以得到一个同步的拷贝。import java.applet.*; import java.awt.*; import java.awt.event.*; public class myApplet extends Applet implements ActionListener ,TextListener TextField tf; TextArea ta; public void init() tf = new TextField(45); ta = new TextArea (5,45); this.add(tf); this.add(ta); tf.addActionListener(this); tf.addTextListener(this); / 单行文本(TextEvent)改变引发的事件 public void textValueChanged(TextEvent e) if(e.getSource()=tf) ta.setText(TextField)e.getSource().getText(); /在单行文本按回车引发的时间 public void actionPerformed(ActionEvent e) if(e.getSource()= tf) ta.setText(); /回车,文本域清空 5)写一个方法,使用Iterator遍历Collection,并打印容器中每个对象的toString(),填充各种类型的Collection,然后对其使用此方法。import java.util.*;import static org.greggordon.tools.Print.*;public class Ex11 public static void printAny(Collection c) Iterator it = c.iterator();while(it.hasNext()print(it.next() + );println();public static void main(String args) ArrayList al =new ArrayList(Arrays.asList(1, 2, 3);LinkedList ll =new LinkedList(Arrays.asList(a, b, c);HashSet hs =new HashSet(Arrays.asList(1.1f, 2.2f, 3.3f);TreeSet ts =new TreeSet(Arrays.asList(1.11, 2.22, 3.33);LinkedHashSet lhs =new LinkedHashSet(Arrays.asList(11, 22, 33);printAny(al);printAny(ll);printAny(hs);printAny(ts);printAny(lhs);6)编写应用程序,它包含一个文本域和三个按钮,单击每个按钮的时候,在文本域中显示不同的文字。import javax.swing.JPanel;import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.JTextField;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Test extends JFrame implements ActionListener private static final long serialVersionUID = 1L; private JPanel jPanel; private JButton jButton1,jButton2,jButton3; private JTextField tf1; public Test(String title) super(title); init(); private void init() jPanel=new JPanel(); jPanel.setLayout(new FlowLayout(); tf1=new JTextField(20); jButton1=new JButton(按钮1); jButton2=new JButton(按钮2); jButton3=new JButton(按钮3); jButton1.addActionListener(this); jButton2.addActionListener(this); jButton3.addActionListener(this); jPanel.add(tf1); jPanel.add(jButton1); jPanel.add(jButton2); jPanel.add(jButton3); this.add(jPanel); this.setSize(300,100); this.setResizable(false); this.setVisible(true); this.addWindowListener(new WindowAdapter() public void windowClosing(final WindowEvent e) System.exit(0); ); public void actionPerformed(ActionEvent e) if(e.getSource().equals(jButton1) tf1.setText(按钮1); if(e.getSource().equals(jButton2) tf1.setText(按钮2); if(e.getSource().equals(jButton3) tf1.setText(按钮3); public static void main(String args) new Test(文字变换); 请读懂以下程序并给出运行结果:(18分)/程序1public class Guess public static void main(String args) int a,b; for(a=14;a=90;a+) for(b=11;b50;b+) if(a-10)=(b-10)*4)&(a+10)%(b+10)=0) System.out.println(ag +a+ daf+b); /程序2class OverrideDemo void test() System.out.println( No parameters ); void test(int a) System.out.println(a: + a); void test(int a,int b) System.out.println(a and b: + a + + b); void test(double a) System.out.println(double a: + a);class Override public static void main(String args) OverrideDemo ob = new OverrideDemo(); ob.test(); ob.test(10); ob.test(10,20); ob.test(123.25); /程序3import java.awt.*;import java.awt.event.*;import java.applet.*;public class ButtonApplet extends Applet implements ActionListener int i1,i2; Button btn1,btn2; public void init() i1=0; i2=0; btn1=new Button(确定); btn2=new Button(取消); add(btn1); add(btn2); btn1.addActionListener(this); btn2.addActionListener(this); public void paint(Graphics g) g.setColor(Color.blue); g.drawString(你点击了确定+ i1+次,20,100); g.setColor(Color.red); g.drawString(你点击了确定+ i2+次,20,120); public void actionPerformed(ActionEvent e) if(e.getSource()=btn1) i1+; if(e.getSource()=btn2) i2+; repaint(); /程序4interface WeaponString name = weapon;void attack();class Tank implements Weaponpublic void attack()System.out.println(Tank attack);class Cannon implements Weaponpublic void attack()System.out.println(Cannon attack);class testWeaponpublic static void main(String args)Weapon wp; wp = new Tank();wp.attack();wp = new Cannon();wp.attack();/程序5import java.awt.*;import javax.swing.*;import java.util.Date;public class DrawLines extends JFrame public void paint(Graphics g) int x1,y1,x2,y2; for(int i=0;i100;i+) x1=(int)(Math.random()*400); x2=(int)(Math.random()*400); y1=(int)(Math.random()*400); y2=(int)(Math.random()*400); g.setColor(new Color(int)(Math.random()*256), (int)(Math.random()*256),(int)(Math.random()*256); g.drawLine(x1, y1, x2, y2); public static void main(String args) DrawLines window=new DrawLines();window.setSize(400, 400);window.setVisible(true);window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); / window.repaint(); /程序6:class Person1protected String name;protected double classsum;public Person1()=;public Person1(String name)=name;class Teacher1 extends Person1 implements PersonPayprivate final int baseWage=800;private double classsum;public Teacher1(String name,double classsum)super(name);this.classsum=classsum;public double pay()return baseWage+classsum*30;public String toString()return 姓名:++t 工资支出:+this.pay()+n;class CollegeStudent1 extends Person1 implements PersonPayprivate double scholarship;public CollegeStudent1(String name,double scholarship)super(name);this.scholarship=scholarship;public double pay()return scholarship;public String toString()return 姓名:++t工资支出:+this.pay()+n;public class WageTest1 public static void main(String args) Person1 ps=new Person12;ps0=new Teacher1(张三,120);ps1=new CollegeStudent1(李四,500);String output=;for(int i=0;ips.length;i+)output+=psi;System.out.println(output);/程序7public class ko public static void main(String args) int t, z=10; t=sum(z); System.out.println(sum=+t); static int sum(int x) if(x=1) return(1); else return(sum(x-1)+x);/程序8public class TextComponetEvent extends Applet implements TextListener,ActionListener TextField tf; TextArea ta; public void init() tf=new TextField(45); ta=new TextArea(5,45); add(tf); add(ta); tf.addActionListener(this); tf.addTextListener(this); public void textValueChanged(TextEvent e) if(e.getSource()=tf) ta.setText(TextField)e.getSource().getText(); public void actionPerformed(ActionEvent e) if(e.getSource()=tf) ta.setText(); /程序9import java.io.*;class Treeint height;Tree()System.out.println(Planting a seedling);height=0;Tree(int initialHeight)height=initialHeight;System.out.println(Creating new Tree that is+height+feet tall);void info()Syst

温馨提示

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

评论

0/150

提交评论