清心博客圈,祝你圣诞节快乐
显示标签为“java”的博文。显示所有博文
显示标签为“java”的博文。显示所有博文

2008年12月20日星期六

[原创分享]离散数学 Binary Tree in Java 基本实现

原文转载自:http://bbs.qxinnet.com/thread-25756-1-1.html

刚闲来无事,下个星期二就考Java了。
全部人都考完了,无名都回新山了,只剩下我一个人。。。夜晚,码头,我一个人,看着遥远的家乡。。。
所以没有事情,无聊人又在忙。。。就只好写一个Java吧!
上个学期数学上了数学,学到Tree,Binary Tree 的。。。上个学期原本想用 Java 实现,但基于基础不够好,怎么可能用Array弄出来呢,但现在也要上完基础的Java了,对物件导向也有较深一层的认识,因此我尝试根据我的思考,来编写了这个基本的实现程序。。。
我用英文做了一些注释,大家慢慢看吧!蛮简单的程序。
有问题还是有错误,请指教

class Node {

public Node parentElement; // parent element of the node

public Node leftElement; // left child element of the node

public Node rightElement; // right child element of the node

public String value; // the value of the node

}

public class TestNode {

public static void main(String args[]) {

//String sentence = "The fox is jump over that fence";

String sentence = "The three fox are jump over that fence";



// Break each word into array

String word[] = sentence.split(" ");



// Initialize the main tree

Node node = new Node();



// SetTree

for(int i=0; i
if(i == 0) {

node.value = word[i];

node.parentElement = null;

} else

setNode(node, word[i]);



}



// GetTree

System.out.println("Demonstration of Mathematic for Computing: Tree");

System.out.println("Sentence: " + sentence);

System.out.println("Height of tree: " + getNodeHeight(node));

System.out.println("Leaf: " + getLeaf(node));

System.out.println("Parent of jump: " + getParent(node, getNode(node, "jump")).value);

System.out.println("Parent of fence: " + getParent(node, getNode(node, "fence")).value);

System.out.println("Height of sub-tree on fox: " + getNodeHeight(getNode(node, "fox")));

}



// Set the structure of the tree on each node

// @param Tree of node, word (value)

public static void setNode(Node node, String word) {

Node childNode;

if(node.value != null) {

childNode = new Node();

childNode.parentElement = node;

childNode.value = word;



// compare ignore case

word = word.toLowerCase();

node.value = node.value.toLowerCase();



if(word.compareTo(node.value) == 0)

return; // if equal, break out of method

else if(word.compareTo(node.value) < 0) {

if(node.leftElement != null)

setNode(node.leftElement, word);

else

node.leftElement = childNode;



} else {

if(node.rightElement != null)

setNode(node.rightElement, word);

else

node.rightElement = childNode;



}



}

}



// Get node by value

// @param Tree of node, value

// @return the node want to get

public static Node getNode(Node node, String value) {

Node targetNode = null;

if(value.equals(node.value))

targetNode = node;

else {

// check if left or right got target, then only one target found only, strange problem, getParent also encountered, maybe not very good

if(node.leftElement != null)

targetNode = targetNode == null ? getNode(node.leftElement, value) : targetNode;



if(node.rightElement != null)

targetNode = targetNode == null ? getNode(node.rightElement, value) : targetNode;

}



return targetNode;

}



// Get height of the tree

// @param Tree node

// @return height of tree

public static int getNodeHeight(Node node) {

int height = 1;

int leftHeight = 0, rightHeight = 0;

if(node.leftElement == null && node.rightElement == null)

return height;

else {

if(node.leftElement != null)

leftHeight += getNodeHeight(node.leftElement);



if(node.rightElement != null)

rightHeight += getNodeHeight(node.rightElement);



}



return height + Math.max(leftHeight, rightHeight);

}



// Get number of leafs in the tree

// @param Tree node

// @return number of leaf

public static int getLeaf(Node node) {

int leaf = 0;

if(node.leftElement == null && node.rightElement == null)

return 1;

else {

if(node.leftElement != null)

leaf += getLeaf(node.leftElement);



if(node.rightElement != null)

leaf += getLeaf(node.rightElement);



}



return leaf;

}



// Get parent of node by value

// @param Tree of node, child of node

// @return the parent of node

public static Node getParent(Node node, Node childNode) {

// found that node

Node parentNode = null;



if(node.value.equals(childNode.value))

return node.parentElement;

else {

if(node.leftElement != null)

parentNode = parentNode == null ? getParent(node.leftElement, childNode) : parentNode;



if(node.rightElement != null)

parentNode = parentNode == null ? getParent(node.rightElement, childNode) : parentNode;



}

return parentNode;

}

}

2008年12月8日星期一

Java showFadeMessage

The author fyhao has written a Java program when he was very boring.

It is a showFadeMessage java program, a paint panel, with a thread to control the animation.

image

Figure A UML Diagram

PaintPanel.java


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class PaintPanel extends JPanel implements MouseListener, MouseMotionListener {
private int x,y,w,h;
private showFadeMessage fadeMsg;
private controlThread t;
public PaintPanel() {
x=0;
y=0;
w=10;
h=10;
fadeMsg = new showFadeMessage("");
t = new controlThread();
t.start();
addMouseListener(this);
addMouseMotionListener(this);
}
public void setx(int x) throws IllegalNumberException {
if(x < 0 || x > 200) throw new IllegalNumberException(1);
this.x = x;
}
public int getx() {
return x;
}
public void sety(int y) throws IllegalNumberException {
if(y < 0 || y > 200) throw new IllegalNumberException(1);
this.y = y;
}
public int gety() {
return y;
}
public void setw(int w) throws IllegalNumberException {
if(w < 0 || w > 200) throw new IllegalNumberException(2);
this.w = w;
}
public int getw() {
return w;
}
public void seth(int h) throws IllegalNumberException {
if(h < 0 || h > 200) throw new IllegalNumberException(2);
this.h = h;
}
public int geth() {
return h;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.WHITE);
g.drawString("X: " + x + " Y: " + y + " Width: " + w + " Height: " + h,20,20);
g.drawRect(30,30,200,200);
g.setColor(new Color(0,0,255));
g.fillRect(x+30,y+30,w,h);
try {
if("".equals(fadeMsg.getText()) == false && fadeMsg.getGray() < 255) {
g.setColor(new Color(fadeMsg.getGray(), fadeMsg.getGray(), fadeMsg.getGray()));
g.drawString(fadeMsg.getText(), 100,100);
}
} catch (Exception e) {}
}
public void mouseExited(MouseEvent e) {
fadeMsg.setText("Mouse Exited");
}
public void mouseEntered(MouseEvent e) {
fadeMsg.setText("Mouse Entered");
}
public void mouseReleased(MouseEvent e) {
fadeMsg.setText("Mouse Released");
}
public void mousePressed(MouseEvent e) {
fadeMsg.setText("Mouse Pressed");
}
public void mouseClicked(MouseEvent e) {
fadeMsg.setText("Mouse Clicked");
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
try {
setx(e.getX() - 30);
sety(e.getY() - 30);
} catch (IllegalNumberException ine) {}
repaint();
}
class controlThread extends Thread {
public void run() {
while(true) {
try {
sleep(100);
fadeMsg.setGray(fadeMsg.getGray() + 15);
} catch (InterruptedException e) {}
repaint();
}
}
}
}



 



showFadeMessage.java



public class showFadeMessage { 
private String text = "";
private int gray;
public showFadeMessage(String text) {
this.text = text;
gray = 255;
}
public void setText(String text) {
this.text = text;
setGray(0);
}
public void setGray(int gray) {
this.gray = gray;
}
public String getText() {
return text;
}
public int getGray() {
return gray;
}
}



 



IllegalNumberException.java



public class IllegalNumberException extends Exception { 
private int type;
public IllegalNumberException(int type) {
this.type = type;
}
public String getMessage() {
String r = "";
switch(type) {
case 1:
r = "The X,Y number should not be less than 0 and more than 200";
break;
case 2:
r = "The Width, Height number should not be less than 0 and more than 200";
break;
default:
r = "Unknown Exception";
}
return r;
}
}



TestPaint.java



import javax.swing.*; 
import java.awt.*;
import java.awt.event.*;

public class TestPaint extends JFrame implements ActionListener {
private JTextField tfX, tfY,tfW,tfH;
private PaintPanel paintPanel;
private JButton submitBtn, resetBtn;
public TestPaint() {
tfX = new JTextField(10);
tfY = new JTextField(10);
tfW = new JTextField(10);
tfH = new JTextField(10);
tfX.setText("0");
tfY.setText("0");
tfW.setText("10");
tfH.setText("10");
paintPanel = new PaintPanel();
}
public void launchFrame() {
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(5,2,10,10));
leftPanel.add(new JLabel("X: "));
leftPanel.add(tfX);
leftPanel.add(new JLabel("Y: "));
leftPanel.add(tfY);
leftPanel.add(new JLabel("W: "));
leftPanel.add(tfW);
leftPanel.add(new JLabel("H: "));
leftPanel.add(tfH);
leftPanel.add(submitBtn = new JButton("Submit"));
leftPanel.add(resetBtn = new JButton("Reset"));
submitBtn.addActionListener(this);
resetBtn.addActionListener(this);
Container con = getContentPane();
con.setLayout(new BorderLayout());
con.add(leftPanel, BorderLayout.WEST);
con.add(paintPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == submitBtn) {
try {
paintPanel.setx(Integer.parseInt(tfX.getText()));
paintPanel.sety(Integer.parseInt(tfY.getText()));
paintPanel.setw(Integer.parseInt(tfW.getText()));
paintPanel.seth(Integer.parseInt(tfH.getText()));
paintPanel.repaint();
} catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(null, "Please ensure that there is no illegal value in the textfield");
} catch (IllegalNumberException ine) {
JOptionPane.showMessageDialog(null, ine.getMessage());
}
}
if(e.getSource() == resetBtn) {
tfX.setText("0");
tfY.setText("0");
tfW.setText("10");
tfH.setText("10");
}
}
public static void main(String args[]) {
TestPaint test = new TestPaint();
test.setTitle("Controlling Paint Program");
test.launchFrame();
test.setSize(500,300);
test.setVisible(true);
test.setResizable(false);
test.setLocationRelativeTo(null);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}



Screen shot:



image



Figure B Screen shot of the paint program



Function:



Four textfield with X-axis, Y-axis, weight and height of the blue box. Whenever a user click the "Submit" button, the blue box at the right will change to its position and also change the size depend on the value the user entered. And also a "Reset" button to reset the value of the textfield.



At the right screen, as we can see the position and the size's data of the blue box. In the center of the right screen, we can see the message shown with bright text, but it will fade to invisible, in animation slide. Whenever the user's mouse move out move in it will show the messages...



And also the user capable of drag the blue box and the data at the top will change automatically.

2008年5月26日星期一

准备开始学习手机编程

想不到这些奇迹,即将发生在我的身上。
我的电脑算不上很好,但是基本上真的还是很快,适合做任何的开发用途。
我的手机也是传说中最便宜但功能最多的牌子,也算不上最好,但好像可以让我做开发用途。但不要说得太早,我最终目的是要做到蓝牙控制电脑,再来学电脑控制web cam啊!!!

接下来就是进行prototyping

接下来就是要进行测试,学习,做一个hello world的程式吧!

开始开发分享的道路,普及化教育,方便别人,也方便自己

对于这个不是很有商业性质的东西,可以拿来教育,写一些学习教程也不错。可以方便后来初学者,也可以方便我 - - 正在初学者,在以后可以回顾我的教程也好。

今天和她msn

我和我的10岁妹妹msn了。很开心呢。她终于会msn了。哈哈!!!要走的时候特别教她论坛和msn,现在她论坛也有很多朋友,刚刚和她聊天了,她除了打字慢之外,其实都能用哦,都会用哦。真的是太开心咯。而且我妹妹也是很美丽很可爱,长大了一定是个美人儿。而且天资聪明。但,是对有兴趣才厉害,马来文和英文不见得厉害,不及格就有。哈哈!!!但是这些东西啊,魔术啊,她很喜欢,所以这些她可以驾驭。其他就不可以。但是,当过学生的我,我知道这些东西是蛮重要的,她真的要好好学习才会有长进。

今天也和她msn

和我的曾经暗恋的人真的是人生一大乐事,但是,我们,算是谈得还好吧!和她说了我妹妹上msn和我,她也说了几句,她也问我学习如何,我也说还好吧!但是不懂她如何了。我也没有问起。我应该多多关心她,问她一些她的东西,为什么刚才没有继续追问下去。

这几天课程一直更换来更换去

这几天啊,课程一直换来换去,把原本还有空洞的时间,弄到很挤,很满。

接下来要做分析

接下来的文章应该做一些分析,做一些评论文章,这样才对吗!!!做一些有关读书上遇到的文章才是正确的道路。明天或者灯下应该开始做一些project研究,要不然怎么下去啊!!!

学习就如政治是个怎么样的路程

政治,好比学习之路,我随便说的,最近那么多政治课题动摇了我的脑袋。