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 مليون عملية
البرنامج أخذ على جهازي نصف ساعه يشتغل :):)
اليوم بندخل في تفاصيل لغة الجافا و نبداء بالبرامج
ماهي الفئة 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 مليون عملية
البرنامج أخذ على جهازي نصف ساعه يشتغل :):)