import java.io.*;
abstract class avg{
protected double first_v;
protected double tal_v;
avg(){
first_v = 1;
}
protected abstract double ans();
public void print_v(double fir_v,double all_v,int mm){
if (mm == 1)
{
for(double i=0; i< all_v; i++)
tal_v = tal_v + ( first_v + i ) ;
}else{
tal_v = 1;
for(double i=0; i< all_v; i++)
tal_v = tal_v * ( first_v + i ) ;
}
System.out.println(tal_v*ans());
}
}
class ma_avg extends avg{
protected double t_v;
ma_avg(){
first_v = 1;
t_v = 1;
}
ma_avg(double to_v){
first_v = 1;
t_v = ((double)(1)/to_v);
}
ma_avg(double fir_v ,double to_v){
first_v = fir_v;
t_v = ((double)(1)/to_v);
}
protected double ans() {
return t_v;
}
}
class th_avg extends avg{
protected double t_v;
th_avg(){
first_v = 1;
t_v = 1;
}
th_avg(double to_v){
first_v = 1;
t_v = Math.sqrt(to_v);
}
th_avg(double fir_v ,double to_v){
first_v = fir_v;
t_v = Math.sqrt(to_v);
}
protected double ans() {
return t_v;
}
}
class work4
{
public static void main(String[] args)
{
avg allavg;
System.out.println("以下為首項為2,項數為2之算術平均數");
allavg= new ma_avg(2,2);
allavg.print_v(2,2,1);
System.out.println("以下為首項為5,項數為2之算術平均數");
allavg= new ma_avg(5,2);
allavg.print_v(5,2,1);
System.out.println("以下為首項為4,項數為4之算術平均數");
allavg= new ma_avg(4,4);
allavg.print_v(4,4,1);
System.out.println("以下為首項為3,項數為6之算術平均數");
allavg= new ma_avg(3,6);
allavg.print_v(3,6,1);
System.out.println("以下為首項為2,項數為2之幾何平均數");
allavg= new th_avg(2,2);
allavg.print_v(2,2,0);
System.out.println("以下為首項為5,項數為2之幾何平均數");
allavg= new th_avg(5,2);
allavg.print_v(5,2,0);
System.out.println("以下為首項為4,項數為4之幾何平均數");
allavg= new th_avg(4,4);
allavg.print_v(4,4,0);
System.out.println("以下為首項為3,項數為6之幾何平均數");
allavg= new th_avg(3,6);
allavg.print_v(3,6,0);
}
}
2008年4月25日 星期五
2008年4月18日 星期五
JAVA繼承觀念運用 階乘計算
/***********************************************************
write by tjm
用來展示繼承
***********************************************************/
import java.io.*;
//這只是一個標準的階乘計算class
class frac{
static int y;
public static int fraction() //算階乘
{ int ans,i;
ans=1;
for (i=1;i<=y;i=i+1) ans=ans*i;
return(ans);
} // fraction 結束
}
//這個class用來擴充階乘的計算,讓實數也可以被計算
public class factorial extends frac {
//實數階乘,這個就是擴充的方法
private static int relfraction(double yy)
{ int ans;
int newy,i;
newy=(int) yy; //換成整數
System.out.println("哪有人輸入實數的,下次請輸入整數:"+newy);
ans=1;
for (i=1;i<=newy;i=i+1) ans=ans*i;
return(ans);
} // fraction 結束
//程式進入點
public static void main(String args[]) throws Exception
{ //程式進入點
String getbr;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int x,pt;
double fx;
/*****請使用者輸入選項**********/
x=5;
factorial target=new factorial(); //將 demo24 物件實體化
while (x>=0)
{
System.out.print("請輸入要計算階乘的數字:");
getbr = br.readLine();
/*****判斷是否是整數*******/
pt=getbr.indexOf("."); //輸入字串如果有小數點就是實數
if (pt < 0) //整數
{
x=Integer.parseInt(getbr);
if (x<0)
{
System.out.println("謝謝!再見!");
continue;
}
target.y=x;
System.out.println(x+ "!="+target.fraction());
}
else
{
fx=Double.parseDouble(getbr);
if (fx<0)
{
System.out.println("謝謝!再見!");
continue;
}
System.out.println(fx+ "!="+target.relfraction(fx));
}
} //while結束
} //main 結束
}
write by tjm
用來展示繼承
***********************************************************/
import java.io.*;
//這只是一個標準的階乘計算class
class frac{
static int y;
public static int fraction() //算階乘
{ int ans,i;
ans=1;
for (i=1;i<=y;i=i+1) ans=ans*i;
return(ans);
} // fraction 結束
}
//這個class用來擴充階乘的計算,讓實數也可以被計算
public class factorial extends frac {
//實數階乘,這個就是擴充的方法
private static int relfraction(double yy)
{ int ans;
int newy,i;
newy=(int) yy; //換成整數
System.out.println("哪有人輸入實數的,下次請輸入整數:"+newy);
ans=1;
for (i=1;i<=newy;i=i+1) ans=ans*i;
return(ans);
} // fraction 結束
//程式進入點
public static void main(String args[]) throws Exception
{ //程式進入點
String getbr;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int x,pt;
double fx;
/*****請使用者輸入選項**********/
x=5;
factorial target=new factorial(); //將 demo24 物件實體化
while (x>=0)
{
System.out.print("請輸入要計算階乘的數字:");
getbr = br.readLine();
/*****判斷是否是整數*******/
pt=getbr.indexOf("."); //輸入字串如果有小數點就是實數
if (pt < 0) //整數
{
x=Integer.parseInt(getbr);
if (x<0)
{
System.out.println("謝謝!再見!");
continue;
}
target.y=x;
System.out.println(x+ "!="+target.fraction());
}
else
{
fx=Double.parseDouble(getbr);
if (fx<0)
{
System.out.println("謝謝!再見!");
continue;
}
System.out.println(fx+ "!="+target.relfraction(fx));
}
} //while結束
} //main 結束
}
2008年4月2日 星期三
JAVA寫的身分證字號產生器與檢查器
輸入組數可產生多組身分證字號
下面可輸入身分證字號驗證正確性與可用性
圖為實際執行畫面
以下為實際JAVA程式碼
import java।util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class id_make extends Frame{
char text[]=new char[10];
int value[]=new int[12];
int sum,nR,check;
//台灣地區代碼 A= 10, I= 34, O= 35
char Area_Code[]={'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'I', 'O'};
//建立元件
TextArea textarea=new TextArea("", 15, 15 ,TextArea.SCROLLBARS_VERTICAL_ONLY);
Label label_1=new Label("產生字號組數:");
Label label_2=new Label("輸入身分證字號:");
TextField textfield_1=new TextField("100");
TextField textfield_2=new TextField("",8);
Button button_1=new Button("產生字號");
Button button_2=new Button("檢查字號");
//版面配置
Panel panel_1=new Panel();
Panel panel_2=new Panel(new GridLayout(6, 1, 0, 5));
//建構子
public id_make(){
//增加元件
panel_1.add(textarea);//文字區
panel_2.add(label_1);
panel_2.add(textfield_1);
panel_2.add(button_1);
panel_2.add(label_2);
panel_2.add(textfield_2);
panel_2.add(button_2);
add(panel_1);
add(panel_2);
//關閉視窗傾聽者
addWindowListener(new WindowLis());
}//建構子
//主函式
public static void main(String[] args){
id_make frame=new id_make();//建立 frame
//工作列
MenuBar menubar=new MenuBar();
Menu menu1=new Menu("檔案");
MenuItem menuitem11=new MenuItem("結束");
Menu menu2=new Menu("關於");
MenuItem menuitem21=new MenuItem("說明");
frame.setTitle("JAVA awt 身分證字號產生器");
frame.setResizable(false);//調整視窗大小
frame.setSize(300, 350);//視窗大小
frame.setLocation(300, 200);//視窗位置
frame.setBackground(Color.white);//視窗背景顏色
frame.textarea.setEditable(false);//文字區不可編輯
//版面配置
frame.setLayout(new GridBagLayout());
//建立工作列與選單
frame.setMenuBar(menubar);
menubar.add(menu1);
menu1.add(menuitem11);
menuitem11.addActionListener(frame.new CloseWindow());
menubar.add(menu2);
menu2.add(menuitem21);
menuitem21.addActionListener(frame.new ActionLis21());
//按鈕傾聽
frame.button_1.addMouseListener(frame.new Button1Lis());
frame.button_2.addMouseListener(frame.new Button2MouseLis());
//視窗可見
frame.setVisible(true);
}//主函式
class Button1Lis extends MouseAdapter{
//產生字號
public void mouseClicked(MouseEvent e){
//清空文字區
textarea.setText("");
//建立亂數
Random random=new Random();
for(long l=0;l
//隨機字母
value[0]=random.nextInt()%26;
value[0]=Math.abs(value[0]);//絕對值
text[0]=Area_Code[value[0]];
value[0]+=10;
value[1]=value[0]/10;
value[2]=value[0]%10;
//隨機性別
value[3]=random.nextInt()%2;
value[3]=Math.abs(value[3]);//絕對值
value[3]++;
text[1]=Integer.toString(value[3]).charAt(0);
//隨機字號
for(int i=0;i<7;i++){
value[4+i]=random.nextInt()%10;
value[4+i]=Math.abs(value[4+i]);//絕對值
text[i+2]=Integer.toString(value[4+i]).charAt(0);
}
//加總
sum=value[1];
for(int i=0,j=9;i<9;i++){
sum+=value[i+2]*j;
j--;
}
//餘數
nR=sum%10;
//檢查碼
check=(nR!=0)?10-nR:nR;
text[9]=Integer.toString(check).charAt(0);
textarea.setText(textarea.getText()+String.valueOf(text)+"\n");
}//迴圈
button_1.setLabel("已產生"+Long.parseLong(textfield_1.getText())+"組");
}//mouseClicked
}//Button1Lis
class Button2MouseLis extends MouseAdapter{
//檢查字號
public void mouseClicked(MouseEvent e){
if(textfield_2.getText().length()==0){
button_2.setLabel("請輸入字號");
}else if(textfield_2.getText().length()==10){
//轉換大寫字母
text[0]=Character.toUpperCase(textfield_2.getText().charAt(0));
//轉換區碼
for(int i=1;i<10;i++){
text[i]=textfield_2.getText().charAt(i);
}
for(int i=0;i<26;i++){
if(text[0]==Area_Code[i]){
value[0]=i+10;//區碼
break;
}
}
value[1]=value[0]/10;
value[2]=value[0]%10;
//字元轉數字
for(int i=3;i<12;i++){
value[i]=Character.getNumericValue(text[i-2]);
}
//加總
sum=value[1];
for(int i=0,j=9;i<9;i++){
sum+=value[i+2]*j;
j--;
}
nR=sum%10;
//檢查碼
check=(nR!=0)?10-nR:nR;
if(check!=value[11]){
button_2.setLabel("錯誤");
}else{
button_2.setLabel("正確");
}
}else{
button_2.setLabel("不完整的字號");
}
}//mouseClicked
}//Button2MouseLis
class ActionLis21 implements ActionListener{
//顯示對話框
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "注意:此工具僅作程式碼研究\nBy Fufuk");
}
}
class WindowLis extends WindowAdapter{
//關閉視窗
public void windowClosing(WindowEvent e){
dispose();
}
}
class CloseWindow implements ActionListener{
//結束視窗
public void actionPerformed(ActionEvent e){
dispose();//關閉視窗
}
}
}//類別
訂閱:
文章 (Atom)