实验1 搭建流水线
GymnasticGame.java
public class GymansticGame {
public static void main(String args[]){
StreamLine line=new StreamLine();
double []b=new double[10];
line.giveResult(b);
}
}
DoThing.java
public abstract class DoThing {
public abstract void doThing(double []a);
public abstract void setNext(DoThing next);
}
DoInput.java
import java.util.*;
public class DoInput extends DoThing {
DoThing nextDoThing;
public void setNext(DoThing next){
nextDoThing=next;
}
public void doThing(double []a){
System.out.println("请输入裁判数");
Scanner read=new Scanner(System.in);
int count=read.nextInt();
System.out.println("请输入各裁判的分数");
a=new double[count];
for(int i=0;i<count;i++){
a[i]=read.nextDouble();
}
nextDoThing.doThing(a);
}
}
DelMaxAndMin.java
import java.util.*;
public class DelMaxAndMin extends DoThing {
DoThing nextDoThing;
public void setNext(DoThing next){
nextDoThing=next;
}
public void doThing(double []a){
Arrays.sort(a);
double []b=Arrays.copyOfRange(a,1,a.length-1);
System.out.print("去掉一个最高分:"+a[a.length-1]+",");
System.out.println("去掉一个最低分:"+a[0]);
nextDoThing.doThing(b);
}
}
ComputerAver.java
import java.util.Arrays;
public class ComputerAver extends DoThing {
DoThing nextDoThing;
public void setNext(DoThing next){
nextDoThing=next;
}
public void doThing(double []a){
double sum=0;
for(int i=0;i<a.length;i++)
sum=sum+a[i];
double aver=sum/a.length;
System.out.print("选手最后得分"+aver);
}
}
StreamLine.java
public class StreamLine {
private DoThing one,two,three;
StreamLine(){
one=new DoInput();
two=new DelMaxAndMin();
three=new ComputerAver();
one.setNext(two);
two.setNext(three);
}
public void giveResult(double a[]){
one.doThing(a);
}
}
实验2 排序与查找
Book.java
public class Book implements Comparable {
double price;
String name;
public void setPrice(double c){
price=c;
}
public double getPrice(){
return price;
}
public void setName(String n){
name=n;
}
public String getName(){
return name;
}
public int compareTo(Object object){
Book bk=(Book)object;
int difference=(int)((this.getPrice()-bk.getPrice())*10000);
return difference;
}
}
MainClass.java
import java.util.*;
public class MainClass {
public static void main(String args[]){
List<Book> bookList=new LinkedList<Book>();
String bookName[]={"Java基础教程","XML基础教程","JSP基础教程","C++基础教程","J2ME编程","操作系统","数据库技术"};
double bookPrice[]={29,21,22,29,34,32,29};
Book book[]=new Book[bookName.length];
for(int k=0;k<book.length;k++){
book[k]=new Book();
book[k].setName(bookName[k]);
book[k].setPrice(bookPrice[k]);
bookList.add(book[k]);
}
Book newBook=new Book();
newBook.setPrice(29);
newBook.setName("Java与模式");
Collections.sort(bookList);
int m=-1;
System.out.println("新书"+newBook.getName()+"("+newBook.getPrice()+")与下列图书:");
while((m=Collections.binarySearch(bookList,newBook,null))>=0){
Book bk=bookList.get(m);
System.out.println("\t"+bk.getName()+"("+bk.getPrice()+")");
bookList.remove(m);
}
System.out.println("价钱相同");
}
}
使用TreeSet排序
Student.java
public class Student implements Comparable{
String name;
int score;
Student(String name,int score){
this.name=name;
this.score=score;
}
public int compareTo(Object b){
Student st=(Student)b;
int m=this.score-st.score;
if(m!=0)
return m;
else
return 1;
}
public int getScore(){
return score;
}
public String getName(){
return name;
}
}
StudentFrame.java
import java.awt.*;
import java. awt.event.*;
import java.util.*;
import javax.swing.*;
public class StudentFrame extends JFrame implements ActionListener {
JTextArea showArea;
JTextField inputName,inputScore;
JButton button;
TreeSet<Student> treeSet;
StudentFrame() {
treeSet=new TreeSet<Student>();//使用无参 数构造方法创建treeSet
showArea=new JTextArea();
showArea.setFont(new Font ("",Font.BOLD,20));
inputName=new JTextField(5);
inputScore=new JTextField(5);
button=new JButton("确定");
button.addActionListener (this);
JPanel pNorth=new JPanel ();
pNorth.add(new JLabel("Name:"));
pNorth.add(inputName);
pNorth.add(new JLabel("Score:"));
pNorth.add(inputScore);
pNorth.add(button);
add(pNorth,BorderLayout.NORTH);
add(showArea,BorderLayout.CENTER);
setSize(300,320);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
String name=inputName.getText();
int score=0;
try{ score=Integer.parseInt(inputScore.getText().trim());
if (name. length()>0) {
Student stu=new Student(name,score);
treeSet.add(stu);//treeSet 添加stu
show (treeSet);
}
}
catch (NumberFormatException exp){
inputScore.setText ("请输入数字字符");
}
}
public void show(TreeSet tree){
showArea.setText(null);
Iterator<Student>te=treeSet.iterator();
while(te.hasNext()){
Student stu=te.next();//te 返回其中的下一个元素
showArea.append("Name:"+stu.getName()+"Score:"+stu.getScore()+"\n");
}
}
public static void main (String args[]) {
new StudentFrame();
}
}
实验4 扫雷小游戏
Block.java
public class Block {
String name;
int number;
boolean boo=false;
public void setName(String name){
this.name=name;
}
public void setNumber(int n){
number=n;
}
public int getNumber(){
return number;
}
public String getName(){
return name;
}
boolean isMine(){
return boo;
}
public void setIsMine(boolean boo){
this.boo=boo;
}
}
LayMines.java
import java.util.*;
public class LayMines {
private int mineNumber;
public void layMinesForBlock(Block block[][], int mineCount){
int row=block.length;
int column=block[0].length;
LinkedList<Block> list=new LinkedList<Block>();//创建空链表 list
for(int i=0;i<row;i++){
for(int j=0; j<column;j++)
list.add(block[i][j]);//list 添加结点,其中的数据为block[i][j]
}
while (mineCount>0) {
int size= list.size();//list 返回结点的个数
int randomIndex=(int)(Math.random()*size);
Block b= list.get(randomIndex);//list 返回索引为randomIndex的结点中的数据
b.setName("雷");
b.setIsMine(true);
list.remove(randomIndex);
mineCount--;
}
for(int i=0;i<row;i++) {
for(int j=0;j<column;j++) {
if(block[i][j].isMine()){}
else
mineNumber=0;
for(int k=Math.max(i-1,0);k<=Math.min(i+1,row-1);k++) {
for(int t=Math.max(j-1,0);t<=Math.min(j+1,column-1);t++){
if(block[k][t].isMine())
mineNumber++;
}
}
if(mineNumber>0) {
block[i][j].setName(""+mineNumber);
block[i][j].setNumber(mineNumber);
}
else {
block[i][j].setName(" ");
block[i][j].setNumber(mineNumber);
}
}
}
}
}
BlockView.java
import java.awt. *;
import javax.swing.*;
public class BlockView extends JPanel {
JLabel blockName;
JButton blockCover;
CardLayout card;
BlockView() {
card=new CardLayout();
setLayout(card);
blockName=new JLabel();
blockCover=new JButton();
add("cover",blockCover);
add ("name", blockName);
}
public void setName(String name) {
blockName.setText(name);
}
public String getName(){
return blockName.getText();
}
public void seeBlockName() {
card.show(this,"name");
validate();
}
public void seeBlockCover () {
card.show(this,"cover");
validate();
}
public JButton getBlockCover() {
return blockCover;
}
}
MineMainFrame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MineMainFrame extends JFrame implements ActionListener{
JButton reStart;
Block block[][];
BlockView blockView[][];
LayMines lay;
int row=10, colum=12, mineCount=22;
int colorSwitch=0;
JPanel pCenter,pNorth;
public MineMainFrame() {
reStart=new JButton("重新开始");
pCenter=new JPanel();
pNorth=new JPanel();
pNorth.setBackground(Color.cyan);
block=new Block[row][colum];
for(int i=0;i<row;i++) {
for(int j=0;j<colum;j++)
block[i][j]=new Block();
}
lay=new LayMines();
lay.layMinesForBlock (block,mineCount);
blockView=new BlockView[row][colum];
pCenter.setLayout(new GridLayout(row,colum));
for(int i=0;i<row;i++) {
for(int j=0;j<colum;j++) {
blockView[i][j]=new BlockView();
blockView[i][j] .setName (block[i][j].getName());
pCenter.add (blockView[i][j]);
blockView[i][j].getBlockCover().addActionListener(this);
}
}
reStart.addActionListener(this);
pNorth.add(reStart);
add(pNorth,BorderLayout.NORTH);
add(pCenter,BorderLayout.CENTER);
setSize(200,232);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed (ActionEvent e) {
JButton source=(JButton)e.getSource();
if(source!=reStart){
int m=-1,n=-1;
for(int i=0;i<row;i++) {
for(int j=0;j<colum;j++) {
if(source==blockView[i][j].getBlockCover()){
m=i;
n=j;
break;
}
}
}
if(block[m][n].isMine()){
for(int i=0;i<row;i++){
for (int j=0;j<colum;j++) {
blockView[i][j].getBlockCover().removeActionListener(this);
if(block[i][j].isMine())
blockView[i][j].seeBlockName();
}
}
}
else {
if (block[m][n].getNumber()>0)
blockView[m][n].seeBlockName();
else if(block[m][n].getNumber()==0)
for(int k=Math.max(m-1,0);k<=Math.min(m+1,row-1);k++){
for(int t=Math.max(n-1,0);t<=Math.min(n+1,colum-1);t++)
blockView[k][t].seeBlockName();
}
}
}
if(source==reStart) {
for(int i=0;i<row;i++) {
for(int j=0;j<colum;j++)
block[i][j].setIsMine(false);
}
lay.layMinesForBlock(block,mineCount);
for(int i=0;i<row;i++){
for(int j=0;j<colum;j++){
blockView[i][j].setName(block[i][j].getName());
blockView[i][j].seeBlockCover();
blockView[i][j].getBlockCover().addActionListener(this);
}
}
}
}
public static void main(String args[]) {
new MineMainFrame();
}
}