You are to create a class with so-called BoundaryCells. Methods in the class thr
ID: 3851359 • 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
//BoundaryCells.java
import java.util.Scanner;
public class BoundaryCells {
int bsize,vsize;
double doubleArray[];
int size;
public BoundaryCells(int bsize,int vsize) throws IllegalArgumentException{
if(bsize<0 && vsize<0){
throw new IllegalArgumentException();
}else{
this.bsize = bsize;
this.vsize = vsize;
size = 2*bsize+vsize;
doubleArray = new double[size];
}
}
public double setBoundaryCell(int index, double value) throws ArrayIndexOutOfBoundsException{
doubleArray[index]=value; // One Array repeats for twice
return value;
}
public double setValueCell(int index, double value) throws ArrayIndexOutOfBoundsException{
doubleArray[index]=value; // Second Array
return 0.0;
}
public double[] getDoubleArray() { // Return Array finally after merging values and indices
return doubleArray;
}
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("Enter BSize and VSize:"); // Reading sizes
int bsize=s.nextInt();
int vsize=s.nextInt();
int size = 2*bsize+vsize;
BoundaryCells bc = new BoundaryCells(bsize,vsize); // Passing to Constructor
for(int j=0;j<size;j++){ // Passing Boundary and values to the methods
if(j>=(vsize+bsize)){
bc.setBoundaryCell(j, (double) j);
}else if(j<bsize){
bc.setBoundaryCell(j, (double) j);
}else {
bc.setValueCell(j, (double) j);
}
}
double arrayValues[] = bc.getDoubleArray(); // Getting array after merging
System.out.print("[");
for(int k=0;k<arrayValues.length;k++){ // Printing Array
if(k==arrayValues.length-1){
System.out.println(arrayValues[k]);
}else{
System.out.print(arrayValues[k]+",");
}
}
System.out.print("]");
}
}
Output:
Enter BSize and VSize:
4 11
[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0,17.0,18.0]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.