Question

In: Computer Science

Task1: Write a class DynArr that represents an array data structure. Here are the attributes (data)...

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.

Solutions

Expert Solution

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);
                
        }
}

Related Solutions

Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces...
Enlistee and Private Classes Write an Enlistee class that keeps data attributes for the following pieces of information: Enlistee name Enlistee number Next, write a class named Private that is a subclass of the Enlistee class. The Private class should keep data attributes for the following information: Platoon number (an integer, such as 1, 2, or 3) Years of service (also an integer) Write the appropriate accessor and mutator methods for each class. Once you have written the classes, write...
Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To...
Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure to...
What is an array data structure? What is an array index? What are the benefits of...
What is an array data structure? What is an array index? What are the benefits of array structures? What are the drawbacks of array structures? What is a grid structure? Give examples of when an array could be used. Give examples of when a grid could be used.
In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
Write a class Car that contains the following attributes: The name of car The direction of...
Write a class Car that contains the following attributes: The name of car The direction of car (E, W, N, S) The position of car (from imaginary zero point) The class has the following member functions: A constructor to initialize the attributes. Turn function to change the direction of car to one step right side (e.g. if the direction is to E,it should be changed to S and so on.) Overload the Turn function to change the direction to any...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT