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...
C++ Implement the array based Binary Heap data structure as discussed in class. This structure should...
C++ Implement the array based Binary Heap data structure as discussed in class. This structure should have a couple of constructures (default constructor, and a constructor that takes an array pointer and a size), a method for inserting items into the heap, a method for removing items from the heap, and a method that returns the number of items currently stored in the heap. This implementation should be templated so that it can store any type of data (you may...
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...
Using classes and array data structure write methods with algorithms for a software that an airline...
Using classes and array data structure write methods with algorithms for a software that an airline can use to view available/booked seats, management booking, canceling booking and reorder seats. The solution should consist of a minimum of two classes with one class containing the main method and second class for managing a manipulating data. The choice of data structure for this assignment will be static one dimension arrays. in C++
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...
Here is the assignment description. * Create a class named 'Account' having the following private attributes...
Here is the assignment description. * Create a class named 'Account' having the following private attributes int accountNumber; double balance; * Write a constructor with parameters for each of the attributes. * Write another constructorwith one parameter for the accountNumber. * Write getter and setter methods for each of the private attributes. * Write a method void credit(double amount) which adds the given amount to the balance. * Write a method void debit(double amount) which subtracts the given amount from...
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 java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT