Advertisement
Slapoguzov

PIP_Lab3

Oct 15th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package java_lab3;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStreamReader;
  12. import java.util.*;
  13.  
  14. /**
  15.  *
  16.  * @author Alex
  17.  */
  18.  
  19. class Punto {
  20.    
  21.     public double X;
  22.     public double Y;
  23.    
  24.     public Punto(double x, double y)
  25.     {
  26.         this.X = x;
  27.         this.Y = y;
  28.     }
  29.    
  30.     @Override
  31.     public String toString()
  32.     {
  33.         return "x = " + this.X + " y = " + this.Y;
  34.                
  35.     }
  36.    
  37. }
  38.  
  39. class Forma {
  40.    
  41.     private float R;
  42.    
  43.     public Forma(float r) {
  44.        
  45.         if(r < 0)
  46.         {
  47.           throw new IllegalArgumentException("R должно быть неотрицательным");  
  48.         }
  49.         else if(Float.isInfinite(r) || Float.isNaN(r))
  50.         {
  51.            throw new IllegalArgumentException("Некорректный ввод R");    
  52.         }
  53.         else
  54.         {
  55.         this.R = r;
  56.         }
  57.     }
  58.    
  59.     public boolean inField(Punto p)
  60.     {
  61.         return ((p.X < 0 && p.Y > 0 && p.X > (-R/2) && p.Y < R) || (p.X > 0 && p.Y > 0
  62.                 && (p.X*p.X + p.Y * p.Y) < (R/2)*(R/2)) || (p.X > 0 && p.Y < 0 && (p.X - p.Y) < R));
  63.     }
  64. }
  65.  
  66. public class Java_Lab3 {
  67.  
  68.     /**
  69.      * @param args the command line arguments
  70.      */
  71.     public static void main(String[] args) throws IOException {
  72.        
  73.         Forma MyForm;
  74.         System.out.println("Множество точек: {{3, -5}, {0, 0}, {2, -2}, {1, -2}, {2, 3}, "
  75.                          + "{1, -2}, {5, 5}, {-5, -3}, {-5, 3}}");
  76.         List<Punto> MyPoints = new LinkedList<Punto>();
  77.        
  78.         MyPoints.add(new Punto(3.0,-5.0));
  79.         MyPoints.add(new Punto(0.0,0.0));
  80.         MyPoints.add(new Punto(2.0,-2.0));
  81.         MyPoints.add(new Punto(2.0,3.0));
  82.         MyPoints.add(new Punto(1.0,-2.0));
  83.         MyPoints.add(new Punto(5.0,5.0));
  84.         MyPoints.add(new Punto(-5.0,-3.0));
  85.         MyPoints.add(new Punto(-5.0,3.0));
  86.        
  87.         boolean flagMenu = true;
  88.        
  89.         while(flagMenu)
  90.         {
  91.             System.out.print("Чтобы ввести R нажмите y, для выхода из программы q: ");
  92.             BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  93.             String str = stdin.readLine();
  94.             try
  95.             {
  96.             switch(str) {
  97.                
  98.                 case "y":
  99.                     MyForm = new Forma(readR());
  100.                     for(int i =0; i < MyPoints.size(); i++)
  101.                     {
  102.                         if(MyForm.inField(MyPoints.get(i)))
  103.                         {
  104.                             System.out.println("Точка " + MyPoints.get(i).toString() + " входит в область");
  105.                         }
  106.                         else
  107.                         {
  108.                             System.out.println("Точка " + MyPoints.get(i).toString() + " не входит в область");
  109.                         }
  110.                     }
  111.                     break;
  112.                 case "q":
  113.                     flagMenu = false;
  114.                     break;
  115.                 default:
  116.                     System.out.println("Некорректный ввод! Попробуйте еще раз");
  117.             }
  118.             }
  119.             catch(IllegalArgumentException e)
  120.             {
  121.                 System.out.println("Ошибка: " + e.getMessage());
  122.                 System.out.print("Для выхода введите q, для продолжения любую другую комбинацию символов: ");
  123.                
  124.                 BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
  125.                 String data = inp.readLine();
  126.                
  127.                 if("q".equals(data))
  128.                 {
  129.                     flagMenu = false;
  130.                 }
  131.             }
  132.         }
  133.        
  134.        
  135.     }
  136.    
  137.     public static float readR() throws IOException
  138.     {
  139.         System.out.print("Введите R: ");
  140.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  141.  
  142.         String str = stdin.readLine();
  143.        
  144.         return new Float(str);
  145.     }
  146.    
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement