1、JAVA期末考试整理资料JAVA期末整理资料1、import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Test extends JFrame int x1,y1,x2,y2;public Test()setVisible(true);setSize(300,300) ;addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)System.exit(0) ; );addMouseListener(ne
2、w MouseAdapter() public void mousePressed(MouseEvent e)x1=e.getX();y1=e.getY(); );addMouseMotionListener(new MouseMotionAdapter() public void mouseDragged(MouseEvent e)x2=e.getX() ;y2=e.getY() ;repaint(); );public void paint(Graphics g)g.drawLine(x1,y1,x2,y2);x1=x2;y1=y2;public static void main(Stri
3、ng args)new Test();2、class Transport int speed,load; public Transport(); public Transport(int speed,int load) this.speed = speed; this.load = load; public static void main(String args) class vehicle extends Transport int wheels,weight; public vehicle(); public vehicle(int speed,int load,int wheels,i
4、nt weight) super(speed,load); this.wheels = wheels; this.weight = weight; class Airplane extends Transport int engintype,enginnumber; public Airplane(); public Airplane(int speed,int load,int engintype,int enginnumber) super(speed,load); this.engintype = engintype; this.enginnumber = enginnumber; 3、
5、class TestRead public static void main(Stringargs) int x=0; try x=System.in.read(); catch(Exception e) while(x!=q) System.out.println(char)x); try x=System.in.read(); catch(Exception e) 4、class AA public void exchange( int a,intarray) a=50; array1=30; class testAA public static void main(String args
6、) int aa=3; int array=1,2,3; AA a=new AA(); a.exchange(aa,array); System.out.println(aa); System.out.println(array1); 5、public class Person public String name; public int age; public String addr; Person () public Person(String name,int age) this.name=name; this.age=age; public Person(String name,int
7、 age,String addr) this(name,age); this.addr=addr; public void getInfo() System.out.println(name); System.out.println(age); class Student extends Person public int grade; public student() public Student(String name,int age,int grade) super(name,age); this.grade=grade; public void getInfo() super.getI
8、nfo(); System.out.println(grade); public static void main(Srtingargs) Student s=new Student(student,16,2); s.getInfo(); 6、interface StudentManageInterface double money = 0; void setFee(double money); double getFee();interface TeacherManageInterface double tmoney = 0; void setPay(double tmoney); doub
9、le getPay();class Graduate implements StudentManageInterface,TeacherManageInterface String name,sex; int age; double fee,pay; public Graduate(); public Graduate(String name,String sex,int age,double fee,double pay) setPay(pay); setFee(fee); public void setPay(double pay) this.pay=pay; public double
10、getPay() return pay; public void setFee(double fee) this.fee=fee; public double getFee() return fee; public class Question7 public static void main(String agrs) Graduate g1 = new Graduate(zhangsan,男,17,12,1500); if(g1.getFee()*12 - g1.getPay()2000 ) System.out.println(provide a loan); 7、package test
11、1;import java.awt.*;import java.awt.event.*;public class Integration implements TextListener,ActionListener Frame f; TextField tfDividend,tfDivisor; Label lb; Button btCount,btClean; public static void main(String args) new Integration(); public Integration() f=new Frame(Integration); f.setLayout(ne
12、w GridLayout(4,1); tfDividend=new TextField(); tfDividend.addTextListener(this);/textValueChanged(TextEvent e) tfDivisor=new TextField(); tfDivisor.addTextListener(this); lb=new Label(); btCount=new Button(除); btCount.setActionCommand(btCount); btCount.setBackground(Color.blue); btCount.addActionLis
13、tener(this);/actionPerformed(ActionListener e) btClean=new Button(清除); btClean.setActionCommand(btClean); btClean.setForeground(Color.red); btClean.addActionListener(this); Panel p=new Panel(new GridLayout(1,2); p.add(btCount); p.add(btClean); f.add(tfDividend); f.add(tfDivisor); f.add(lb); /f.add(b
14、tCount); /f.add(btClean); f.add(p); /f.addWindowListener(this); f.addWindowListener( new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); f.pack(); f.setVisible(true); public void textValueChanged(TextEvent e) lb.setText(tfDividend.getText(); public void actionPerformed(Ac
15、tionEvent e) String cmd=e.getActionCommand(); if(cmd.equals(btCount) lb.setText(Double.parseDouble(tfDividend.getText()+ / +Double.parseDouble(tfDivisor.getText()+=+divide(Double.parseDouble(tfDividend.getText(),Double.parseDouble(tfDivisor.getText(); else if(cmd.equals(btClean) tfDividend.setText()
16、; tfDivisor.setText(); lb.setText(); public double divide(double x,double y) return x/y; 8、package test1;import java.awt.*;import java.awt.event.*;public class Integration implements TextListener,ActionListener Frame f; TextField tfDividend,tfDivisor; Label lb; Button btCount,btClean; public static
17、void main(String args) new Integration(); public Integration() f=new Frame(Integration); f.setLayout(new GridLayout(4,1); tfDividend=new TextField(); tfDividend.addTextListener(this);/textValueChanged(TextEvent e) tfDivisor=new TextField(); tfDivisor.addTextListener(this); lb=new Label(); btCount=ne
18、w Button(除); btCount.setActionCommand(btCount); btCount.setBackground(Color.blue); btCount.addActionListener(this);/actionPerformed(ActionListener e) btClean=new Button(清除); btClean.setActionCommand(btClean); btClean.setForeground(Color.red); btClean.addActionListener(this); Panel p=new Panel(new Gr
19、idLayout(1,2); p.add(btCount); p.add(btClean); f.add(tfDividend); f.add(tfDivisor); f.add(lb); /f.add(btCount); /f.add(btClean); f.add(p); /f.addWindowListener(this); f.addWindowListener( new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); f.pack(); f.setVisible(true); pu
20、blic void textValueChanged(TextEvent e) lb.setText(tfDividend.getText(); public void actionPerformed(ActionEvent e) String cmd=e.getActionCommand(); if(cmd.equals(btCount) lb.setText(Double.parseDouble(tfDividend.getText()+ / +Double.parseDouble(tfDivisor.getText()+=+divide(Double.parseDouble(tfDivi
21、dend.getText(),Double.parseDouble(tfDivisor.getText(); else if(cmd.equals(btClean) tfDividend.setText(); tfDivisor.setText(); lb.setText(); public double divide(double x,double y) return x/y; 9、import java.awt.*;import java.awt.event.*;public class FBL implements ActionListener Frame f; Button b1,b2
22、; Label lb; public FBL() f=new Frame(FBL); f.setLayout(new GridLayout(3,1); b1=new Button(b1); b2=new Button(b2); b1.addActionListener(this); b2.addActionListener(this); lb=new Label(); f.add(b1); f.add(b2); f.add(lb); f.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e)
23、System.exit(0); ); f.pack(); f.setVisible(true); public void actionPerformed(ActionEvent e) Button b=(Button)e.getSource(); lb.setText(b.getLabel(); public static void main(String args) new FBL(); 10、import java.io.*;public class FileRuntimeTest public static void main(String args) File f=new File(a
24、rgs1); if(f.exists() try Process p=null; p=Runtime.getRuntime().exec(args); Thread.sleep(5000); p.destroy(); catch(Exception e) e.printStackTrace(); else try f.createNewFile(); catch(Exception e) System.out.println(e.getMessage(); 11、import java.awt.*;import java.awt.event.*;import java.io.*;public
25、class MyNotepad extends WindowAdapter implements ActionListener Frame f; Button btn; TextArea ta; String filename; public static void main(String args) new MyNotepad(args0); public MyNotepad(String filename) this.filename=filename; f=new Frame(filename); f.addWindowListener(this); btn=new Button(sav
26、e); btn.addActionListener(this); ta=new TextArea(10,40); f.add(ta,BorderLayout.CENTER); f.add(btn,BorderLayout.SOUTH); f.pack(); f.setVisible(true); public void actionPerformed(ActionEvent e) try FileWriter fout=new FileWriter(filename,false); fout.write(ta.getText(); fout.close(); catch(IOException
27、 ioe) System.err.println(ioe); public void windowClosing(WindowEvent e) System.exit(0); 12、import java.awt.*;import java.awt.event.*;import java.io.*;public class MyNotepad extends WindowAdapter implements ActionListener Frame f; Button btn; TextArea ta; String filename; public static void main(String args) new MyNotepad(args0); public MyNotepad(String filename) this.filename=filename; f=new Frame(filename); f.addWindowListener(this); btn=n