Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package java_lab3;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- /**
- *
- * @author Alex
- */
- class Punto {
- public double X;
- public double Y;
- public Punto(double x, double y)
- {
- this.X = x;
- this.Y = y;
- }
- @Override
- public String toString()
- {
- return "x = " + this.X + " y = " + this.Y;
- }
- }
- class Forma {
- private float R;
- public Forma(float r) {
- if(r < 0)
- {
- throw new IllegalArgumentException("R должно быть неотрицательным");
- }
- else if(Float.isInfinite(r) || Float.isNaN(r))
- {
- throw new IllegalArgumentException("Некорректный ввод R");
- }
- else
- {
- this.R = r;
- }
- }
- public boolean inField(Punto p)
- {
- return ((p.X < 0 && p.Y > 0 && p.X > (-R/2) && p.Y < R) || (p.X > 0 && p.Y > 0
- && (p.X*p.X + p.Y * p.Y) < (R/2)*(R/2)) || (p.X > 0 && p.Y < 0 && (p.X - p.Y) < R));
- }
- }
- public class Java_Lab3 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws IOException {
- Forma MyForm;
- System.out.println("Множество точек: {{3, -5}, {0, 0}, {2, -2}, {1, -2}, {2, 3}, "
- + "{1, -2}, {5, 5}, {-5, -3}, {-5, 3}}");
- List<Punto> MyPoints = new LinkedList<Punto>();
- MyPoints.add(new Punto(3.0,-5.0));
- MyPoints.add(new Punto(0.0,0.0));
- MyPoints.add(new Punto(2.0,-2.0));
- MyPoints.add(new Punto(2.0,3.0));
- MyPoints.add(new Punto(1.0,-2.0));
- MyPoints.add(new Punto(5.0,5.0));
- MyPoints.add(new Punto(-5.0,-3.0));
- MyPoints.add(new Punto(-5.0,3.0));
- boolean flagMenu = true;
- while(flagMenu)
- {
- System.out.print("Чтобы ввести R нажмите y, для выхода из программы q: ");
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String str = stdin.readLine();
- try
- {
- switch(str) {
- case "y":
- MyForm = new Forma(readR());
- for(int i =0; i < MyPoints.size(); i++)
- {
- if(MyForm.inField(MyPoints.get(i)))
- {
- System.out.println("Точка " + MyPoints.get(i).toString() + " входит в область");
- }
- else
- {
- System.out.println("Точка " + MyPoints.get(i).toString() + " не входит в область");
- }
- }
- break;
- case "q":
- flagMenu = false;
- break;
- default:
- System.out.println("Некорректный ввод! Попробуйте еще раз");
- }
- }
- catch(IllegalArgumentException e)
- {
- System.out.println("Ошибка: " + e.getMessage());
- System.out.print("Для выхода введите q, для продолжения любую другую комбинацию символов: ");
- BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
- String data = inp.readLine();
- if("q".equals(data))
- {
- flagMenu = false;
- }
- }
- }
- }
- public static float readR() throws IOException
- {
- System.out.print("Введите R: ");
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String str = stdin.readLine();
- return new Float(str);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement