Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class Point {
- private double x;
- private double y;
- public Point(double x, double y){
- setX(x);
- setY(y);
- }
- public void setX(double x){
- this.x = x;
- }
- public void setY(double y){
- this.y = y;
- }
- public double getX(){
- return this.x;
- }
- public double getY(){
- return this.y;
- }
- public double getDistance(){
- return Math.sqrt(x*x + y*y);
- }
- public String toString(){
- return "" + this.x + " " + this.y;
- }
- }
- public class Sandbox {
- public static void main(String[] args) {
- try {
- File file = new File("points.txt");
- //FileWriter fw = new FileWriter(file);
- ArrayList<Point> pList = new ArrayList<>();
- Scanner sc = new Scanner(file);
- while(sc.hasNextLine()){
- String line = sc.nextLine();
- int spacePos = line.indexOf(",");
- //System.out.println(line.substring(0, spacePos));
- double x = Double.parseDouble(line.substring(0, spacePos));
- String remStr = line.substring(spacePos+2);
- //System.out.println(remStr.substring(0, spacePos2));
- double y = Double.parseDouble(remStr));
- //System.out.println(line);
- Point point = new Point(3.0, 4.0);
- pList.add(point);
- }
- sc.close();
- /*
- Scanner sc = new Scanner(System.in);
- int size = 0;
- System.out.print("Enter size: ");
- size = Integer.parseInt(sc.nextLine());
- for(int i = 0; i < size; ++i){
- System.out.print("Enter x: ");
- double x = Double.parseDouble(sc.nextLine());
- System.out.print("Enter y: ");
- double y = Double.parseDouble(sc.nextLine());
- Point point = new Point(x, y);
- pList.add(point);
- }
- sc.close();
- */
- for(int i = 0; i < pList.size(); ++i){
- double d = pList.get(i).getDistance();
- System.out.println("Distance is " + d);
- //fw.write(pList.get(i).getX() + " " + pList.get(i).getY() + " " + d + "\n");
- }
- //fw.close();
- } catch(Exception e){
- e.printStackTrace();
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement