تمارين بلغة الجافا ...حساب المسافة بين نقتطين Write the code for the class MyPoint. Also write a test program
منتدى مصر التقني :: قسم لغات البرمجة Programming languages :: لغة الجافا Java :: نقاش و أستفسارات الاعضاء
صفحة 1 من اصل 1 • شاطر
تمارين بلغة الجافا ...حساب المسافة بين نقتطين Write the code for the class MyPoint. Also write a test program
السؤال :
هذا البرنامج يتطلب انشاء بفئة باسم MyPoint لتعين نقطة على مستوي الاحداثى السينى و الصادي ويتطلب طريقتين لحساب المسافة بين نقتطين ...الاولى لحساب المسافة بين نقطة و اخرى ....والطريقة الثانية لحساب المسافة بين نقطة و احداثيات نقطة اخري هذا فى المطلوب الاول أما المطلبو الثاني يتطلب انشاء مصفوفة نقاط تبدء من النقطة (1,1) وحتى النقطة ( 10,10)
و البرنامج على النحو التالى :
1_Write the code for the class MyPoint. Also write a test program (called TestMyPoint) to test all the methods defined in the class.
The Assignment begins by instructing us to create the MyPoint class that contains:
* Two instance variables x (int) and y (int).
* A "no-argument" (or "no-arg") constructor that construct a point at (0, 0).
* A constructor that constructs a point with the given x and y coordinates.
* Getter and setter for the instance variables x and y.
* A method setXY() to set both x and y.
* A toString() method that returns a string description of the instance in the format "(x, y)".
* A method called distance(int x, int y) that returns the distance from this point to another point at the given (x, y) coordinates.
* An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint instance another.
* Two instance variables x (int) and y (int).
* A "no-argument" (or "no-arg") constructor that construct a point at (0, 0).
* A constructor that constructs a point with the given x and y coordinates.
* Getter and setter for the instance variables x and y.
* A method setXY() to set both x and y.
* A toString() method that returns a string description of the instance in the format "(x, y)".
* A method called distance(int x, int y) that returns the distance from this point to another point at the given (x, y) coordinates.
* An overloaded distance(MyPoint another) that returns the distance from this point to the given MyPoint instance another.
2_2. Write a program that allocates 10 points in an array of MyPoint, and initializes to (1, 1), (2, 2), ... (10, 10).
* Hints: You need to allocate the array, as well as each of the ten MyPoint instances.
* MyPoint[] points = new MyPoint[10]; // Declare and allocate an array of MyPoint
* for (......) {
* points[i] = new MyPoint(...); // Allocate each of MyPoint instances
* }
* Hints: You need to allocate the array, as well as each of the ten MyPoint instances.
* MyPoint[] points = new MyPoint[10]; // Declare and allocate an array of MyPoint
* for (......) {
* points[i] = new MyPoint(...); // Allocate each of MyPoint instances
* }
هذا البرنامج يتطلب انشاء بفئة باسم MyPoint لتعين نقطة على مستوي الاحداثى السينى و الصادي ويتطلب طريقتين لحساب المسافة بين نقتطين ...الاولى لحساب المسافة بين نقطة و اخرى ....والطريقة الثانية لحساب المسافة بين نقطة و احداثيات نقطة اخري هذا فى المطلوب الاول أما المطلبو الثاني يتطلب انشاء مصفوفة نقاط تبدء من النقطة (1,1) وحتى النقطة ( 10,10)
و البرنامج على النحو التالى :
- الكود:
public class MyPoint {
int x , y;
public static void main(String[] args) {
//A
MyPoint p1 = new MyPoint();
System.out.println(p1);
p1.setX(8);
p1.setY(6);
System.out.println("x is :" +p1.getX());
System.out.println("y is :" +p1.getY());
p1.setXY(3, 0);
System.out.println(p1.getXY()[0]);
System.out.println(p1.getXY()[1]);
System.out.println(p1);
MyPoint p2 = new MyPoint(0,4);
System.out.println(p2);
System.out.println(p1.distance(p2));
System.out.println(p2.distance(p1));
System.out.println(p1.distance(5, 6));
System.out.println(p1.distance());
// B
MyPoint[] p3 = new MyPoint[10];
for(int i = 0; i < p3.length; i++) {
p3[i] = new MyPoint(1+i,1+i);
System.out.println(p3[i]);
}
}
public MyPoint(){ this.x = 0; this.y = 0;}
public MyPoint(int x, int y) { this.x = x; this.y = y; }
private int getX(){ return this.x; }
private void setX(int x){ this.x=x; }
private int getY(){return this.y; }
private void setY(int y){this.y=y; }
private int[] getXY() { return new int[] {x, y}; }
private void setXY(int x,int y){ this.x=x;this.y=y; }
@Override
public String toString(){
return String.format("("+ this.x +","+ this.y +")");
}
public double distance() {
int xDiff = this.x ;
int yDiff = this.y ;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
public double distance(MyPoint another) {
int xDiff = this.x - another.x;
int yDiff = this.y - another.y;
return Math.sqrt(xDiff*xDiff + yDiff*yDiff);
}
}
ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
سبحان الله وبحمدة .....سبحان الله العظيم
| |
منتدى مصر التقني :: قسم لغات البرمجة Programming languages :: لغة الجافا Java :: نقاش و أستفسارات الاعضاء
صفحة 1 من اصل 1
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى
الخميس أبريل 25, 2024 8:56 pm من طرف moslema_r
» شركة مكافحة حشرات في راس الخيمة
الخميس أبريل 25, 2024 8:34 pm من طرف moslema_r
» تطبيقIncogniton لإدارة ملفات تعريف متعددة للمتصفح
الأربعاء أبريل 24, 2024 3:51 pm من طرف زاكااا
» افضل شركة مكافحة النمل الابيض بالرياض
الإثنين أبريل 22, 2024 9:41 pm من طرف moslema_r
» Exemplary Сasual Dating - Verified Women
الخميس أبريل 18, 2024 9:18 pm من طرف wdqwd
» رش الدفان بالرياض
الخميس أبريل 18, 2024 7:11 pm من طرف moslema_r
» شركة تنظيف خزانات الشارقة
الخميس أبريل 18, 2024 6:57 pm من طرف moslema_r
» شركة مكافحة الفئران بالرياض
الإثنين أبريل 15, 2024 8:23 pm من طرف moslema_r
» افضل شركة كشف تسربات المياه بالباحة
الخميس أبريل 04, 2024 7:02 am من طرف gmalnagy
» ساعدوني وفهموني help me
الجمعة مارس 29, 2024 8:37 am من طرف linesoft