Advertisement
irmantas_radavicius

Untitled

Apr 15th, 2022
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class Point {
  5.     private double x;
  6.     private double y;
  7.    
  8.     public Point(double x, double y){
  9.         setX(x);
  10.         setY(y);
  11.     }
  12.    
  13.     public void setX(double x){
  14.         this.x = x;
  15.     }
  16.    
  17.     public void setY(double y){
  18.         this.y = y;
  19.     }
  20.    
  21.     public double getX(){
  22.         return this.x;
  23.     }
  24.    
  25.     public double getY(){
  26.         return this.y;
  27.     }
  28.    
  29.     public double getDistance(){
  30.         return Math.sqrt(x*x + y*y);
  31.     }
  32.    
  33.     public String toString(){
  34.         return "" + this.x + " " + this.y;
  35.     }
  36. }
  37.  
  38. public class Sandbox {  
  39.        
  40.     public static void main(String[] args) {           
  41.         try {      
  42.             ArrayList<Point> pList = new ArrayList<>();
  43.            
  44.             Scanner sc = new Scanner(System.in);
  45.            
  46.             int size = 0;
  47.             System.out.print("Enter size: ");
  48.             size = Integer.parseInt(sc.nextLine());
  49.            
  50.             for(int i = 0; i < size; ++i){             
  51.                 System.out.print("Enter x: ");
  52.                 double x = Double.parseDouble(sc.nextLine());
  53.                 System.out.print("Enter y: ");
  54.                 double y = Double.parseDouble(sc.nextLine());
  55.                 Point point = new Point(x, y);
  56.                 pList.add(point);
  57.             }
  58.             sc.close();
  59.            
  60.             for(int i = 0; i < pList.size(); ++i){                 
  61.                 System.out.println("Distance is " + pList.get(i).getDistance());
  62.             }
  63.            
  64.            
  65.         } catch(Exception e){          
  66.             e.printStackTrace();           
  67.             System.out.println("Unexpected error, sorry!");
  68.         }          
  69.     }  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement