The objective of this assignment is to refresh your knowledge of how to solve simple
programming problems involving loops, arrays and methods.
Your task is manage a collection of rectangles that conceptually represent windows on a computer screen.
A rectangle is represented by the coordinates of its upper left-hand corner and its bottom right-hand corner as shown:
You will be provided an input file that contains the coordinates of the rectangles.
Each line in the file represents a rectangle in the form,
(a, b, c, d)
where
a, b are the x and y coordinates of the upper left-hand corner of the rectangle and
c, d are the x and y coordinates of the lower right-hand corner of the rectangle.
The rectangle shown above is represented by the tuple (50,50,100,100).
The methods you need to write are the following:
- public int[][] read(String fileName)
Returns a 2D array with the rectangle coordinates. It reads lines from the file, with each line representing the coordinates for a rectangle in comma-delimited format.
Here's an example of two rectangles, the first of which was shown above:
50,50,100,100
75,75,150,150
The first line represents the first rectangle and the second line represents the second rectangle.
Save each line as a row in an array, such that the first line can be saved as,
coords[0][0] = 50
coords[0][1] = 50
coords[0][2] = 100
coords[0][3] = 100
Similarly, the second line will go to the second row. If there were more, they would go to the subsequent rows of the array.
Since you will be reading an input file it is a good practice to implement the part of the code within a try/catch block. For example,
try{
Scanner scan = new Scanner(new File(fileName));
..
..
}catch (IOException x) {
System.err.format("%s does not exist",x.getMessage());
}
Note:You don't know ahead of time how many rectangles will be represent in the file, so before declaring your 2D array, you will need to check how many rectangles are there.
At any point in your code you can display a collection of rectangles using the show class as:
new show(coords)
Note: If you are not satisfied with the colors of the rectangles, run the code again and different colors will show up.
- public boolean overlap(int[][] coords, int i, int j) returns true if rectangles at indices i and j of the 2D array coords overlap, false otherwise. Each rectangle is represented as a row of a 2D array, for e.g., if i=0 and j=1, it means, rows 0 and 1 of the coordinates array. You are encouraged to try multiple different values of i and j to test your code.
- public boolean containedIn(int[][] coords,int i, int j) returns true if rectangle at index j of the 2D array coords is contained in the rectangle at index i of the array, false otherwise. Pick multiple different values of i and j to test your code.
- public void drag(int[][] coords,int i, int x,int y) modifies the coordinates of the rectangle at index i moving such that it is centered at coordinates (x,y).
For example, given the following coordinates array:
25, 55, 105, 100
60, 70, 200, 220
10, 35, 30, 100
If i=1, x =100 and y=120, you need to find the new corners and update the second row of the coordinates array which is
60 70 200 220. with those new corners.
- public int[][] resize(int[][] coords, int i, int x,int y) returns a modified 2D coords array. Your goal is to take the rectangle at index i of the input coords array and modify it such that new lower right coordinates of that rectangle are located at x and y. For e.g., if i=2 and the row at index 2 of the coords array is
50, 40, 150, 75
with x = 80 and y = 150, the row above would become
50,40,80,150
Modify row at index 2 in coords array and return it. Test your code with multiple different values for i, x and y.
- main() calls the appropriate methods within the assign1 class.
Use the following
file as a starting point. All your code should be written within the class
Assignment1.
To help you debug your code, we have provided you with code that displays a collection of rectangles.
The Java code contains an example on how to use it (see the
example method in the code).
And here's also an example
coordinates file.
In addition to testing your code using this file, you are encouraged to try your own examples.
Submitting Your Assignment
Submit your Assignment1.java using the
checkin program.
In recitation you will practice trial submission.
Note that your code should compile on department Linux machines.
Make sure your code compiles, and that you are submitting the source code rather than the Java class file.