Question: C# I have finished mostly everything. I am stuck on how to use the object in the main method (the getValue method). Create a class

C#

I have finished mostly everything. I am stuck on how to use the object in the main method (the getValue method).

Create a class called Expandable. It should contain an instance variable that is a private 10 element int array. It should also have get and set methods that allow you assign or retrieve individual int values from the array.

Include code in your Expandable class that does the following: if you ever try to add an int value to a position that is out of the range of the array index numbers (i.e. you're trying to add an int to index number 12 in a 10 element array), then do the following:

1) Create a new array that is large enough to include the new index number. 2) Copy the data from the old array to the new array. 3) Put the new int value into the indicated position within the new array. 4) Assign the new array to the instance variable for the int array in the Expandable class (i.e. you're replacing the old array with the new array).

If the element number the user tries to assign is 1000 or greater, do not do any of the above. Instead, give the user a message stating that the range in question is not allowed. In other words, the array can never have more than 1000 elements (index numbers from 0 to 999).

An Example of how this might run:

//The array in the Expandable object has 10 elements. Expandable exp = new Expandable();

//assigning the value 10 to element number 12 //create a new array and assign a 10 to element number 10 //remember to copy any data in the old array to the new array. exp.setValueToArray(10, 12);

//get the new value from index number 12 in the array. int x = exp.getValue(12);

//should print '10' System.out.print(x);

//should print 'That index number is not allowed' exp.setValueToArray(15, 2000);

namespace Project { class Program { static void Main(string[] args) { Expandable exp = new Expandable(10, 9); int x = exp.getValue(9); } } class Expandable { private int[] expandableArray = new int[10];

public Expandable(int uv, int ui) { userValue = uv; userIndex = ui;

}

public int userValue { get; set; } public int userIndex { get; set; } public int[] arrayValues { get { return expandableArray; } set { bool flag = true; while (flag == true) { flag = false; if (userIndex < 0 || userIndex > 999) { Console.WriteLine("Error: The index must be 0-999, please re-enter:"); flag = true; } } if (userIndex > 9) { int[] newArray = new int[userIndex]; Array.Copy(expandableArray, newArray, userIndex); newArray[userIndex] = userValue;

newArray = value; } else { expandableArray[userIndex] = userValue; expandableArray = value; }

} } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!