۱۳۸۷ مهر ۹, سه‌شنبه

لغات کلیدی جاوا java keywords

زبان برنامه نویسی جاوا از لغات کلیدی زیر استفاده می کند:
The abstract Keyword
The boolean Keyword
The break Keyword
The byte Keyword
The case Keyword
The catch Keyword
The char Keyword
The class Keyword
The continue Keyword
The default Keyword
The do Keyword
The double Keyword
The else Keyword
The extends Keyword
The false Keyword
The final Keyword
The finally Keyword
The float Keyword
The for Keyword
The if Keyword
The implements Keyword
The import Keyword
The instanceof Keyword
The int Keyword
The interface Keyword
The long Keyword
The new Keyword
The null Keyword
The package Keyword
The private Keyword
The protected Keyword
The public Keyword
The return Keyword
The short Keyword
The static Keyword
The super Keyword
The switch Keyword
The synchronized Keyword
The this Keyword
The throw Keyword
The throws Keyword
The transient Keyword
The try Keyword
The true Keyword
The void Keyword
The while Keyword

برای اطلاعات بیشتر اینجا را بخوانید!

۱۳۸۷ مهر ۵, جمعه

جملات switch در جاوا!

جملات switch شکل کوتاه شده ای از نوع جملات if می باشند. جملاتی از نوع if به شکل زیر متداول نیستند:

if (x == 0) doSomething0();
else if (x == 1) doSomething1();
else if (x == 2) doSomething2();
else if (x == 3) doSomething3();
else if (x == 4) doSomething4();
else doSomethingElse();

0

جاوا شکل کوتاه شده ای برای این جملات تکراری دارد که جملات switch-case نامیده می شود. در زیر شما می توانید ببینید چگونه جملاتی نظیر جمله بالا را با استفاده از switch-case بنویسیم:

switch (x) {
case 0:
doSomething0();
break;
case 1:
doSomething1();
break;
case 2:
doSomething2();
break;
case 3:
doSomething3();
break;
case 4:
doSomething4();
break;
default:
doSomethingElse();
}

0

در این قطعه x باید یک متغییر یا اظهار باشد که به صورت یک int بدون کاهش دقت ارزش گذاری شود. این به این معنی است که متغیر باید یک int , byte, short, یا char باشد یا اظهار باید به یکی از شکل های یاد شده برگشت داده شود.


لینک انگلیسی

۱۳۸۷ مهر ۳, چهارشنبه

سازنده ها Constructors

وقتی شما یک نمونه جدید ( یک شی جدید) با استفاده از لغت کلیدی new ایجاد می کنید ، یک سازنده برای آن کلاس صدا زده می شود. سازنده ها جهت ایجاد متغیر عای نمونه ( فیلد ها) یک شی به کار میروند. سازنده ها شبیه متد ها هستند اما با آنها برخی تفاوت های مهم دارند.

*نام سازنده همان نام کلاس است. یک سازنده باید همان نام کلاسی را که در آن است را داشته باشد.
*سازنده های پیش فرض. اگر شما یک سازنده را برای یک کلاس تعریف نکنید یک سازنده پیش فرض توسط کامپایلر ساخته می شود. سازنده پیش فرض سازنده های parent را فرا می خواند(()super). و تمام متغییر های نمونه را مقدار دهی پیش فرض می کند( صفر برای انواع عددی ، تهیnull برای شیء و false برای مقادیر بولی booleans ) .
*سازنده های پیش فرض فقط زمانی خلق می شوند که هیچ سازنده ای موجود نداشته باشد. اگر شما یک سازنده برای کلاس تعریف کنید هیچ سازنده پیش فرضی به طور خودکار ایجاد نمی شود.
*تفاوت های بین متد ها و سازنده ها.
- در یک سازنده نوع بازگشتی وجود ندارد. مقدار خود شی است و نیازی به اختصاص مقدار بازگشتی ندارد.
- در بدنه سازنده هیچ جمله بازگشتی وجود ندارد.
-در خط اول یک سازنده باید یک صدا کننده برای سازنده دیگر در همان کلاس وجود داشته باشد( با استفاده از this )، یا یک صدا کننده بر سازنده ابر کلاس ( با استفاده از super ) . اگر خط اول هیچ کدام را نداشته باشد ، کامپایلر به طور خود کار یک سازنده ابر کلاس وارد می کند.
(ادامه دارد)



محاسبه سن خورشیدی Computing your solar age

این برنامه در گروه جاوا ی گروه کار بران لینوکس اصفهان نوشته شده:

import javax.swing.JOptionPane;
class Age13 {
public static void main (String args[]) {
int Ity, Itm ,Itd ,Iby ,Ibm ,Ibd, cd, cm, cy;
String Name, Ty ,Tm ,Td ,By ,Bm ,Bd;
// double Tdd, Bdd, DAge, YY, MM, DD, MYY, MMM, MDD;
Name = JOptionPane.showInputDialog("Enter your name");

Ty = JOptionPane.showInputDialog("Enter this year");

Tm = JOptionPane.showInputDialog("Enter this month");

Td = JOptionPane.showInputDialog("Enter today");

By = JOptionPane.showInputDialog("Enter your birth year");

Bm = JOptionPane.showInputDialog("Enter your birth month");

Bd = JOptionPane.showInputDialog("Enter your birth day");
Ity = Integer.parseInt(Ty);

Itm = Integer.parseInt(Tm);

Itd = Integer.parseInt(Td);

Iby = Integer.parseInt(By);

Ibm = Integer.parseInt(Bm);

Ibd = Integer.parseInt(Bd);

cd= Itd - Ibd;

if ( cd <0 cd =" cd" itm =" Itm"> 6 && Itm != 12 ){
cd = cd + 30 ;
Itm = Itm - 1 ;
}
else if (cd<0 itm ="=" cd =" cd" itm ="=" cd =" cd" cm =" Itm" cm =" cm" ity =" Ity">12 && cm == 12)
{
cm = cm -12;
Ity = Ity +1;
}
cy = Ity - Iby;
JOptionPane.showMessageDialog( null,
"Your solar years are : " + cy +
"\n Your solar months are : " + cm +
// "\n Your solar days are : " + cd +
"\n Your solar days are : " + cd ,

"Hello " + Name,
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}

This program created by Isfahan JAVA Group


0

۱۳۸۷ مهر ۲, سه‌شنبه

نحوه ساخت فایل jar

فرض کنید برنامه ای را که نوشته ایم با نام Sample.java ذخیره کرده ایم
۱- ابتدا برنامه ای را که نوشته اید کامپایل کنید تا فایل کلاس ساخته شود. javac Sample.java
۲- یک فایل متنی با Text Editor باز کنید و در آن دو خط زیر را تایپ کنید.
Manifest-Version: 1.0
Main-Class: Sample
و با نام Sample.mf آن را ذخیره کنید.
۳- برای ایجاد یک فایل jar دستور زیر را تایپ کنید.

jar cmf Sample.mf Sample.jar Sample.class  Sample.java 

نت بینز ۶.1

با این IDE میتونید به راحتی برنانه نویسی کنید و برای ساخت فرم و انواع ابزار ها نیازی به نوشتن کد ندارید! برای دانلود به سایت زیر مراجعه کنید! http://www.netbeans.org/

یک نرم افزار ساده شیمیA Simple Chemistry Software

این اولین نرم افزار شیمی ه که موفق شدم با جاوا بنویسم !

A simple chemistry software in Persian language


//Chem1.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Chem3 extends JFrame implements ItemListener{

JComboBox cb;
JLabel lbl;
Icon i01, i02, i03, i04, i05, i06, i07, i08, i09, i10, i11, i12 , i13 ,i14 ,i15,i16,i17,i18,i19,i20,i21,i22,i23,i24,i25,i26,i27,i28,i29,i30,i31,i32,i33,i34,i35 ;
//Main
public static void main(String ar[]){
JFrame f=new Chem3();
f.setTitle ("Created By Kian Kiani");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setSize(200,160);
f.setVisible(true);
}
//Frame
public Chem3(){

i01 = new ImageIcon( "images/ca.jpg" );
i02 = new ImageIcon( "images/k.jpg" );
i03 = new ImageIcon( "images/So.jpg" );
i04 = new ImageIcon( "images/Li.jpg" );
i05 = new ImageIcon( "images/mg.jpg" );
i06 = new ImageIcon( "images/fe.jpg" );
i07 = new ImageIcon( "images/ba.jpg" );
i08 = new ImageIcon( "images/cu.jpg" );
i09 = new ImageIcon( "images/shi.jpg" );
i10 = new ImageIcon( "images/p.jpg" );
i11 = new ImageIcon( "images/cr.jpg");
i12 = new ImageIcon( "images/z.jpg");
i13 = new ImageIcon( "images/a.jpg");
i14 = new ImageIcon( "images/km.jpg");
i15 = new ImageIcon( "images/km1.jpg");
i16 = new ImageIcon( "images/am.jpg");
i17 = new ImageIcon( "images/soc.jpg");
i18 = new ImageIcon( "images/cl.jpg");
i19 = new ImageIcon( "images/clc.jpg");
i20 = new ImageIcon( "images/hcl.jpg");
i21 = new ImageIcon( "images/sa.jpg");
i22 = new ImageIcon( "images/su.jpg");
i23 = new ImageIcon( "images/pa.jpg");
i24 = new ImageIcon( "images/ppo.jpg");
i25 = new ImageIcon( "images/si.jpg");
i26 = new ImageIcon( "images/pb.jpg");
i27 = new ImageIcon( "images/cc.jpg");
i28 = new ImageIcon( "images/hip.jpg");
i29 = new ImageIcon( "images/nia.jpg");
i30 = new ImageIcon( "images/nio.jpg");
i31 = new ImageIcon( "images/nis.jpg");
i32 = new ImageIcon( "images/cb.jpg");
i33 = new ImageIcon( "images/amo.jpg");
i34 = new ImageIcon( "images/pc.jpg");
i35 = new ImageIcon( "images/hg.jpg");

cb = new JComboBox();
String[] strmenu = {"CaCO3", "KNO3", "Na2SO4", "LiCl","MgO","Fe(OH)3","BaSO3","Cu(NO2)2","NaOH", "KOH","Cr2S3",
"Zn3(PO4)2","AgI","KMno4","K2MnO4","(NH4)2Cr2O7","Na2CrO4","NaClO2","Ca(ClO3)2","HCl","H2SO4","H2SO3","H3PO4",
"H3PO3","Na2SiO3","PbO2","CaC2","KClO","HNO3","HNO2","NiSO4","Ca(HCO3)2","NH4NO3","KClO4","Hg(NO3)2"};
for( int i=0 ; i < lbl="new" c="getContentPane();" c="getContentPane();" imgidx="cb.getSelectedIndex();" imgidx="="0){" imgidx="="1){" imgidx="="2){" imgidx="="3){" imgidx="="4){" imgidx="="5){" imgidx="="6){" imgidx="="7){" imgidx="="8){" imgidx="="9){" imgidx="="10){" imgidx="="11){" imgidx="="12){" imgidx="="13){" imgidx="="14){" imgidx="="15){" imgidx="="16){" imgidx="="17){" imgidx="="18){" imgidx="="19){" imgidx="="20){" imgidx="="21){" imgidx="="22){" imgidx="="23){" imgidx="="24){" imgidx="="25){" imgidx="="26){" imgidx="="27){" imgidx="="28){" imgidx="="29){" imgidx="="30){" imgidx="="31){" imgidx="="32){" imgidx="="33){" imgidx="="34){" lbl="new" event="" public="" void="" itemevent="" container="" c="getContentPane();" int="" else="" if="" imgidx="cb.getSelectedIndex();" setbackground="" 0="">