المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : دورة الجافا: الفئة Class و الشيء Object


M.Alkahtani
11/2/2006, 04:47
بسم الله الرحمن الرحيم
اليوم بندخل في تفاصيل لغة الجافا و نبداء بالبرامج

ماهي الفئة Class؟
الفئة عبارة عن بيانات Data و طرق Method تعمل على هذه البيانات
المثال التالي عبارة عن بيانات Point التي هي عباره عن متغييرين x,y لأن النقطة نرمز لها بالإحداثي السيني x و الصادي y فهذين الأحدثيين يعطوننا نقطة معينة
و عملية على هذه البيانات وهو distanceFromOrigin البعد من نقطة الأصل(0,0) و يعطى عن طريق X<sup>2</sup> * y<sup>2</sup>√ الجذر التربيعي لمضروب المحور السيني تربيع في الصادي تربيع.
نلاحظ في هذا المثال المنشيء للفئة (Constructer) وهو يعطي النقطتين قيمهما
http://telecom.alhilalclub.com/Lessons/Java/3/Point.png
إنشاء شيء (Object)
يتم عن طريق أعطاء متغير نوع البيانات المستخدم في الفئة كمثال
Point P;
أنشاء نقطة مع إعطائها قيمة معينة
<p dir="ltr">Point p = new Point(2.0, -3.5);</p>
لاحظ كلمة new دائماً تستخدم عن إنشاء شيء جديد!

إستخدام الشيء
ناقشنا أنشاء الشي و الأن نناقش القراءة و التعديل و أستخدام دوال الفئة.
<p dir="ltr">double x = p.x;</p> لقراءة العنصر x من النقطة P
<p dir="ltr">p.y = p.x * p.x;</p> لإعطاء العنصر y في النقطة P القيمة التالية
<p dir="ltr">double d = p.distanceFromOrigin( );</p> لإستخدام الطريقة distanceFromOrigin من الفئة Point أو لحساب بعد النقطة عن المركز (0,0)

مثال آخر لنفرض أننا نريد كتابة برنامج لحساب محيط و مساحة الدائرة فالبرنامج التالي يعتبر منطقياً
http://telecom.alhilalclub.com/Lessons/Java/3/Circle.png
الدائرة تعرف بنقطة المركز و نصف القطر لاحظ أننا أستخدمنا البرنامج السابق Point عن طريق الكلمة extends فمعنها أن هذا البرنامج يعتمد على الذي قبله و لاحظ أن المنشيء لبرنامج الدائرة أستخدم منشيء برنامج النقطة عن طريق الأمر super

هل مللت هذه البرامج السهله المكرره تعال معي الى التحدي :):)
الموضوع التالي يحتوي على لغز رائع من انشتاين
http://www.alhilalclub.com/vb/showthread.php?t=75539
السؤال هو كيف نعمل برنامج لحل هذا الغز؟
طبعاً عندنا 5 بيوت (5 مصفوفات) و 5 أشياء متعلقة بالبيوت (5 مصفوفات) معناته راح ننشيء مصفوفه ثنائية الأبعاد مصفوفة داخل مصفوفة كل مصفوفة تتكون من خمس عناصر
و نعمل عليها عدة مقارنات كم في البرنامج التالي
class Einstein {
public static final int nofHouses = 5;
public static final int nofProperties = 5;

public static final int NATIONALITY = 0;
public static final int COLORS = 1;
public static final int CIGAR = 2;
public static final int PET = 3;
public static final int DRINK = 4;

static class State {
final String[][] houses;

State (String[][] houses) {
this.houses = houses;
}
}

public static void checkAndPrint(String[][] houses) throws Exception {

String[][] houses1 = new String[nofHouses][nofProperties];
for (int i=0; i<nofHouses; i++) {
for (int j=0; j<nofProperties; j++) {
houses1[i][j] = houses[i][j];
}
}

State s = new State (houses1);

condition9(s);
condition13(s);
condition4(s);
condition7(s);
condition1(s);
condition2(s);
condition3(s);
condition5(s);
condition6(s);
condition8(s);
condition14(s);
condition12(s);
condition10(s);
condition11(s);
condition15(s);

int iHouse = iHouseWithFish(s);

System.out.println("The owner of a fish is " + owner(s, iHouse));

for (int i=0; i<nofHouses; i++) {
String out = "House " + (i+1) + ": ";
for (int j=0; j<nofProperties; j++) {
out+=(s.houses[i][j]+" ");
}
System.out.println(out);
}
}

public static String owner(State s, int iHouse) {
return s.houses[iHouse][NATIONALITY];
}

// 1. The Brit lives in a red house.

public static void condition1(State s) throws Exception {
inOneHouse(s, "Brit", "Red");
}

// 2. The Swede keeps dogs as pets.

public static void condition2(State s) throws Exception {
inOneHouse(s, "Swede", "Dog");
}

// 3. The Irish drinks tea.

public static void condition3(State s) throws Exception {
inOneHouse(s, "Irish", "Tea");
}

// 4. The green house is on the left of the white house.

public static void condition4(State s) throws Exception {
for (int i=0; i<nofHouses-1; i++) {
if (s.houses[i] [COLORS]=="Green") {
s.houses[i] [COLORS]= "Green";
if (s.houses[i+1][COLORS]=="White") {
s.houses[i+1][COLORS]= "White";
return;
}}
}
throw new Exception();
}

// 5. The green house owner drinks coffee.

public static void condition5(State s) throws Exception {
inOneHouse(s, "Green", "Coffee");
}

// 6. The person who smokes Pall Mall rears birds.

public static void condition6(State s) throws Exception {
inOneHouse(s, "PallMall", "Bird");
}

// 7. The man living in the house right in the centre drinks milk.

public static void condition7(State s) throws Exception {
if (s.houses[(nofHouses-1)/2][DRINK]=="Milk")
{ s.houses[(nofHouses-1)/2][DRINK]= "Milk";
return;
}
throw new Exception();
}

// 8. The owner of the yellow house smokes Dunhill.

public static void condition8(State s) throws Exception {
inOneHouse(s, "Yellow", "Dunhill");
}

// 9. The Norwegian lives in the first house.

public static void condition9(State s) throws Exception {
if (s.houses[0][NATIONALITY]=="Norwegian")
{ s.houses[0][NATIONALITY]= "Norwegian";
return;
}
throw new Exception();
}

// 10. The man who smokes Blend lives next to the one who keeps cats.

public static void condition10(State s) throws Exception {
inNextHouse(s, "Blend", "Cat");
}

// 11. The man who keeps horses lives next to the man who smokes Dunhill.

public static void condition11(State s) throws Exception {
inNextHouse(s, "Dunhill", "Horse");
}

// 12. The owner who smokes Blue Master drinks beer.

public static void condition12(State s) throws Exception {
inOneHouse(s, "BlueMaster", "Beer");
}

// 13. The Norwegian lives next to the blue house.

public static void condition13(State s) throws Exception {
inNextHouse(s, "Norwegian", "Blue");
}

// 14. The German smokes Prince.

public static void condition14(State s) throws Exception {
inOneHouse(s, "German", "Prince");
}

// 15. The man who smokes Blend has a neighbour who drinks water

public static void condition15(State s) throws Exception {
inNextHouse(s, "Blend", "Water");
}

// Find the number of the house with a fish

public static int iHouseWithFish(State s) throws Exception {
for (int i=0; i<nofHouses; i++) {
if (s.houses[i][PET]=="Fish")
{ s.houses[i][PET]= "Fish";
return i;
}
}
throw new Exception();
}

public static void inOneHouse(State s, String str1, String str2) throws Exception {
for (int i=0; i<nofHouses; i++) {
if (s.houses[i][index(str1)]==str1) {
s.houses[i][index(str1)]= str1;
if (s.houses[i][index(str2)]==str2) {
s.houses[i][index(str2)]= str2;
return;
}}
}
throw new Exception();
}

public static void inNextHouse(State s, String str1, String str2) throws Exception {
for (int i=0; i<nofHouses-1; i++) {
jscp_joinLabeled: {
if (s.houses[i] [index(str1)]==str1) {
s.houses[i] [index(str1)]= str1;
if (s.houses[i+1][index(str2)]==str2) {
s.houses[i+1][index(str2)]= str2;
return;
}}
}
if (s.houses[i+1][index(str1)]==str1) {
s.houses[i+1][index(str1)]= str1;
if (s.houses[i] [index(str2)]==str2) {
s.houses[i] [index(str2)]= str2;
return;
}}
}
throw new Exception();
}

public static int index(String str) throws Exception {
if(str=="Norwegian" ||
str=="Irish" ||
str=="Brit" ||
str=="German" ||
str=="Swede" ) return NATIONALITY;

if(str=="Yellow" ||
str=="Blue" ||
str=="Red" ||
str=="Green" ||
str=="White" ) return COLORS;

if(str=="Dunhill" ||
str=="Blend" ||
str=="PallMall" ||
str=="Prince" ||
str=="BlueMaster" ) return CIGAR;

if(str=="Cat" ||
str=="Horse" ||
str=="Bird" ||
str=="Fish" ||
str=="Dog" ) return PET;

if(str=="Water" ||
str=="Tea" ||
str=="Milk" ||
str=="Coffee" ||
str=="Beer" ) return DRINK;

throw new Exception();
}

public static void main (String args[]) throws Exception {
String[][] houses = {
{"Norwegian", "Yellow", "Dunhill", "Cat", "Water" },
{"Irish", "Blue", "Blend", "Horse", "Tea" },
{"Brit", "Red", "PallMall", "Bird", "Milk" },
{"German", "Green", "Prince", "Fish", "Coffee"},
{"Swede", "White", "BlueMaster", "Dog", "Beer" }
};
checkAndPrint (houses);
}
}
ملاحظة البرنامج فيه خطأ منطقي :):)
الي بيعطيني برنامج يحل هالخطأ له جائزة
معلومة : 5^5 = 3125 مجموع الأحتمات تتقلص الى 51 أحتمال بسب الجمل المنطقية
لإختبار جميع الإحتمالات (51, 5) = 2349060 و 5! = 120 أو بما يعني أننا سنختبر
120 * 2349060 = 281887200 ≈ 282 مليون عملية

البرنامج أخذ على جهازي نصف ساعه يشتغل :):)

أحــب الـشـلـ 10 ـهـوب
11/2/2006, 17:51
يعطيك العافيه على اللي تسويه !!

™ мя.∂ήαž
11/2/2006, 19:37
يــامال العافيه ,,

ما شاءالله عليك عيني عليك باردة :yuupy:

البرق سامي
12/2/2006, 00:07
يعطيك العافيه


لو تعطينا مقدمه عن الموضع هذا أو تمهيد !!

M.Alkahtani
12/2/2006, 00:40
يعطيك العافيه


لو تعطينا مقدمه عن الموضع هذا أو تمهيد !!
شكلك ما تابعت المواضيع السابقة
http://www.alhilalclub.com/vb/showthread.php?t=74684
http://www.alhilalclub.com/vb/showthread.php?t=75479

ذيب آسيا
12/2/2006, 01:48
يامال العافية والله يامشرفنا :)

بالنسبة لأول مثالين ... كانوا واضحين ... ويناسبون المرحلة اللي وصلت لها من الشرح ... بس بصراحة لغز اينشتاين جاااامد :jhgy: ... وخصوصاً انه تم استخدام بعض الأشياء اللي ماشرحت من قبل مثل الـ 2D arrays .


حاولت أتتبع البرنامج .. بس فيه شي مافهمته :req:

تكرر مصطلح throws Exception كثييير بهالبرنامج ... وبصراحة مافهمت وش المقصود فيه ولا الهدف منه ... ياليت توضح لي :)


تحيتي لك ياغالي :noo:

M.Alkahtani
12/2/2006, 03:20
throws Exception تعني أن الجملة البرمجية تمر على إحتمالات الخطأ

فمثلاً هذا البرنامج البسيط يقوم بقسمة عددين
import java.io.*;

class ExceptionGeneratorApp {



public static InputStreamReader input = new InputStreamReader(System.in);
public static BufferedReader keyboardInput = new BufferedReader(input);



/* Main method */

public static void main(String[] args) throws IOException {

// Input two integers

int input1 = new Integer(keyboardInput.readLine()).intValue();
int input2 = new Integer(keyboardInput.readLine()).intValue();

// Divide first by second and output result

System.out.println("Division = " + input1/input2);

}
}

جرب البرنامج بدون throws IOException ستجد أن البرنامج قد لا يعمل عند قسمة عدد معين على صفر لأنه غير معرف الناتج
لكن بوجود throws IOException سيطبع لك البرنامج هذه الرسالة
المدخلات في المره الأولى صحيحة 4 و2 و الناتج 2
المدخلات في المرة الثانية خاطئة 4 و0 فظهرت لنا رسالة الخطاء

$ java ExceptionGeneratorApp
4
2
Division = 2

$ java ExceptionGeneratorApp
4
0
java.lang.ArithmeticException: / by zero
at ExceptionGeneratorApp.main(ExceptionGeneratorApp.java:30)

ذيب آسيا
12/2/2006, 04:35
اهاا ... طيب معليش بس سؤال ثاني :kello:

وش فرقها عن الـ try و الـ catch ؟!

ومن جد مشكوووورر على جهدك الرائع :)

هلالي اكاديمي
12/2/2006, 07:58
ا^

^

^

التراي والكاتش يكونوا لكود معين ,,
مثلا ابي الــ Exception حق كود معين ,,

أحط الكود بين Try و Catch ,,,,
و بيصلح Throw للكود المختار ,,

تسسسسسلم مكسا ما تقصر وربي ,, :love: :yes:

ذيب آسيا
12/2/2006, 22:02
عوافي يالأكاديمي :)

وبما إنك خريج ICS الا شوي ... طلع لنا الخطأ المنطقي بالبرنامج <<< وهقه :d:

هلالي اكاديمي
13/2/2006, 02:05
^

^

^

أخلص الريبورت حقي و أكتشف الخطأ المنطقي ,, :) ----> خليني أقابلك و نتفاهم ,, :zael: :fghty:

يعني يبي لي حول الاسبوع لين أخلص من شغلات التطبيق ووجع الراس (himo) ,,

أسطورة الهلال
13/2/2006, 02:40
يعطيك العاااافية ............

اخونا ماكســا ..............

طريقة حلوة وسهلة .......

هلالي اكاديمي
16/2/2006, 15:30
عرفت الخطأ مكسا ..

اللحين المفروض تشيل الاختيار الصح اذا لقى الاجابة ,,
وبكذا تقلل عدد اللوب ,,
ويقل الكمبايليشن تايم ,, :jhgy:

أحد فهم ؟؟؟ أو أشرحه مره ثانيه,, :amle:

M.Alkahtani
16/2/2006, 23:45
عرفت الخطأ مكسا ..

اللحين المفروض تشيل الاختيار الصح اذا لقى الاجابة ,,
وبكذا تقلل عدد اللوب ,,
ويقل الكمبايليشن تايم ,, :jhgy:

أحد فهم ؟؟؟ أو أشرحه مره ثانيه,, :amle:
لا مهوب هذا الخطأ الخطأ في منطق البرنامج ما يعطيك الحلول كاملة يعطيك حل واحد بس إذا تبي الحلول كامله بيأخذ 30 دقيقة عشان يشتغل البرنامج :):)

abo-sagr
17/2/2006, 03:27
عــــــــوافي حبــــــــيبي ..

وشكــراً لك

هلالي اكاديمي
17/2/2006, 06:41
الغالي مكسا ...
اللحين المشكله كلها ان البرنامج يحتاج وقت كبير علشان يطلع الناتج .. صح
وتبي طريقه تختصر فيها هالوقت ,, :rolleyes:

المشكله كلها يعني كمبايليشن تايم :confused:

انا بأحاول أشغل الكود عندي وأعرف وش مشكلته ..
وبرنجع لأيام الــ JCreater ..... :d:

والله زماااااان عنه , :) :cool:

I b r a a
17/2/2006, 13:11
يعطيك العافية حبيبي

Hard
17/2/2006, 18:06
الله يعطيك العافية يامكسا

M.Alkahtani
17/2/2006, 22:16
الغالي مكسا ...
اللحين المشكله كلها ان البرنامج يحتاج وقت كبير علشان يطلع الناتج .. صح
وتبي طريقه تختصر فيها هالوقت ,, :rolleyes:

المشكله كلها يعني كمبايليشن تايم :confused:

انا بأحاول أشغل الكود عندي وأعرف وش مشكلته ..
وبرنجع لأيام الــ JCreater ..... :d:

والله زماااااان عنه , :) :cool:

لا هالبرنامج ما يأخذ وقت هذا حبيب :):) ما يأخذ ولا ثانية
المشكلة فيه أن منطقه غلط ما يعطي الحلول الكاملة للغز فهمت قصدي

هلالي اكاديمي
18/2/2006, 07:15
فهمت عليك ,,
انا بالالغاز رايح في فيهااا ,, :jhgy:
بس البرامج خذ ولد :d: (himo)