import java.util.*; import java.math.*; import java.io.*; class Assignment6 { public static void main(String[] args)throws Exception{ double X1; double Y1; double X2; double Y2; Point P1; Point P2; Vector V; Triangle T1; Rectangle R1; int L1; int L2; int N1; int N2; int N3; int N4; Scanner in = new Scanner(System.in); System.out.println("Please enter a filename:"); String filename = in.nextLine(); File inputFile = new File(filename); Scanner reader = new Scanner(inputFile); ArrayList<Point> storePoint = new ArrayList<Point>(); while ( reader.hasNext()){ X1 = reader.nextDouble(); Y1 = reader.nextDouble(); P1 = new Point(X1,Y1); storePoint.add(P1); } //Points Now Stored In An ArrayList!! Scanner input = new Scanner(System.in); System.out.println("Please enter a filename:"); String filename1 = input.nextLine(); File inputVector = new File(filename1); Scanner VectorInput = new Scanner(inputVector); ArrayList<Vector> storeVector = new ArrayList<Vector>(); while (VectorInput.hasNext()){ L1 = VectorInput.nextInt(); L2 = VectorInput.nextInt(); V = new Vector (storePoint.get(L1),storePoint.get(L2)); storeVector.add(V); } //Vectors Now Stored In An ArrayList!! Scanner input1 = new Scanner(System.in); System.out.println("Please enter a filename:"); String filename2 = input1.nextLine(); File inputTangle = new File(filename2); Scanner TangleInput = new Scanner(inputTangle); ArrayList<Triangle> storeTriangle = new ArrayList<Triangle>(); ArrayList<Rectangle> storeRectangle = new ArrayList<Rectangle>(); while (TangleInput.hasNext()){ } } }

# How to store triangles and rectangles?

Page 1 of 1## 6 Replies - 3343 Views - Last Post: 13 December 2009 - 08:43 PM

### #1

# How to store triangles and rectangles?

Posted 13 December 2009 - 05:27 PM

So far this code reads several Points from a file and stores them in an arraylist. These points are then used to create several vectors using a different file selecting which points to use, which are then stored in an arraylist. The problem is I now need to create 5 rectangles and 5 triangles using those vectors. I am not sure how to go about this. Can anyone guide me in the right direction? Thanks

##
**Replies To:** How to store triangles and rectangles?

### #2

## Re: How to store triangles and rectangles?

Posted 13 December 2009 - 06:35 PM

Are you talking about picking points out of the vectors that will create the shapes or are you talking about making it draw those shapes using the points you stored in the vectors? I guess the real question is, what is the purpose of the vectors? Is each vector a shape that will then needed to be drawn?

### #3

## Re: How to store triangles and rectangles?

Posted 13 December 2009 - 06:41 PM

Firstly thanks for the reply!! This is the lecturer's exact words:-

Once you have written, and hopefully tested the two new classes you can continue. You will be con-

structing these shapes from a data file as before, except that this time there are three files. The names of

the three files will be passed in as parameters to the main method, just like assignment 6. The first one will

contain data for 25 Points, an x and y value on each line, separated by a single space. All values will be

integers.

The second file will contain details of 35 vectors, each vector defined by two integers for which x >= 0

and x < 25. Each value will refer to the points constructed by part one above. Finally the third file will

contain details of five Rectangles, each represented on a separate line of four integers, each representing one

of the vectors constructed in part two above, followed by five Triangles, each represented by three integers

on a separate line, representing the Vectors as before. Your job will be to construct Points from file one

storing them in a suitable data structure, then construct Vectors, then construct and store the Rectangles

and Triangles. Finally to iterate through the collection of shapes, printing out for each one its perimeter

length and area.

I have got as far as the vectors, just actually saving the triangles and rectangles has stumped me as they are both in one file. Thanks

Once you have written, and hopefully tested the two new classes you can continue. You will be con-

structing these shapes from a data file as before, except that this time there are three files. The names of

the three files will be passed in as parameters to the main method, just like assignment 6. The first one will

contain data for 25 Points, an x and y value on each line, separated by a single space. All values will be

integers.

The second file will contain details of 35 vectors, each vector defined by two integers for which x >= 0

and x < 25. Each value will refer to the points constructed by part one above. Finally the third file will

contain details of five Rectangles, each represented on a separate line of four integers, each representing one

of the vectors constructed in part two above, followed by five Triangles, each represented by three integers

on a separate line, representing the Vectors as before. Your job will be to construct Points from file one

storing them in a suitable data structure, then construct Vectors, then construct and store the Rectangles

and Triangles. Finally to iterate through the collection of shapes, printing out for each one its perimeter

length and area.

I have got as far as the vectors, just actually saving the triangles and rectangles has stumped me as they are both in one file. Thanks

### #4

## Re: How to store triangles and rectangles?

Posted 13 December 2009 - 07:31 PM

Nice to see I'm not the only one who left this assignment till last minute. The way I've gotten round it is by using a for loop. With File3, he says the first 5 lines are rectangles and then 5 triangles, so I've scanned the first 5 lines into an array for rectangles and the second 5 into an array for triangles. It's compiled, but I haven't tested it yet. This is my code for the last file, sorry for lack of notes:

Hope this helps, good luck.

Edit: Just realised this makes no sense without notes. "vectors, rectangles, triangles" are all arrays that I declared at the start of the program.

File inputFile3 = new File(filename3); Scanner reader3 = new Scanner(inputFile3); while(reader3.hasNextLine()){ String line3 = reader3.nextLine(); Scanner lineScanner3 = new Scanner(line3); lineScanner3.useDelimiter(" "); while(lineScanner3.hasNext()){ String firstvalue3 = lineScanner3.next(); String secondvalue3 = lineScanner3.next(); String thirdvalue = lineScanner3.next(); String fourthvalue = lineScanner3.next(); int w = Integer.parseInt(firstvalue3); int x = Integer.parseInt(secondvalue3); int y = Integer.parseInt(thirdvalue); int z = Integer.parseInt(fourthvalue); for(int i = 0; i < 5; i++){ Rectangle R = new Rectangle(vectors[w], vectors[x], vectors[y], vectors[z]); rectangles[i] = R; } for(int i = 0; i > 5; i++){ Triangle T = new Triangle(vectors[w], vectors[x], vectors[y]); triangles[i] = T; } Rectangle R1 = rectangles[0]; Rectangle R2 = rectangles[1]; Rectangle R3 = rectangles[2]; Rectangle R4 = rectangles[3]; Rectangle R5 = rectangles[4]; Triangle T1 = triangles[0]; Triangle T2 = triangles[1]; Triangle T3 = triangles[2]; Triangle T4 = triangles[3]; Triangle T5 = triangles[4];

Hope this helps, good luck.

Edit: Just realised this makes no sense without notes. "vectors, rectangles, triangles" are all arrays that I declared at the start of the program.

This post has been edited by **Uprocker**: 13 December 2009 - 07:35 PM

### #5

## Re: How to store triangles and rectangles?

Posted 13 December 2009 - 07:39 PM

LEDGEND!!! After you have tested it could you possibly let me know if it works? Cos it will take a while for me to adapt the code to what I have thanks!!!

### #6

## Re: How to store triangles and rectangles?

Posted 13 December 2009 - 08:11 PM

ok...... Does anyone have any ideas how i can adapt this too my code? I am really stumped. Thanks!

### #7

## Re: How to store triangles and rectangles?

Posted 13 December 2009 - 08:43 PM

It ain't working, and nothing I try fixes it. I'm giving in and sending it in as it is. Sorry bud.

Page 1 of 1