You are to create a class with so-called BoundaryCells. Methods in the class thr
ID: 3800691 • Letter: Y
Question
You are to create a class with so-called BoundaryCells. Methods in the class throw exceptions under certain conditions.
A BoundaryCell object stores an array of double. The constructor has two logical arguments used in the rest of this discussion
bsize - the number of boundary cells
vsize - the number of value cells
The total size of an internal array stored in a BoundaryCells object is: 2 * bsize + vsize
One can visualize the internal array as three arrays put together into a single array.
[ --- bsize --- ][ ---- vsize ---- ][ --- bsize ---]
The first bsize indices are boundary cells. The last bsize indices are also boundary cells.
Example: If you create a BoundaryCells object with bsize=3 and vsize=10, then
valid indices for Boundary cells are: 0,1,2 and 13,14,15
valid indices for Value cells are: 3,4,5, ...., 12
Define the following constructor and methods (all should be public)
BoundaryCells(int vsize, int bsize) throws IllegalArgumentException
create a BoundaryCells object with an internal double array of size: 2 * bsize + vsize
throws IllegalArgumentException if either bsize < 0 or vsize < 0
double setBoundaryCell(int index, double value) throws ArrayIndexOutOfBoundsException
set the contents of the internal array to value if index is a valid index in the "boundary" part of the array
throws ArrayIndexOutOfBounds Exception if index would not store into one of the boundary cells
return the value set when given a valid
double setValueCell(int index, double value) throws ArrayIndexOutOfBoundsException
set the contents of the internal array to value if index is a valid index in the "values" part of the array
throws ArrayIndexOutOfBounds Exception if index would not store into one of the boundary cells
return the value set when given a valid index
double [] getArray()
return a reference to the internal array storing the values. This should be a single array and must be a reference (not a deep copy) of the array used to store values.
Explanation / Answer
public class BoundaryCells {
public static void main(String args[] ) throws Exception {
double[] arr; /* reference variable for internale array of double */
int totalSize=0;
int boundrySize=0;
public BoundaryCells(int bsize , int vsize) /* constructor */
{
/* we applied if condition and if bsize and vsize are negative then it throw IllegalArgumentException*/
if((bsize <0) || (vsize <0))
{
throw new IllegalArgumentException("Error: bsize and csize are less than zero");
}
boundrySize = bsize;
valueSize = vsize;
totalSize = 2*bsize + vsize ; /* evaluate total size of array */
arr = new double[totalSize]; /* then created array of this size */
}
public void setBoundaryCell(int index, double value)
{
/* we applied condition on index for boundry part of an array and if index is not valid it throws ArrayIndexOfBoundsException*/
if((index < boundrySize) && ( index >= (totalSize-boundrySize))
{
arr[index] = value; /* set give value in array on given index */
}
else
{
throw new ArrayIndexOutOfBoundsException("Error: index is not valid for boundry part of an array");
}
}
/* method to get value of boundry part of array for given index */
public double getBoundaryCell(int index)
{
double value;
if((index < boundrySize) && ( index >= (totalSize-boundrySize))
{
value = arr[index];
}
else
{
throw new ArrayIndexOutOfBoundsException("Error: index is not valid for boundry part of an array");
}
return value;
}
public void setValueCell(int index, double value)
{
/* we applied condition on index for values part of an array and if index is not valid it throws ArrayIndexOfBoundsException*/
if((index >= boundrySize) && (index < (totalSize-boundrySize))
{
arr[index] = value /* set give value in array on given index */
}
else
{
throw new ArrayIndexOutOfBoundsException("Error: index is not valid for values part of an array");
}
}
/* method to get value of valuecell part of array for given index */
public double getValueCell(int index)
{
double value;
if((index >= boundrySize) && (index < (totalSize-boundrySize))
{
value = arr[index];
}
else
{
throw new ArrayIndexOutOfBoundsException("Error: index is not valid for boundry part of an array");
}
return value;
}
/* return the reference of array */
public double[] getArray()
{
return arr;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.