2

I am supposed to read an input of 4 coordinates Example: 0 1 1 1 (where 0 and 1 are the X and Y coordinates of the 1st point and 1 and 1 are the coordinates of the second point) How am I supposed to store these integers in an arraylist of type Point. Following is my approach :

  public class Project1 {
     private int m;
     private int n;
     private WeightedQuickUnionUF qu;
     private int[][] grid;
     private ArrayList<Point> connections;

    /**
    * initializes UnionFind structure, grid and connection list
    * @param m
    * @param n
    */
    public Project1(int m, int n){
    m=m;
    n=n;
    for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
     grid = new int[i][j];
    }
   }
 connections = new ArrayList<Point>();
 int size = m*n;
 qu = new WeightedQuickUnionUF(size);

   }

  public void read_input() 
  {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of pairs of connections:  ");
    int no_of_connections = sc.nextInt();
    for(int i=0; i < (no_of_connections * 4); i++){
    Point coordinates = (Point)sc.nextInt();
    connections.add(coordinates);
  }
   sc.close();
 } 
user2892860
  • 35
  • 1
  • 3
  • 1
    What constructors are available for your `Point` class? It would be helpful if there was a `new Point(0, 1, 1, 1);` Then it would be all too easy to just add it to your array list – CubeJockey Sep 23 '15 at 18:41
  • There are no constructors for the Point class – user2892860 Sep 23 '15 at 18:53
  • 1
    You'll have to elaborate on the Point class then. Is it a custom class? Or are you using a library – CubeJockey Sep 23 '15 at 18:55
  • according to your comment below, you're using the Java.awt.Point class. It has *three* constructors, outlined in the documentation: http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html – CubeJockey Sep 23 '15 at 19:14

3 Answers3

3

You can create a custom Object TwoPoint that holds all 4 coordinates. Use java.awt.Point. I will suggest you to use a HashMap<TwoPoint> where the key will be the index of your input and value will be TwoPoint. For example , if you want to retrieve 4th input , just call pointMap.get(4).

import java.awt.Point;
import java.util.HashMap;
import java.util.Scanner;

public class StorePoints
{
    static int  i   = 0;

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        HashMap<Integer, TwoPoints> pointMap = new HashMap<Integer, StorePoints.TwoPoints>();
        StorePoints sp = new StorePoints();
        for (int i = 0; i < 3; i++)
        {
            System.out.println("Enter 4 coordinates: input ="+(i+1) +"out of  3");
            sp.takeInput(sc,pointMap);
        }

        System.out.println(" Enter index of coordinate  you want to see : [1-3]");
        int index=sc.nextInt();
        if(index<1||index>3)
            System.out.println("Wrong index");
        else
        System.out.println("Input index="+index+", "+pointMap.get(index-1).toString());
        sc.close();
    }

    public void takeInput(Scanner sc,HashMap<Integer, TwoPoints> pointMap)
    {
        int int1=sc.nextInt();int int2= sc.nextInt();int int3= sc.nextInt();int int4= sc.nextInt();
        TwoPoints tp=new TwoPoints(int1,int2,int3,int4);
        pointMap.put(i++, tp);
    }

    class TwoPoints
    {

        Point   p1;
        Point   p2;

        public TwoPoints(int int1, int int2, int int3, int int4)
        {
            p1 = new Point(int1, int2);
            p2 = new Point(int3, int4);
        }

        // getters and setters
        public Point getP1()
        {
            return p1;
        }

        public void setP1(Point p1)
        {
            this.p1 = p1;
        }

        public Point getP2()
        {
            return p2;
        }

        public void setP2(Point p2)
        {
            this.p2 = p2;
        }

        public String toString()
        {
            return "(" + p1 + "," + p2 + ")";
        }
    }
}

Sample output

Enter 4 coordinates: input =1out of  3
1 1 2 3
Enter 4 coordinates: input =2out of  3
2 2 3 4
Enter 4 coordinates: input =3out of  3
2 2 4 5
 Enter coordinate number you want to see : [1-3]
1
Input index=1, (java.awt.Point[x=1,y=1],java.awt.Point[x=2,y=3])
App Work
  • 21,899
  • 5
  • 25
  • 38
2

As AppWork Suggested , use java.util.HashMap to store 4 coordinates. You can sort the map using Comparator if you need. Using java.util.ArrayList you cannot ensure tracking the coordinates latter.

Ther is no easy way to sort HashMap. An alternative can be to get the keys set and sort it.

Set<Integer> keySet =pointMap.keySet();
List <Integer> keyList=new ArrayList<Integer>(keySet);
Collections.sort(keyList);

Now you can iterate the HashMap using this keyList. Since the keys are integer, No need to use Comoparator. If the keys were String, a Comparator could be used instead.

Amar boi
  • 90
  • 5
1

You can't just cast int to Point like that.

If your Point class has a constructor Point(int x, int y), then you can read both coordinates from the Scanner and create Point from them:

int x = sc.nextInt();
int y = sc.nextInt();
Point coordinates = new Point(x, y);

You will need to modify your loop accordingly, so that you read the correct number of ints from the input.

Cinnam
  • 1,892
  • 1
  • 15
  • 23
  • What do you mean by the Point class ? – user2892860 Sep 23 '15 at 18:59
  • @user Cinnam is referring to the Java Point class http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html – CubeJockey Sep 23 '15 at 19:01
  • Where would I declare my point class ? Can you give an example ? – user2892860 Sep 23 '15 at 19:03
  • 1
    @user2892860 You are using it in your code - don't you have it declared already? Or are you using `java.awt.Point` as Trobbins mentioned? – Cinnam Sep 23 '15 at 19:04
  • Where am I using it ? I am using java.awt.Point – user2892860 Sep 23 '15 at 19:07
  • @user2892860 Ok, then it has the constructor that I used in my answer, you can use it in your code like that. – Cinnam Sep 23 '15 at 19:08
  • 1
    @Cinnam Point coordinates = new Point(sc.nextInt(), sc.nextInt()); might not work - I don't think there is any guarantee of the order of evaluation of the arguments in the calling sequence. – FredK Sep 23 '15 at 19:13
  • 1
    @FredK I didn't think of that, thanks. According to http://stackoverflow.com/questions/2201688/order-of-execution-of-parameters-guarantees-in-java it should be evaluated left to right, but it's not recommended to rely on that. I'll edit it. – Cinnam Sep 23 '15 at 19:18