منتدى مصر التقني
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

تمارين بلغة الجافا ...حساب المسافة بين نقتطين Write the code for the class MyPoint. Also write a test program

اذهب الى الأسفل

تمارين بلغة الجافا ...حساب المسافة بين نقتطين  Write the code for the class MyPoint. Also write a test program Empty تمارين بلغة الجافا ...حساب المسافة بين نقتطين Write the code for the class MyPoint. Also write a test program

مُساهمة من طرف أحمد مناع الخميس مايو 07, 2020 3:07 pm

السؤال :

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.

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
* }


هذا البرنامج يتطلب انشاء بفئة باسم 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);
   }
}


ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
سبحان الله وبحمدة .....سبحان الله العظيم
أحمد مناع
أحمد مناع
.
.

تاريخ التسجيل : 15/02/2011
المساهمات : 1108
النقاط : 202034
التقيم : 144
الدولة : مصر
الجنس : ذكر

https://egy-tech.forumegypt.net

الرجوع الى أعلى الصفحة اذهب الى الأسفل

الرجوع الى أعلى الصفحة

ََ

مواضيع ذات صلة


 
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى