الثلاثاء، 30 يونيو 2009

JOptionPane how to use

تمرير String على شكل Object

كود
String msg[] = {"Welcome", "Home"};
JOptionPane.showMessageDialog(null, msg);


في هذا المثال قمنا بانشاء اوبجيكت من String ومررناها الي JOptionPane

فكان شكلها كالتالي






تحديد عرض مربع الحوار

لو اردنا عرض مربع حوار لعرض رسالة

وهذه الرسالة التي نريد عرضها للمستخدم طويلة

بالطبع سيظهر مربع الحوار وبداخله الرسالة و عرضه يعتمد على طول الرسالة

فنجد في بعض الاحيان ان الرسالة تظهر خارج حدود الشاشة فما الحل ؟

نحدد عرض مربع الحوار بحيث تصبح الرسالة في اكثر من سطر

لكن لدينا مشكلة اخرى

مكونات JOptionPane فقط للقراءة read-only

لذلك سنقوم بعملية override و التعديل على خصائص JOptionPane

وذلك لان عرض مربع الحوار يأخذ القيمة الافتراضية لطول ال String

وعند تحديد قيمة لعرض مربع الحوار يقوم بعملية word-wrap

بمعنى اذا كانت الرسالة طويلة سيتم تقسيمها حسب عرض مربع الحوار في عدة اسطر وليس العكس كما هو الحال الافتراضي

لنرى مثال

كود
public static JOptionPane getNarrowOptionPane(int maxCharactersPerLineCount) {

class NarrowOptionPane extends JOptionPane {
int maxCharactersPerLineCount;
NarrowOptionPane(int maxCharactersPerLineCount) {
this.maxCharactersPerLineCount =
maxCharactersPerLineCount;
}
public int getMaxCharactersPerLineCount() {
return maxCharactersPerLineCount;
}
}

return new NarrowOptionPane(maxCharactersPerLineCount);
}


شرح الكود

قمنا بعمل ميثود ترجع قيمة من نوع JOptionPane وتستقبل متغير من نوع int

وهو قيمة عرض مربع الحوار التي سنمررها

بعد ذلك قمنا بتعريق كلاس داخلية inner class تحمل اسم NarrowOptionPane وترث من JOptionPane

و قمنا بتعريف متغير maxCharactersPerLineCount وهو موجود اصلا في JOptionPane

وعرفنا دالة بناء constructor وعدلنا على قيمة maxCharactersPerLineCount

وانشأنا ميثود ترجع قيمة maxCharactersPerLineCount


وهذا الكود كامل

كود
import javax.swing.*;

public class TestJOptionPane extends JFrame{

public static JOptionPane getNarrowOptionPane(int maxCharactersPerLineCount) {
// Our inner class definition
class NarrowOptionPane extends JOptionPane {
int maxCharactersPerLineCount;
NarrowOptionPane(int maxCharactersPerLineCount) {
this.maxCharactersPerLineCount =
maxCharactersPerLineCount;
}
public int getMaxCharactersPerLineCount() {
return maxCharactersPerLineCount;
}
}

return new NarrowOptionPane(maxCharactersPerLineCount);
}

public static void main(String arg[]){

String msg = "This is a really long message. ...This is a really long message. ...This is a really long message. ...This is a really long message. ...This is a really long message. ...This is a really long message. ...";
JOptionPane pane = getNarrowOptionPane(50);
pane.setMessage(msg);
pane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = pane.createDialog(null, "Width 50");
dialog.show();

}

}


في داخل الميثود main

عرفنا String ومررنا لها قيمة طووووويلة

انشأنا اوبجيكت من JOptionPane اسميناها pane

و اعطيناها قيمة من الميثود getNarrowOptionPane مع تمرير قيمة 50 وهي التي ستكون عرض مربع الحوار

القيمة 50 هي عدد الاحرف التي ستظهر في السطر الواحد

بعد ذلك استخدمنا JDialog لعرض JOptionPane وذلك عن طريق انشاء اوبجيكت جديد وتمرير pane لها

وبعد ذلك عرض ال dialog


سيكون الشكل كالتالي




خروج صوت عند ظهور مربع الحوار

يوجد اربعة انواع من الاصوات

OptionPane.errorSound

OptionPane.informationSound

OptionPane.questionSound

OptionPane.warningSound

وهم على حسب نوع مربع الحوار

اما استخدامهم فهو كالتالي

كود
UIManager.put("OptionPane.errorSound","sounds/OptionPaneError.wav");


UIManager.put تستقبل قيمتين الاولى نوع الصوت والثانية مسار ملف الصوت

وعند وضع هذا الكود في البرنامج فيقوم تلقائيا باصدار الصوت حسب نوع الرسالة

بدلا من هذا الكود يمكننا استخدام كود آخر ليريحنا

كود
UIManager.put("AuditoryCues.playList",UIManager.get("AuditoryCues.defaultCueList"));


في هذا الكود يقوم باحضاء كافة الاصوات الاربعة ويصدر كل صوت حسب نوع الرسالة


مثال

كود
import javax.swing.*;

public class TestJOptionPane extends JFrame{


public static void main(String arg[]){
UIManager.put("AuditoryCues.playList",UIManager.get("AuditoryCues.defaultCueList"));

JOptionPane.showMessageDialog(null,"QUESTION ?????","QUESTION_MESSAGE",JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null,"ERROR !!!!!!!","QUESTION_MESSAGE",JOptionPane.ERROR_MESSAGE);

}

}



مشروع جافا 1 مع الشرح, Multi number system calculator

السلام عليكــم ورحمـة الله وبركاتــة ،،

المشروع طالب عمل آلة حاسبة تقوم بالعمليات الاساسية لعدة انظمة من الارقام

الجمع
الطرح
الضرب
القسمة

للانظمة التالية

الثنائي
العشري
الثماني

والتحويل من نظام الي آخر

في هذا الشرح يوجد كسور والتي سنتجاهلها في عمل المشروع لانه حسب ما هو موضح في المشروع المطلوب الاعداد الصحيحة فقط

طبعا طلب منا نعمل كلاسات

الكلاس الاساسية Number

وثلاث كلاسات اخرى ترث من الكلاس الاساسية

Binary Decimal Octal

قبل ما نبدا في عمل الكلاسات خلينا نفهم الانظمة كيف بتم التعامل معها


النظام الثنائي Binary

إن الأساس المستعمل في النظام الثنائي هو 2 ويتكون هذا النظام من رقمين فقط هما 0 و1 ويسمى كل منهما رقماً ثنائياً Binary Digit

ولتمثيل كل من الرقمين 0 و 1 فأنه لا يلزم إلا خانة واحدة, ولهذا السبب أصبح من الشائع أطلاق اسم بت Bit على الخانة التي يحتلها الرقم داخل العدد الثنائي

التحويل من النظام الثنائي إلى النظام العشري :

لتحويل أي عدد ثنائي إلى مكافئه العشري فإنه يجب علينا استعمال قانون التمثيل الموضعي للأعداد

و ينطبق هذا القانون عندما يكون الرقم الثنائي صحيحاً أو كسراً مع مراعاة أن أساس نظام العد هنا هو 2



مثال حول العدد الثنائي التالي إلى مكافئه العشري:




تحويل الأعداد من النظام العشري إلى الثنائي :

لتحويل أي عدد صحيح موجب من النظام العشري إلى الثنائي نستعمل طريقة الباقي Remainder Method الموضحة كالآتي:

1. أقسم العدد العشري على الأساس 2 .
2. أحسب باقي القسمة الذي يكون أما 1 أو 0 .
3. أقسم ناتج القسمة السابق على الأساس 2 كما في خطوة (1).
4. أحسب باقي القسمة كما في خطوة (2).
5. استمر في عملية القسمة وتحديد الباقي حتى يصبح خارج القسمة الصحيح صفراً.
6. العدد الثنائي المطلوب يتكون من أرقام الباقي مقروءة من الباقي الأخير إلى الأول (لاحظ أن الباقي الأول يمثل LSD بينما يمثل الباقي الأخير MSD ).
مثال لتحويل الرقم 12 من النظام العشري إلى الثنائي نتبع الآتي:



ناتج القسمة الباقي

12 ÷2 =6 0 الخانة الأدنى منزلة LSD
0 6÷2 =3
1 3÷2 =1
1 1÷2 =0 الخانة الأعلى منزلة MSD


فيكون الناتج (من أسفل إلى أعلى ومن اليسار إلى اليمين):



النظام الثماني Octal System :



كما هو معروف فإن أساس النظام الثماني هو العدد 8.وتتكون رموز هذا النظام من الأرقام

التحويل من النظام الثماني إلى العشري:



للتحويل من النظام الثماني إلى النظام العشري يستعمل قانون التمثيل الموضعي للأعداد مع مراعاة أن أساس نظام العد هنا هو 8 .

مثال حول العدد الثماني إلى مكافئه العشري ؟



الناتج :

تحويل من النظام العشري إلى الثماني:

مثال حول العدد العشري 122 إلى مكافئه الثماني؟

ناتج القسمة الباقي
122÷8=15 2
15÷8=1 7
1÷8=0 1

إنهاء القسمة

فيكون الناتج (من أسفل إلى أعلى ومن اليسار إلى اليمين):



وهذا ما سنتبعه في عمل المشروع


نبدا في البرمجة و نعمل الكلاسات اللي موضحة بالمشروع

الكلاس الاول Number

كود
public class Number{
public String number;


void printInFormat(){
System.out.println("The result is ***"+number);
}

}



كلاس النظام الثنائي Binary

كود
public class Binary extends Number{


boolean check_B(){
boolean check=false;
for (int i = 0; i {

if(Character.getNumericValue(number.charAt(i))>1||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in binary format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToDecimal(){
int n=0;
for (int i = number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==number.length()-1)
{
n+=Character.getNumericValue(number.charAt(j))*Math.pow(2,i);

}
}
}

return n;

}
int SumTwoBinary(Binary b){


int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);

}
}
}

y=n+x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
int SubTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);

}
}
}

y=n-x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
int MultiTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);

}
}
}

y=n*x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
int DivTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);

}
}
}

y=n/x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
}


كلاس النظام العشري Decimal

كود
public class Decimal extends Number{


boolean check_D(){
boolean check=false;
for (int i = 0; i {

if(Character.getNumericValue(number.charAt(i))>9||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in decimal format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToBinary(){
int y=Integer.parseInt(this.number);
String result="";
String last="";
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);


}
int convertToOctal(){
int y=Integer.parseInt(this.number);
String result="";
String last="";
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
int SumTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y+z;
}
int SubTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y-z;
}
int MultiTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y*z;
}
int DivTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y/z;
}
}



كلاس النظام الثماني Octal

كود
public class Octal extends Number{


boolean check_Oc(){
boolean check=false;
for (int i = 0; i {

if(Character.getNumericValue(number.charAt(i))>7||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in octal format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToDecimal(){

int n=0;
for (int i = number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==number.length()-1)
{
n+=Character.getNumericValue(number.charAt(j))*Math.pow(8,i);

}
}
}

return n;

}
int SumTwoOctal(Octal c){



int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);

}
}
}

y=n+x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}





int SubTwoOctal(Octal c){

int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);

}
}
}

y=n-x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
int MultiTwoOctal(Octal c){

int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);

}
}
}

y=n*x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
int DivTwoOctal(Octal c){

int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);

}
}
}

y=n/x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);

return Integer.parseInt(last);
}
}



الان الكلاس الرئيسي Test

كود
import java.util.Scanner;
public class Test{
static Scanner input=new Scanner(System.in);
public static void main(String arg[]){
int ch=0;

do{
System.out.println("**********************************************************************");
System.out.println("1.Decimal 2.Binary 3.Octal 4.Conversions 5.Quit");
System.out.println("**********************************************************************");
System.out.print("SHOOSE YOUR SYSTEM: ");
ch=input.nextInt();
switch(ch){
case 1:Decimal();break;
case 2:Binary();break;
case 3:Octal();break;
case 4:Conversions();break;
case 5:Quit();break;
}
}while(ch!=5);
}
public static void Decimal(){
char c='0';
System.out.println("\t\t A) Add two Decimal");
System.out.println("\t\t S) Subtract two Decimal");
System.out.println("\t\t M) Multiple two Decimal");
System.out.println("\t\t D) Divide two Decimal");
System.out.println("\t\t B) Back to main menu");
do{

System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':adddecimal();break;
case 'S':subdecimal();break;
case 'M':muldecimal();break;
case 'D':divdecimal();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');

}
public static void adddecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.SumTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void subdecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.SubTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void muldecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.MultiTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void divdecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.DivTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void Binary(){
char c='0';
System.out.println("\t\t A) Add two binary");
System.out.println("\t\t S) Subtract two binary");
System.out.println("\t\t M) Multiple two binary");
System.out.println("\t\t D) Divide two binary");
System.out.println("\t\t B) Back to main menu");
do{

System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':addbinary();break;
case 'S':subbinary();break;
case 'M':mulbinary();break;
case 'D':divbinary();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void addbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.SumTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void subbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.SubTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void mulbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.MultiTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void divbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.DivTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void Octal(){
char c='0';
System.out.println("\t\t A) Add two Octal");
System.out.println("\t\t S) Subtract two Octal");
System.out.println("\t\t M) Multiple two Octal");
System.out.println("\t\t D) Divide two Octal");
System.out.println("\t\t B) Back to main menu");
do{

System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':addOctal();break;
case 'S':subOctal();break;
case 'M':mulOctal();break;
case 'D':divOctal();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void addOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.SumTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void divOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.DivTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void subOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.SubTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void mulOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.MultiTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;

}
public static void Conversions(){
int c=0;
System.out.println("\n This section is designed to convert from one system to another.....\n");
System.out.println("1- Decimal to Binary\t3- Decimal to Octal\t5- Binary to Octal");
System.out.println("2- Binary to Decimal\t4- Octal to Decimal\t6- Octal to Binary");
System.out.println("\t\t 7- Back to Main");
System.out.println("****************************************************************************");
do{

System.out.print("\nENTER YOUR CHOICE: ");
c=input.nextInt();

switch(c){
case 1:D2B();break;
case 2:B2D();break;
case 3:D2O();break;
case 4:O2D();break;
case 5:B2O();break;
case 6:O2B();break;
case 7:System.out.println("Finished section............");
}
}while(c!=7);
}
public static void D2B(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Decimal d1=new Decimal();
d1.number=first;
if(d1.check_D())
{
int ans=d1.convertToBinary();
System.out.println("The result is *** "+ans);
}else return;

}
public static void B2D(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Binary b1=new Binary();
b1.number=first;
if(b1.check_B())
{
int ans=b1.convertToDecimal();
System.out.println("The result is *** "+ans);
}else return;

}
public static void D2O(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Decimal d1=new Decimal();
d1.number=first;
if(d1.check_D())
{
int ans=d1.convertToOctal();
System.out.println("The result is *** "+ans);
}else return;

}
public static void O2D(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Octal o1=new Octal();
o1.number=first;
if(o1.check_Oc())
{
int ans=o1.convertToDecimal();
System.out.println("The result is *** "+ans);
}else return;

}
public static void B2O(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Binary b1=new Binary();
b1.number=first;
if(b1.check_B())
{
int temp=b1.convertToDecimal();
Decimal d1=new Decimal();
d1.number=""+temp;
int ans=d1.convertToOctal();
System.out.println("The result is *** "+ans);
}else return;

}
public static void O2B(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Octal o1=new Octal();
o1.number=first;
if(o1.check_Oc())
{
int temp=o1.convertToDecimal();
Decimal d1=new Decimal();
d1.number=""+temp;
int ans=d1.convertToBinary();
System.out.println("The result is *** "+ans);
}else return;

}
public static void Quit(){
System.out.println("Thank you for ysing our system....");
}
}



تحياتي

عمل ساعة رقمية بالجافا

This is a basic digital clock in Java that works off your operating system time, it works in a multithreaded environment and have coded it to put my own background in.


كود
import java.awt.*;
import javax.swing.*;
import java.util.*;

class Clock extends JFrame implements Runnable
{
Thread runner; //declare global objects
Font clockFont;

public Clock()
{
super("Java clock");
setSize( 350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false); //create window

clockFont = new Font("Serif", Font.BOLD, 40); //create font instance

Container contentArea = getContentPane();
ClockPanel timeDisplay = new ClockPanel();


contentArea.add(timeDisplay); //add components
setContentPane(contentArea);
start(); //start thread running

}


class ClockPanel extends JPanel
{
public void paintComponent(Graphics painter )
{
Image pic =
Toolkit.getDefaultToolkit().getImage("background.jpg");

if(pic != null)

painter.drawImage(pic, 0, 0, this); //create image


//if I didn't use a background image I would have used the setColor and fillRect methods to set background

painter.setFont(clockFont); //create clock components
painter.setColor(Color.black);
painter.drawString( timeNow(), 60, 40);


}
}


//get current time
public String timeNow()
{
Calendar now = Calendar.getInstance();
int hrs = now.get(Calendar.HOUR_OF_DAY);
int min = now.get(Calendar.MINUTE);
int sec = now.get(Calendar.SECOND);

String time = zero(hrs)+":"+zero(min)+":"+zero(sec);

return time;
}



public String zero(int num)
{
String number=( num < 10) ? ("0"+num) : (""+num);
return number; //Add leading zero if needed

}


public void start()
{
if(runner == null) runner = new Thread(this);
runner.start();
//method to start thread
}


public void run()
{
while (runner == Thread.currentThread() )
{
repaint();
//define thread task
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Thread failed");
}

}
}

//create main method
public static void main(String [] args)
{
Clock eg = new Clock();
}
}


--------------------

استخدام الكلمة المحجوزة this, this how to use


this
الها اكثر من استخدام

الاستخدام الاول :

تحل مشكلة اسماء المتغيرات المتشابهة

لو كان عندي ميثود كالتالي

كود
public void setName(String name){

this.name=name;

}


هون الميثود بتستقبل متغير اسمه name

لكن موجود عندي متغير تاني من نفس النوع وبيحمل نفس الاسم

لو ما استخدمنا this

حيصير name=name

المترجم الخاص بالجافا حيعتبر انه احنا بنساوي المتغير بنفسه

لكن مع استخدام this ميزنا المتغيريين عن بعض

this.name بتأشر على المتغير المعرف مسبقا في الكلاس

وname بتأشر على المتغير اللي حتستقبله الميثود



الاستخدام الثاني :

استدعاء دوال البناء

لو كان عندي في الكلاس دالة بناء تستقبل متغير من نوع String

كود
public className(String name){

this.name=name;
}



و عملنا دالة بناء ثانية كالتالي

كود
public className(String name,int id){
this(name);
this.id=id;
}


this(name)

حتقوم باستدعاء دالة البناء التي تستقبل متغير من نوع String

وتمرر اليها القيمة name

وطبعا هالشي هذا لتجنب اعادة تكرار الكود

ويجب ان تكون اول جملة في دالة البناء

ولا يمكن استخدامها في استدعاء اكثر من دالة بناء

تحياتي

شرح لكيفيه اعلان الدوال methods في ال java

* الداله تعني اداء وظيفه معينه في البرنامج واما ان ترجع قيمه او لا ترجع قيمه
والصيغه العامه لاعلانها في لغه الجافا :
MethodModifiers Resulttype MethodDeclarator
Throws clause Methodbody
وسنشرح اي واحده علي حده
* اولا ال MethodModifiers :-
وهي بدورها تحدد الaccess control او التحكم في الوصول الي الداله وهي اختياريه والوضع الافتراضي لها هو (private ) ويمكن ان تكون هناك اكثر من
MethodModifiers .
* ثانيا ال ResultType :-
وهي اجباريه , تكون (void ) اذا كانت الدوال لا ترجع شي وتكون احدي ال
primitive data type او ال non primitive data type اذا كانت الداله ترجع شي ,
اما اذا كانت الداله لا ترجع شي (void ) فيجب ان لا تحتوي علي جمله (Return ) , اما اذا كانت الداله ترجع قيم فيجب ان تحتوي علي جمله (Return ).
* ثالثا ال Methoddeclartor :-
هو اسم الداله وبارمتراتها ان وجدت , ويمكن ان تكون هناك اكثر من داله باسم
واحد وهذا يعرف بال(overload ) الذي يحقق تعدديه الاشكال (polymorphism )
في مفاهيم الكائنات الموجهه (object oriented concept ) .
* رابعا ال Throws clause :-
وهي تسمي فقره الاستثناء وهي اختياريه عند الاعلان عن الداله ,وتستخدم
هذه الفقره بتوجيه المنفذ الي عمل محدد عند حدوث خطا اثناء التنفيذ ويعرف
هذا بال (exception handling ).
* خامسا ال Method body :-
ويبدا بالقوس } وتنتهي بالقوس {.