In: Computer Science
Task1:
Write a class DynArr that represents an array data structure.
Here are the attributes (data) of the class (note - fields of the objects should be marked as private)”
INITIAL_SIZE: the initial size of the array = 10;
_innerArr: an array of integers
_growthFactor: the factor by which to resize the array with to become bigger.
_lastIndex: an index on the last element in the array.
The class will have the constructor public DynArr() that initialzes an array of integers with size equal to
INITIAL_SIZE , _growthFactor =2, and _lastIndex = 10.
DynArr class consists the following methods :
public int addToEnd(int valueToAdd): takes an integer parameter valueToAdd and add it to the end of the array. The method should first examine if the array is not full, otherwise the method should call grow() method to enlarge the array.
private void grow(): this method enlarge the array according to the _grothFactor attribute value.
public int getFromEnd(): a method that returns the last element added to the array.
public int getFromStart(): a method that returns the first element in the array.
public void removeFromStart(): a method that removes the first element from the array.
public void setLastIndex(int lastIndex) : assigns lastIndex parameter to _lastIndex instance varial.
public boolean remove(int index): remove from the array the element indexed by index parameter.
public int getValue(int index): returns the element of the array indexed by index parameter.
public int getLastIndex(): returns the index of the last element in the array.
public String toString(): returns a string that contains all the elements in the array separated by a comma.
Task2:
Write another class public class DynMatrix that contains the following data attributes:
_arr : an array of DynArr object.
SIZE: size of the array
_arr with final value = 100.
The class DynMatrix has the constructor:
public DynMatrix(): initializes _arr with size equale to SIZE.
The class DynMatrix contains the method:
public String toString(): returns a string with all the elements in _arr separated by a comma.
Task 3:
Write a tester class called arrTest with main method to do the following:
Create two instances of DynArr object.
Add 5 elements to _innerArr in each instance of DynArr entered by user.
Create an instance of DynMatrix object,
Add the two DynArr objects to _arr in DynMatrix.
Print the contents of _arr in DynMatrix instance.
I have implemented the DynArr and DynMatrix and per the given description
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.Code Screenshot:
2.OUTPUT
3.CODE
class DynArr{
private final int INITIAL_SIZE;
private int[] _innerArr;
private int _growthFactor;
private int _lastIndex;
public DynArr(){
INITIAL_SIZE=10;
_innerArr=new int[INITIAL_SIZE];
_growthFactor=2;
_lastIndex=10;
}
public int addToEnd(int valueToAdd){
/*takes an integer parameter valueToAdd and add it to the end of the array.
The method should first examine if the array is not full,
otherwise the method should call grow() method to enlarge the array.*/
if(_lastIndex==0){
grow();
}
--_lastIndex;
_innerArr[_lastIndex]=valueToAdd;
return _lastIndex;
}
/*: this method enlarge the array according
to the _grothFactor attribute value.*/
private void grow(){
int i,j;
int[] _innerArrNew=new int[_innerArr.length];
//copy the original element into the temporary array
for(i=_lastIndex-1;i>0;i--){
_innerArrNew[i]=_innerArr[i];
}
//increase array size by groth facter
_innerArr=new int[_innerArr.length*_growthFactor];
j=_innerArr.length*_growthFactor-1;
for(i=_lastIndex-1;i>0;i--,j--){
_innerArr[j]=_innerArrNew[i];
}
_lastIndex=j+1;
}
/*a method that returns the last element added to the array.*/
public int getFromEnd(){
int i=_innerArr.length-1;
i=_innerArr[i];;
return i;
}
/* a method that returns the first element in the array.*/
public int getFromStart(){
int t=_innerArr[_lastIndex-1];
return t;
}
/*a method that removes the first element from the array.*/
public void removeFromStart(){
_innerArr[_lastIndex-1]=0;
_lastIndex--;
}
/*assigns _lastIndex parameter to __lastIndex instance varial.*/
public void set_lastIndex(int lastIndex) {
_lastIndex=lastIndex;
}
/* remove from the array the element indexed by index parameter.*/
public boolean remove(int index){
boolean status=false;
for(int i=index;i>_lastIndex;i--){
_innerArr[i]=_innerArr[i+1];
status=true;
}
if(status)
_lastIndex--;
return status;
}
/*returns the element of the array indexed by index parameter.*/
public int getValue(int index){
return _innerArr[index];
}
/* returns the index of the last element in the array.*/
public int get_lastIndex(){
return _lastIndex;
}
/*returns a string that contains all the elements in the array separated by a comma.*/
public String toString(){
String s="";
for(int i=_lastIndex;i<_innerArr.length;i++){
s+=_innerArr[i]+",";
}
return s;
}
}
public class DynMatrix{
DynArr _arr[];
private int SIZE;
public DynMatrix(){
SIZE=100;
_arr=new DynArr[SIZE];
}
public String toString(){
String s="";
for(int i=0;i<SIZE;i++){
s+=_arr[i].toString()+"\n";
}
return s;
}
}
public class arrTest{
public static void main(String args[]){
int i,j;
DynArr d=new DynArr ();
for(i=0;i<5;i++)
d.addToEnd(i);
System.out.print("Elements in the first Array :");
System.out.println(d);
DynArr d2=new DynArr ();
for(i=5;i>0;i--)
d2.addToEnd(i);
System.out.print("\nElements in the second Array :");
System.out.println(d2);
}
}