上机实验报告源代码

class People

{ protected double weight,height;

public void speakHello()

{ System.out.println("yayawawa");

}

public void averageHeight()

{ height=173;

System.out.println("average height:"+height);

}

public void averageWeight()

{ weight=70;

System.out.println("average weight:"+weight);

}

}

class ChinaPeople extends People

{ public void speakHello()

{ System.out.println("你好,吃饭了吗?");

}

public void averageHeight()

{ height=173.0;

System.out.println("中国人的average height:"+height); }

public void averageWeight()

{ weight=67.34;

System.out.println("中国人的average weight:"+weight); }

public void chinaGongfu()

{ System.out.println("坐如钟,站如松,睡如弓"); }

}

class AmericanPeople extends People

{ public void speakHello()

{ System.out.println("How do you do");

}

public void averageHeight()

{ height=188.0;

System.out.println("American average height:"+height); }

public void averageWeight()

{ weight=80.23;

System.out.println("American average weight:"+weight); }

public void americanBoxing()

{ System.out.println("直拳,钩拳");

1

}

}

class BeijingPeople extends ChinaPeople

{ public void speakHello()

{ System.out.println("您好");

}

public void aversageHeight()

{ height=167.0;

System.out.println("北京人的average height:"+height); }

public void averageWeight()

{ weight=68.5;

System.out.println("北京人的average weight:"+weight); }

public void beijingOpera()

{ System.out.println("京剧术语");

}

}

public class Example

{ public static void main(String args[])

{ ChinaPeople chinaPeople=new ChinaPeople();

AmericanPeople americaPeople=new AmericanPeople(); BeijingPeople beijingPeople=new BeijingPeople();

chinaPeople.speakHello();

americanPeople.speakHello();

beijingPeople.speakHello();

americanPeople.averageHeight();

beijingPeople.averageHeight();

chinaPeople.averageWeight();

americanPeople.averageWeight();

beijingPeople.averageWeight();

chinaPeople.chinaGongfu();

americaPeople.americanBoxing();

beijingPeople.beijingOpera();

beijingPeople.chinaGongfu();

}

}

import java.awt.*;

import java.awt.event.*;

public class ComputerFrame extends Frame implements ActionListener { TextField textOne,textTwo,textResult;

2

Button getProblem,giveAnwser;

Label operatorLabel,message;

Teacher teacher;

ComputerFrame(String s)

{ super(s);

teacher=new Teacher();

setLayout(new FlowLayout());

textOne=new TextField(10);

textTwo=new TextField(10);

textResult=new TextField(10);

operatorLabel=new Label("+");

message=new Label("你还没有回答呢");

getProblem=new Button("获取题目");

giveAnwser=new Button("确认答案");

add(getProblem);

add(textOne);

add(operatorLabel);

add(textTwo);

add(new Label("="));

add(textResult);

add(giveAnwser);

add(message);

textResult.requestFocus();

textOne.setEditable(false);

textTwo.setEditable(false);

getProblem.addActionListener(this);

giveAnwser.addActionListener(this);

textResult.addActionListener(this);

setBounds(100,100,450,100);

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0);

}

}

);

}

public void actionPerformed(ActionEvent e)

{ if(e.getSource()==getProblem)

{ int number1=teacher.giveNumberOne(100);

int number2=teacher.giveNumberTwo(100);

String operator=teacher.giveOperator();

textOne.setText(""+number1);

3

textTwo.setText(""+number2);

operatorLabel.setText(operator);

message.setText("请回答");

textResult.setText(null);

}

if(e.getSource()==giveAnwser)

{ String answer=textResult.getText(); try{

int result=Integer.parseInt(answer); if(teacher.getRight(result)==true) { message.setText("你回答正确"); }

else

{ message.setText("你回答错误"); }

}

catch(NumberFormatException ex)

{ message.setText("请输入数字字符"); }

}

textResult.requestFocus();

validate();

}

}

public class Teacher

{ int numberOne,numberTwo;

String operator=" ";

boolean right;

public int giveNumberOne(int n)

{ numberOne=(int)(Math.random()*n)+1;

return numberOne;

}

public int giveNumberTwo(int n)

{ numberTwo=(int)(Math.random()*n)+1;

return numberTwo;

}

public String giveOperator()

{ double d=Math.random();

if(d>=0.5)

operator="+";

else

4

operator="-";

return operator;

}

public boolean getRight(int answer) { if(operator.equals("+"))

{ if(answer==numberOne+numberTwo) right=true;

else

right=false;

}

else if(operator.equals("-"))

{ if(answer==numberOne-numberTwo) right=true;

else

right=false;

}

return right;

}

}

public class MainClass

{ public static void main(String args[]) { ComputerFrame frame;

frame=new ComputerFrame("算术测试"); }

}

WordThread.java

import java.awt.*;

public class WordThread extends Thread { char word;

int k=19968;

Label com;

WordThread(Label com)

{ this.com=com;

}

public void run()

{ k=19968;

while(true)

{

word=(char)k;

com.setText(""+word);

try{ sleep(6000);

5

}

catch(InterruptedException e){}

k++;

if(k>=29968) k=19968;

}

}

}

ThreadFrame.java

import java.awt.*;

import java.awt.event.*;

public class ThreadFrame extends Frame implements ActionListener {

Label wordLabel;

Button button;

TextField inputText,scoreText;

WordThread giveWord;

int score=0;

ThreadFrame()

{ wordLabel=new Label(" ",Label.CENTER);

wordLabel.setFont(new Font("",Font.BOLD,72));

button=new Button("开始");

inputText=new TextField(3);

scoreText=new TextField(5);

scoreText.setEditable(false);

giveWord=new WordThread(wordLabel);

button.addActionListener(this);

inputText.addActionListener(this);

add(button,BorderLayout.NORTH);

add(wordLabel,BorderLayout.CENTER);

Panel southP=new Panel();

southP.add(new Label("输入标签所显示的汉字后回车:")); southP.add(inputText);

southP.add(scoreText);

add(southP,BorderLayout.SOUTH);

setBounds(100,100,350,180);

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0);

}

}

);

}

6

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==button)

{ if(!(giveWord.isAlive())) //giveWord调用方法isAlive() { giveWord=new WordThread(wordLabel);

}

try

{ giveWord.start(); }

catch(Exception exe){}

}

else if(e.getSource()==inputText)

{ if(inputText.getText().equals(wordLabel.getText()))

{ score++;

}

scoreText.setText("得分:"+score);

inputText.setText(null);

}

}

}

WordThread.java

public class ThreadWordMainClass

{ public static void main(String args[])

{ new ThreadFrame();

}

ReadZipFile.java

import java.io.*;

import java.util.zip.*;

public class ReadZipFile

{ public static void main(String args[])

{ File f=new File("book.zip");

File dir=new File("Book");

byte b[]=new byte[100];

dir.mkdir();

try

{ ZipInputStream in=new ZipInputStream(new FileInputStream(f)); ZipEntry zipEntry=null;

while((zipEntry=in.getNextEntry())!=null)

{ File file=new File(dir,zipEntry.getName());

FileOutputStream out=new FileOutputStream(file); int n=-1;

System.out.println(file.getAbsolutePath()+"的内容:"); 7

while((n=in.read(b,0,100))!=-1)

{ String str=new String(b,0,n);

System.out.println(str);

out.write(b,0,n);

}

out.close();

}

in.close();

}

catch(IOException ee)

{

System.out.println(ee);

}

}

}

ReadFile.java

import java.awt.*;

import java.awt.event.*;

import java.net.*;

import java.io.*;

public class ReadURLSource

{ public static void main(String args[])

{ new NetWin();

}

}

class NetWin extends Frame implements ActionListener,Runnable { Button button;

URL url;

TextField text;

TextArea area;

byte b[]=new byte[118];

Thread thread;

NetWin()

{ text=new TextField(20);

area=new TextArea(12,12);

button=new Button("确定");

button.addActionListener(this);

thread=new Thread(this);

Panel p=new Panel();

p.add(new Label("输入网址:"));

8

p.add(text);

p.add(button);

add(area,BorderLayout.CENTER);

add(p,BorderLayout.NORTH);

setBounds(60,60,360,300);

setVisible(true);

validate();

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0);

}

});

}

public void actionPerformed(ActionEvent e)

{

if(!(thread.isAlive()))

thread=new Thread(this);

try{

thread.start();

}

catch(Exception ee)

{ text.setText("我正在读取"+url);

}

}

public void run()

{ try { int n=-1;

area.setText(null);

String name=text.getText().trim();

url=new URL(name);

String hostName= url.getHost();

int urlPortNumber= url.getPort();

String fileName= url.getFile();

InputStream in= url.openStream();

area.append("\n主机:"+hostName+"端口:"+urlPortNumber+

"包含的文件名字:"+fileName);

area.append("\n文件的内容如下:");

while((n=in.read(b))!=-1)

{ String s=new String(b,0,n);

area.append(s);

}

}

catch(MalformedURLException e1)

{ text.setText(""+e1);

return;

9

}

catch(IOException e1)

{ text.setText(""+e1);

return;

}

}

import java.util.*;

public class Example13_1

{ public static void main(String args[])

{ List list=new LinkedList();

list.add("is");

list.add("a");

int number=list.size();

System.out.println("现在链表中有"+number+

"个节点:");

for(int i=0;i<number;i++)

{ String temp=(String)list.get(i);

System.out.println("第"+i+"节点中的数据:"+temp); }

list.add(0,"It");

number=list.size();

list.add(number-1,"door");

number=list.size();

System.out.println("现在链表中有"+number+"个节点:"); for(int i=0;i<number;i++)

{ String temp=(String)list.get(i);

System.out.println("第"+i+"节点中的数据:"+temp); }

list.remove(0);

list.remove(1);

list.set(0,"open");

number=list.size();

System.out.println("现在链表中有"+number+"个节点:"); for(int i=0;i<number;i++)

{ String temp=(String)list.get(i);

System.out.println("第"+i+"节点中的数据:"+temp); }

}

}

10

相关推荐