Advertisement
irmantas_radavicius

Untitled

Apr 15th, 2022
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 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.            
  43.             File file = new File("points.txt");
  44.            
  45.             //FileWriter fw = new FileWriter(file);
  46.            
  47.             ArrayList<Point> pList = new ArrayList<>();
  48.            
  49.             Scanner sc = new Scanner(file);
  50.             while(sc.hasNextLine()){
  51.                 String line = sc.nextLine();
  52.                 int spacePos = line.indexOf(",");
  53.                 //System.out.println(line.substring(0, spacePos));
  54.                 double x = Double.parseDouble(line.substring(0, spacePos));
  55.                                        
  56.                 String remStr = line.substring(spacePos+2);
  57.                 //System.out.println(remStr.substring(0, spacePos2));
  58.                 double y = Double.parseDouble(remStr));
  59.                
  60.                 //System.out.println(line);
  61.                 Point point = new Point(3.0, 4.0);             
  62.                 pList.add(point);
  63.             }
  64.            
  65.            
  66.             sc.close();
  67.             /*
  68.             Scanner sc = new Scanner(System.in);
  69.             int size = 0;
  70.             System.out.print("Enter size: ");
  71.             size = Integer.parseInt(sc.nextLine());
  72.            
  73.             for(int i = 0; i < size; ++i){         
  74.                 System.out.print("Enter x: ");
  75.                 double x = Double.parseDouble(sc.nextLine());
  76.                 System.out.print("Enter y: ");
  77.                 double y = Double.parseDouble(sc.nextLine());              
  78.                
  79.                 Point point = new Point(x, y);             
  80.                 pList.add(point);
  81.             }
  82.             sc.close();
  83.             */
  84.            
  85.             for(int i = 0; i < pList.size(); ++i){                 
  86.                 double d = pList.get(i).getDistance();
  87.                 System.out.println("Distance is " + d);
  88.                 //fw.write(pList.get(i).getX() + " " + pList.get(i).getY() + " " + d + "\n");
  89.             }
  90.            
  91.            
  92.             //fw.close();
  93.            
  94.         } catch(Exception e){          
  95.             e.printStackTrace();           
  96.             System.out.println("Unexpected error, sorry!");
  97.         }          
  98.     }  
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement