Advertisement
irmantas_radavicius

Untitled

Apr 15th, 2022
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 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.  
  39. class PointList {
  40.     private ArrayList<Point> pList = new ArrayList<>();            
  41.    
  42.     public PointList(File file) throws FileNotFoundException {
  43.         Scanner sc = new Scanner(file);        
  44.         while(sc.hasNextLine()){               
  45.             double x = Double.parseDouble(sc.nextLine());              
  46.             double y = Double.parseDouble(sc.nextLine());
  47.             Point point = new Point(x, y);             
  48.             pList.add(point);
  49.         }
  50.         sc.close();
  51.     }
  52.    
  53.     public void print(){
  54.         for(int i = 0; i < pList.size(); ++i){                 
  55.             Point p = pList.get(i);
  56.             double d = p.getDistance();
  57.             System.out.println(p + " d=" + d);
  58.         }
  59.         System.out.println();      
  60.     }
  61.     public void sort(){
  62.         Collections.sort(pList, new Comparator<Point>(){
  63.                 public int compare(Point p1, Point p2){
  64.                     return Double.compare(p1.getDistance(), p2.getDistance());
  65.                 }
  66.             }
  67.         );
  68.     }
  69.     public void sort(Comparator<Point> cmp){
  70.         Collections.sort(pList, cmp);
  71.     }
  72. }
  73.  
  74. class MyComparator implements Comparator<Point> {
  75.     public int compare(Point p1, Point p2){
  76.         int xCmp = Double.compare(p1.getX(), p2.getX());
  77.         int yCmp = Double.compare(p1.getY(), p2.getY());
  78.         return (xCmp != 0) ? xCmp : yCmp;      
  79.     }
  80. }
  81.  
  82. public class Sandbox {  
  83.        
  84.     public static void main(String[] args) {           
  85.         try {      
  86.        
  87.             PointList pList = new PointList(new File("points.txt"));
  88.  
  89.             pList.sort();              
  90.             pList.print();
  91.            
  92.             Comparator<Point> cmpX = new Comparator<Point>(){
  93.                 public int compare(Point p1, Point p2){
  94.                     return Double.compare(p1.getX(), p2.getX());
  95.                 }
  96.             };         
  97.             pList.sort(cmpX);          
  98.             pList.print();     
  99.  
  100.            
  101.             pList.sort(new Comparator<Point>(){
  102.                     public int compare(Point p1, Point p2){
  103.                         return Double.compare(p1.getY(), p2.getY());
  104.                     }
  105.                 }          
  106.             );
  107.             pList.print();
  108.            
  109.             pList.sort(new MyComparator());        
  110.             pList.print();
  111.            
  112.         } catch(Exception e){          
  113.             e.printStackTrace();           
  114.             System.out.println("Unexpected error, sorry!");
  115.         }          
  116.     }  
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement