Skip to content

Commit e6c581e

Browse files
committed
Added mmn13 solution
1 parent 8685cbb commit e6c581e

2 files changed

Lines changed: 365 additions & 0 deletions

File tree

ממנים/ממ13/Collection.java

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/**
2+
* A class that represent a collection of Box3D.
3+
* Boxes will be add through addBox function, which will also take care of the order.
4+
*
5+
* @author Ben Brandes
6+
* @version 13.04.19
7+
*/
8+
public class Collection {
9+
private Box3D[] _boxes;
10+
private int _noOfBoxes;
11+
final int MAX_NUM_BOXES = 100;
12+
final int INITIAL_VAL = 0;
13+
14+
/**
15+
* Constructor will initialize the array to the default size (100)
16+
* And set the noOfBoxes to 0
17+
*/
18+
public Collection() {
19+
_boxes = new Box3D[MAX_NUM_BOXES];
20+
_noOfBoxes = INITIAL_VAL;
21+
}
22+
23+
/**
24+
* Will add a box to the array and return true if there is place for another box and false for failure
25+
*
26+
* @param base Point3D base for the Box
27+
* @param length Box's length
28+
* @param width Box's width
29+
* @param height Box's height
30+
* @return true - if the box added successfully, false - if the array is full
31+
*/
32+
public boolean addBox(Point3D base, int length, int width, int height) {
33+
if (_noOfBoxes == MAX_NUM_BOXES) {
34+
return false;
35+
} else {
36+
Box3D newBox = new Box3D(base, length, width, height);
37+
int index = this.findSuitableIndex(newBox); // Passing the box to private method that'll check for the right location
38+
this.freeIndex(index); // After finding the right location, move all the boxes to the right
39+
_boxes[index] = new Box3D(newBox); // Assign the new box to it's right location
40+
_noOfBoxes++; // Increase the number of boxes
41+
return true;
42+
}
43+
}
44+
45+
/**
46+
* Create an array that contain all the boxes, the array size will be the number of boxes
47+
*
48+
* @return An array with a size of the number of boxes
49+
*/
50+
public Box3D[] getBoxes() {
51+
Box3D[] all = new Box3D[_noOfBoxes];
52+
53+
for (int i = 0; i < _noOfBoxes; i++) {
54+
all[i] = new Box3D(_boxes[i]);
55+
}
56+
57+
return all;
58+
}
59+
60+
/**
61+
* Return the total number of the boxes in the array
62+
*
63+
* @return the total number of the boxes in the array
64+
*/
65+
public int getNumOfBoxes() {
66+
return _noOfBoxes;
67+
}
68+
69+
/**
70+
* Checks how many boxes in the array can contain the parameter box
71+
*
72+
* @param box the box that will be checked
73+
* @return how many boxes in the array can contain the parameter box
74+
*/
75+
public int howManyContains(Box3D box) {
76+
int count = 0;
77+
78+
for (int i = 0; i < _noOfBoxes; i++) {
79+
if (_boxes[i].contains(box))
80+
count++;
81+
}
82+
83+
return count;
84+
}
85+
86+
/**
87+
* Return the longest distance between two boxes in the array
88+
*
89+
* @return the longest distance between two boxes in the array
90+
*/
91+
public double longestDistance() {
92+
if(_noOfBoxes < 2)
93+
return 0;
94+
95+
double distance = 0;
96+
97+
for (int i = 0; i < _noOfBoxes; i++) {
98+
for (int j = 0; j < _noOfBoxes; j++) {
99+
double d = _boxes[i].distance(_boxes[j]);
100+
if (d > distance)
101+
distance = d;
102+
}
103+
}
104+
105+
return distance;
106+
}
107+
108+
/**
109+
* Return the box the highest base point
110+
* If the array is empty, will return <code>null</code>
111+
*
112+
* @return The box with the highest base point, if empty will return <code>null</code>
113+
*/
114+
public Box3D mostUpperBaseCorner() {
115+
if (_noOfBoxes == 0)
116+
return null;
117+
118+
Box3D highest = new Box3D();
119+
for (int i = 0; i < _noOfBoxes; i++) {
120+
if (_boxes[i].getBase().isAbove(highest.getBase())) {
121+
highest = _boxes[i];
122+
}
123+
}
124+
125+
return new Box3D(highest);
126+
}
127+
128+
/**
129+
* Return a string that'll display all the boxes
130+
*
131+
* @return a string that'll display all the boxes
132+
*/
133+
public String toString() {
134+
String boxes = "";
135+
for (int i = 0; i < _noOfBoxes; i++) {
136+
boxes += "Box no. " + (i + 1) + ": " + _boxes[i].toString() + "\n";
137+
}
138+
139+
return boxes;
140+
}
141+
142+
/**
143+
* Return the total of the surface area of all the boxes in the array
144+
*
145+
* @return the total of the surface area of all the boxes in the array
146+
*/
147+
public double totalSurfaceArea() {
148+
double area = 0;
149+
150+
for (int i = 0; i < _noOfBoxes; i++) {
151+
area += _boxes[i].getSurfaceArea();
152+
}
153+
154+
return area;
155+
}
156+
157+
/**
158+
* Will check all the boxes within this index range and
159+
* return the volume of the smallest box that can contain all those boxes.
160+
*
161+
* @param i index within the array
162+
* @param j index within the array
163+
* @return return the volume of the smallest box that can contain all those boxes
164+
*/
165+
public int volumeOfSmallestBox(int i, int j) {
166+
// Checks if the indexes are valid
167+
if (i >= MAX_NUM_BOXES || j >= MAX_NUM_BOXES || i < 0 || j < 0 || j >= _noOfBoxes || i >= _noOfBoxes)
168+
return 0;
169+
170+
int min = (i > j) ? i : j;
171+
int max = (j > i) ? j : i;
172+
173+
int maxH = 0, maxW = 0, maxL = 0;
174+
175+
for (int l = min; l <= max; l++) {
176+
if (_boxes[l].getHeight() >= maxH)
177+
maxH = _boxes[l].getHeight() + 1;
178+
if (_boxes[l].getLength() >= maxL)
179+
maxL = _boxes[l].getLength() + 1;
180+
if (_boxes[l].getWidth() >= maxW)
181+
maxW = _boxes[l].getWidth() + 1;
182+
}
183+
184+
return maxH * maxL * maxW;
185+
}
186+
187+
// Return the index of the best place for the new box
188+
private int findSuitableIndex(Box3D box) {
189+
for (int i = 0; i < _noOfBoxes; i++) {
190+
if (_boxes[i].getVolume() >= box.getVolume())
191+
return i;
192+
}
193+
return _noOfBoxes;
194+
}
195+
196+
// Will move all the boxes next to the index parameter 1 place right
197+
private void freeIndex(int index) {
198+
for (int i = _noOfBoxes; i > index; i--) {
199+
_boxes[i] = new Box3D(_boxes[i - 1]);
200+
}
201+
}
202+
}

ממנים/ממ13/Matrix.java

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* A class that using a 2D array to simulate a Matrix and alter it's cells
3+
*
4+
* @author Ben Brandes
5+
* @version 13.04.19
6+
*/
7+
public class Matrix {
8+
final int MAX = 255;
9+
private int[][] _array;
10+
11+
/**
12+
* Constructs a Matrix from a two-dimensional array, the dimensions as well as the
13+
* values of the this Matrix will be the same as the dimensions and the values of the
14+
* two-dimensional array.
15+
*
16+
* @param array the array to be set
17+
*/
18+
public Matrix(int[][] array) {
19+
_array = new int[array.length][];
20+
copyArray(_array, array);
21+
}
22+
23+
24+
/**
25+
* Constructs a size1 by size2 Matrix of zeros.
26+
*
27+
* @param size1 represents the columns
28+
* @param size2 represents the rows
29+
*/
30+
public Matrix(int size1, int size2) {
31+
_array = new int[size1][size2];
32+
}
33+
34+
/**
35+
* Makes a new Matrix with the opposite values when the max value is 255
36+
*
37+
* @return
38+
*/
39+
public Matrix makeNegative() {
40+
int[][] negArr = new int[_array.length][];
41+
copyArray(negArr, _array);
42+
43+
for (int i = 0; i < _array.length; i++) {
44+
for (int j = 0; j < _array[i].length; j++) {
45+
negArr[i][j] = MAX - negArr[i][j];
46+
}
47+
}
48+
49+
return new Matrix(negArr);
50+
}
51+
52+
// Copy a given array to new one
53+
private void copyArray(int[][] blank, int[][] full) {
54+
for (int i = 0; i < full.length; i++) {
55+
blank[i] = new int[full[i].length];
56+
for (int j = 0; j < full[0].length; j++) {
57+
blank[i][j] = full[i][j];
58+
}
59+
}
60+
}
61+
62+
/**
63+
* Return a Matrix that will change every cell to it's neighbors's average
64+
*
65+
* @return a Matrix that will change every cell to it's neighbors's average
66+
*/
67+
public Matrix imageFilterAverage() {
68+
int[][] filterArr = new int[_array.length][];
69+
70+
for (int i = 0; i < filterArr.length; i++) {
71+
filterArr[i] = new int[_array[i].length];
72+
for (int j = 0; j < filterArr[0].length; j++) {
73+
filterArr[i][j] = sumNeighbors(_array, i, j);
74+
}
75+
}
76+
77+
return new Matrix(filterArr);
78+
}
79+
80+
// Running with a loop on all the neighbors around (i,j) and summing up
81+
private int sumNeighbors(int[][] arr, int col, int row) {
82+
int sum = 0;
83+
int cells = 0;
84+
85+
// Start at the left upper corner to the right lower corner
86+
// Depends on whether has only one row or only one column
87+
// And if it's one of the forth walls
88+
for (int i = ((col == 0) ? 0 : col - 1); i <= ((col == arr.length - 1) ? col : col + 1); i++) {
89+
for (int j = ((row == 0) ? 0 : row - 1); j <= ((row == arr[i].length - 1) ? row : row + 1); j++) {
90+
sum += arr[i][j];
91+
cells++;
92+
}
93+
}
94+
95+
return sum / cells;
96+
}
97+
98+
/**
99+
* Return the matrix after rotating it 90 degrees clockwise
100+
*
101+
* @return the matrix after rotating it 90 degrees clockwise
102+
*/
103+
public Matrix rotateClockwise() {
104+
int[][] rotate = new int[maxCols(_array)][_array.length];
105+
106+
for (int i = 0; i < rotate.length; i++) {
107+
for (int j = 0; j < rotate[i].length; j++) {
108+
rotate[i][j] = _array[_array.length - j - 1][i];
109+
}
110+
}
111+
112+
return new Matrix(rotate);
113+
}
114+
115+
/**
116+
* Return the matrix after rotating it 90 degrees clockwise
117+
*
118+
* @return the matrix after rotating it 90 degrees clockwise
119+
*/
120+
public Matrix rotateCounterClockwise() {
121+
int[][] rotate = new int[maxCols(_array)][_array.length];
122+
123+
for (int i = 0; i < rotate.length; i++) {
124+
for (int j = 0; j < rotate[i].length; j++) {
125+
rotate[i][j] = _array[j][rotate.length - i - 1];
126+
}
127+
}
128+
129+
return new Matrix(rotate);
130+
}
131+
132+
// Return the number that represent the array with the highest column number
133+
private int maxCols(int[][] arr) {
134+
int rows = 0;
135+
136+
for (int i = 0; i < arr.length; i++) {
137+
if (arr[i].length > rows)
138+
rows = arr[i].length;
139+
}
140+
141+
return rows;
142+
}
143+
144+
/**
145+
* Return the Matrix in table form
146+
*
147+
* @return the Matrix in table form
148+
*/
149+
public String toString() {
150+
String mat = "";
151+
152+
for (int i = 0; i < _array.length; i++) {
153+
for (int j = 0; j < _array[i].length; j++) {
154+
mat += _array[i][j];
155+
if (j < _array[i].length - 1) {
156+
mat += "\t";
157+
}
158+
}
159+
mat += "\n";
160+
}
161+
return mat;
162+
}
163+
}

0 commit comments

Comments
 (0)