JAVA期末考试整理资料.docx
《JAVA期末考试整理资料.docx》由会员分享,可在线阅读,更多相关《JAVA期末考试整理资料.docx(41页珍藏版)》请在冰点文库上搜索。
JAVA期末考试整理资料
JAVA期末整理资料
1、
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassTestextendsJFrame{
intx1,y1,x2,y2;publicTest(){
setVisible(true);
setSize(300,300);
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);}});
addMouseListener(
newMouseAdapter(){
publicvoidmousePressed(MouseEvente){
x1=e.getX();
y1=e.getY();}});
addMouseMotionListener(newMouseMotionAdapter(){
publicvoidmouseDragged(MouseEvente){
x2=e.getX();
y2=e.getY();
repaint();}});
}
publicvoidpaint(Graphicsg)
{
g.drawLine(x1,y1,x2,y2);
x1=x2;
y1=y2;
}
publicstaticvoidmain(Stringargs[])
{
newTest();
}
}
2、classTransport
{
intspeed,load;
publicTransport(){};
publicTransport(intspeed,intload)
{
this.speed=speed;
this.load=load;
}
publicstaticvoidmain(String[]args)
{
}
}
classvehicleextendsTransport
{
intwheels,weight;
publicvehicle(){};
publicvehicle(intspeed,intload,intwheels,intweight)
{
super(speed,load);
this.wheels=wheels;
this.weight=weight;
}
}
classAirplaneextendsTransport
{
intengintype,enginnumber;
publicAirplane(){};
publicAirplane(intspeed,intload,intengintype,intenginnumber)
{
super(speed,load);
this.engintype=engintype;
this.enginnumber=enginnumber;
}
}
3、classTestRead{
publicstaticvoidmain(String[]args){
intx=0;
try{
x=System.in.read();
}catch(Exceptione){}
while(x!
='q'){
System.out.println((char)x);
try{
x=System.in.read();
}catch(Exceptione){}
}
}
}
4、classAA
{
publicvoidexchange(inta,int[]array)
{
a=50;
array[1]=30;
}
}
classtestAA
{
publicstaticvoidmain(String[]args)
{
intaa=3;
int[]array={1,2,3};
AAa=newAA();
a.exchange(aa,array);
System.out.println(aa);
System.out.println(array[1]);
}
}
5、publicclassPerson{
publicStringname;
publicintage;
publicStringaddr;
Person(){}
publicPerson(Stringname,intage){
this.name=name;
this.age=age;
}
publicPerson(Stringname,intage,Stringaddr){
this(name,age);
this.addr=addr;
}
publicvoidgetInfo(){
System.out.println(name);
System.out.println(age);
}
}
classStudentextendsPerson{
publicintgrade;
publicstudent(){}
publicStudent(Stringname,intage,intgrade){
super(name,age);
this.grade=grade;
}
publicvoidgetInfo(){
super.getInfo();
System.out.println(grade);
}
publicstaticvoidmain(Srting[]args){
Students=newStudent("student",16,2);
s.getInfo();
}
}
6、interfaceStudentManageInterface
{
doublemoney=0;
voidsetFee(doublemoney);
doublegetFee();
}
interfaceTeacherManageInterface
{
doubletmoney=0;
voidsetPay(doubletmoney);
doublegetPay();
}
classGraduateimplementsStudentManageInterface,TeacherManageInterface
{
Stringname,sex;
intage;
doublefee,pay;
publicGraduate(){};
publicGraduate(Stringname,Stringsex,intage,doublefee,doublepay)
{
setPay(pay);
setFee(fee);
}
publicvoidsetPay(doublepay){
this.pay=pay;
}
publicdoublegetPay(){
returnpay;
}
publicvoidsetFee(doublefee){
this.fee=fee;
}
publicdoublegetFee(){
returnfee;
}
}
publicclassQuestion7{
publicstaticvoidmain(String[]agrs)
{
Graduateg1=newGraduate("zhangsan","男",17,12,1500);
if(g1.getFee()*12-g1.getPay()<2000)
{
System.out.println("providealoan");
}
}
}
7、packagetest1;
importjava.awt.*;
importjava.awt.event.*;
publicclassIntegrationimplementsTextListener,ActionListener{
Framef;
TextFieldtfDividend,tfDivisor;
Labellb;
ButtonbtCount,btClean;
publicstaticvoidmain(String[]args){
newIntegration();
}
publicIntegration(){
f=newFrame("Integration");
f.setLayout(newGridLayout(4,1));
tfDividend=newTextField();
tfDividend.addTextListener(this);//textValueChanged(TextEvente)
tfDivisor=newTextField();
tfDivisor.addTextListener(this);
lb=newLabel();
btCount=newButton("除");
btCount.setActionCommand("btCount");
btCount.setBackground(Color.blue);
btCount.addActionListener(this);//actionPerformed(ActionListenere)
btClean=newButton("清除");
btClean.setActionCommand("btClean");
btClean.setForeground(Color.red);
btClean.addActionListener(this);
Panelp=newPanel(newGridLayout(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(
newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
);
f.pack();
f.setVisible(true);
}
publicvoidtextValueChanged(TextEvente){
lb.setText(tfDividend.getText());
}
publicvoidactionPerformed(ActionEvente){
Stringcmd=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())));
elseif(cmd.equals("btClean")){
tfDividend.setText("");
tfDivisor.setText("");
lb.setText("");
}
}
publicdoubledivide(doublex,doubley){
returnx/y;
}
}
8、packagetest1;
importjava.awt.*;
importjava.awt.event.*;
publicclassIntegrationimplementsTextListener,ActionListener{
Framef;
TextFieldtfDividend,tfDivisor;
Labellb;
ButtonbtCount,btClean;
publicstaticvoidmain(String[]args){
newIntegration();
}
publicIntegration(){
f=newFrame("Integration");
f.setLayout(newGridLayout(4,1));
tfDividend=newTextField();
tfDividend.addTextListener(this);//textValueChanged(TextEvente)
tfDivisor=newTextField();
tfDivisor.addTextListener(this);
lb=newLabel();
btCount=newButton("除");
btCount.setActionCommand("btCount");
btCount.setBackground(Color.blue);
btCount.addActionListener(this);//actionPerformed(ActionListenere)
btClean=newButton("清除");
btClean.setActionCommand("btClean");
btClean.setForeground(Color.red);
btClean.addActionListener(this);
Panelp=newPanel(newGridLayout(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(
newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
);
f.pack();
f.setVisible(true);
}
publicvoidtextValueChanged(TextEvente){
lb.setText(tfDividend.getText());
}
publicvoidactionPerformed(ActionEvente){
Stringcmd=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())));
elseif(cmd.equals("btClean")){
tfDividend.setText("");
tfDivisor.setText("");
lb.setText("");
}
}
publicdoubledivide(doublex,doubley){
returnx/y;
}
}
9、importjava.awt.*;
importjava.awt.event.*;
publicclassFBLimplementsActionListener{
Framef;
Buttonb1,b2;
Labellb;
publicFBL(){
f=newFrame("FBL");
f.setLayout(newGridLayout(3,1));
b1=newButton("b1");
b2=newButton("b2");
b1.addActionListener(this);
b2.addActionListener(this);
lb=newLabel();
f.add(b1);
f.add(b2);
f.add(lb);
f.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
f.pack();
f.setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
Buttonb=(Button)e.getSource();
lb.setText(b.getLabel());
}
publicstaticvoidmain(String[]args){
newFBL();
}
}
10、importjava.io.*;
publicclassFileRuntimeTest
{
publicstaticvoidmain(String[]args)
{
Filef=newFile(args[1]);
if(f.exists())
try{
Processp=null;
p=Runtime.getRuntime().exec(args);
Thread.sleep(5000);
p.destroy();
}catch(Exceptione){
e.printStackTrace();
}
else
try
{
f.createNewFile();
}
catch(Exceptione)
{
System.out.println(e.getMessage());
}
}
}
11、importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
publicclassMyNotepadextendsWindowAdapterimplementsActionListener{
Framef;
Buttonbtn;
TextAreata;
Stringfilename;
publicstaticvoidmain(String[]args){
newMyNotepad(args[0]);
}
publicMyNotepad(Stringfilename){
this.filename=filename;
f=newFrame(filename);
f.addWindowListener(this);
btn=newButton("save");
btn.addActionListener(this);
ta=newTextArea(10,40);
f.add(ta,BorderLayout.CENTER);
f.add(btn,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
try{
FileWriterfout=newFileWriter(filename,false);
fout.write(ta.getText());
fout.close();
}
catch(IOExceptionioe){
System.err.println(ioe);
}
}
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
}
12、importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
publicclassMyNotepadextendsWindowAdapterimplementsActionListener{
Framef;
Buttonbtn;
TextAreata;
Stringfilename;
publicstaticvoidmain(String[]args){
newMyNotepad(args[0]);
}
publicMyNotepad(Stringfilename){
this.filename=filename;
f=newFrame(filename);
f.addWindowListener(this);
btn=n