(1)分析找出問題域中的對象,並將對象歸類,注意篩選掉不必要的對象或類。
(2)確定類的屬性。
(3)確定對象之間的關係,包括依賴、泛化、關聯、實現等等。
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class BinaryJFrame extends JFrame implements CaretListener
{
private JTextField texts[];
public BinaryJFrame()
{
super("十進制整數轉換");
this.setBounds(300,240,440,140);
this.setResizable(false);
this.setBackground(Color.lightGray);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new FlowLayout(FlowLayout.RIGHT));
String labelstr[]={"十進制","二進制","八進制","十六進制"};
texts = new JTextField[labelstr.length];
for (int i=0; i<texts.length; i++)
{
this.getContentPane().add(new JLabel(labelstr[i]));
texts[i] = new JTextField(32);
texts[i].setEditable(false);
this.getContentPane().add(texts[i]);
}
texts[0].setEditable(true);
texts[0].setText("-1");
texts[0].addCaretListener(this); //註冊文本編輯事件監聽器
this.caretUpdate(null);
this.setVisible(true);
}
public void caretUpdate(CaretEvent e) //文本編輯事件處理方法
{
try
{
int i = Integer.parseInt(texts[0].getText());
texts[1].setText(Integer.toBinaryString(i)); //二進制
texts[2].setText(Integer.toOctalString(i)); //八進制
texts[3].setText(Integer.toHexString(i)); //十六進制
//或
// texts[2].setText(String.format("%o", i)); //八進制
// texts[3].setText(String.format("%x", i)); //十六進制
}
catch(NumberFormatException nfe)
{
JOptionPane.showMessageDialog(this,"/""+texts[0].getText()+"/"不能轉換,請重新輸入!");
}
finally{}
}
public static void main(String arg[])
{
new BinaryJFrame();
}
}
Java進制轉換程序的問題分解,類與對象的關係
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.