Number 1 I have completed and number 6 is just uploading. I just need some help with the 2,3,4
and 5.
This is my code from Vector.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace Vector
{
public class Vector<T>
{
// This constant determines the default number of elements in a newly created vector.
// It is also used to extended the capacity of the existing vector
private const int DEFAULT_CAPACITY = 10;
// This array represents the internal data structure wrapped by the vector class.
// In fact, all the elements are to be stored in this private array.
// You will just write extra functionality (methods) to make the work with the array more convenient
for the user.
private T[] data;
// This property represents the number of elements in the vector
public int Count { get; private set; } = 0;
// This property represents the maximum number of elements (capacity) in the vector
public int Capacity
{
get { return data.Length; }
}
// This is an overloaded constructor
public Vector(int capacity)
{
data = new T[capacity];
}
// This is the implementation of the default constructor
public Vector() : this(DEFAULT_CAPACITY) { }
// An Indexer is a special type of property that allows a class or structure to be accessed the same
way as array for its internal collection.
// For example, introducing the following indexer you may address an element of the vector as
vector[i] or vector[0] or ...
public T this[int index]
{
get
{
if (index >= Count || index < 0) throw new IndexOutOfRangeException();
return data[index];
}
set
{
if (index >= Count || index < 0) throw new IndexOutOfRangeException();
data[index] = value;
}
}
// This private method allows extension of the existing capacity of the vector by another
'extraCapacity' elements.
// The new capacity is equal to the existing one plus 'extraCapacity'.
// It copies the elements of 'data' (the existing array) to 'newData' (the new array), and then makes
data pointing to 'newData'.
private void ExtendData(int extraCapacity)
{
T[] newData = new T[Capacity + extraCapacity];
for (int i = 0; i < Count; i++) newData[i] = data[i];
data = newData;
}
// This method adds a new element to the existing array.
// If the internal array is out of capacity, its capacity is first extended to fit the new element.
public void Add(T element)
{
if (Count == Capacity) ExtendData(DEFAULT_CAPACITY);
data[Count] = element;
Count++;
}
// This method searches for the specified object and returns the zerobased index of the first
occurrence within the entire data structure.
// This method performs a linear search; therefore, this method is an O(n) runtime complexity
operation.
// If occurrence is not found, then the method returns 1.
// Note that Equals is the proper method to compare two objects for equality, you must not use
operator '=' for this purpose.
public int IndexOf(T element)
{
for (var i = 0; i < Count; i++)
{
if (data[i].Equals(element)) return i;
}
return -1;
}
// TODO: Your task is to implement a.
Number 1 I have completed and number 6 is just uploading I .pdf
1. Number 1 I have completed and number 6 is just uploading. I just need some help with the 2,3,4
and 5.
This is my code from Vector.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace Vector
{
public class Vector<T>
{
// This constant determines the default number of elements in a newly created vector.
// It is also used to extended the capacity of the existing vector
private const int DEFAULT_CAPACITY = 10;
// This array represents the internal data structure wrapped by the vector class.
// In fact, all the elements are to be stored in this private array.
// You will just write extra functionality (methods) to make the work with the array more convenient
for the user.
private T[] data;
// This property represents the number of elements in the vector
public int Count { get; private set; } = 0;
// This property represents the maximum number of elements (capacity) in the vector
public int Capacity
{
get { return data.Length; }
}
// This is an overloaded constructor
public Vector(int capacity)
{
data = new T[capacity];
}
// This is the implementation of the default constructor
public Vector() : this(DEFAULT_CAPACITY) { }
// An Indexer is a special type of property that allows a class or structure to be accessed the same
way as array for its internal collection.
// For example, introducing the following indexer you may address an element of the vector as
vector[i] or vector[0] or ...
public T this[int index]
{
get
{
if (index >= Count || index < 0) throw new IndexOutOfRangeException();
return data[index];
2. }
set
{
if (index >= Count || index < 0) throw new IndexOutOfRangeException();
data[index] = value;
}
}
// This private method allows extension of the existing capacity of the vector by another
'extraCapacity' elements.
// The new capacity is equal to the existing one plus 'extraCapacity'.
// It copies the elements of 'data' (the existing array) to 'newData' (the new array), and then makes
data pointing to 'newData'.
private void ExtendData(int extraCapacity)
{
T[] newData = new T[Capacity + extraCapacity];
for (int i = 0; i < Count; i++) newData[i] = data[i];
data = newData;
}
// This method adds a new element to the existing array.
// If the internal array is out of capacity, its capacity is first extended to fit the new element.
public void Add(T element)
{
if (Count == Capacity) ExtendData(DEFAULT_CAPACITY);
data[Count] = element;
Count++;
}
// This method searches for the specified object and returns the zerobased index of the first
occurrence within the entire data structure.
// This method performs a linear search; therefore, this method is an O(n) runtime complexity
operation.
// If occurrence is not found, then the method returns 1.
// Note that Equals is the proper method to compare two objects for equality, you must not use
operator '=' for this purpose.
public int IndexOf(T element)
{
for (var i = 0; i < Count; i++)
{
if (data[i].Equals(element)) return i;
}
return -1;
}
// TODO: Your task is to implement all the remaining methods.
3. // Read the instruction carefully, study the code examples from above as they should help you to
write the rest of the code.
public void Insert(int index, T element)
{
if (Count == Capacity)
{
ExtendData(DEFAULT_CAPACITY);//Used to increase capacity of vector and copy old elements
into new
}
//If index is equal to count, add an element to the end
if (index == Count)
{
Add(element);
return;
}
//Check if a element is being insterted outside of the vector. If it is, throws an exception
if (index > Count || index < 0)
{
throw new IndexOutOfRangeException("Index is out of range: " +
index);
}
int count = Count;
while (count != index)//While loop to shuffle elements to the end of vector
{
data[count] = data[count - 1];//Shuffle
count--;//Goes Back
}
data[count] = element;//Add element
Count++;//Increase vector size for new element
}
public void Clear()
{
Array.Clear(data, 0, Count); //Clear all elements from the array.
Count = 0; //Sets Count to 0
}
public bool Contains(T element)
{
//Check if element is in vector
return IndexOf(element) != -1;
}
public bool Remove(T element)
{
4. int index = IndexOf(element);//Variable to find index of a particular element
if (index > -1) //Check if its valid
{
RemoveAt(index);//Removes a particular element at index
return true;
}
return false;//method needs return type
}
public void RemoveAt(int index)
{
//Check if the index is within the range of the array.
if (index < 0 || index >= Count)
{
throw new IndexOutOfRangeException("Index is out of range: " +
index);
}
//Removes the element of an index from the vector
for (int i = index; i < Count - 1; i++)
{
data[i] = data[i + 1];
}
Count--;//Decrease Count due to remove of element
}
public override string ToString()
{
StringBuilder stringBuild = new StringBuilder("[", 50);// Stringbuilder Instance
for (var i = 0; i < Count; i++) //for loop to add , to each element
{
stringBuild.Append(data[i]);//Calls append to string
if (i != Count - 1) stringBuild.Append(',');//Check if Count is last elemnt and adjusts if not
}
stringBuild.Append(']');
return stringBuild.ToString();//has to return has method has return type
}
}
}
Here is ss of the tester.cs:
Please comment the new code as well so I can learn it for next task. Thank you.
1. Create a copy of your solution for Task 1.1 and replace the Tester.cs file by that attached to this
task. The Main method of the new Tester class is to be compatible with the Vector T>. Again, it
contains a series of tests that will verify if the functionality that you need to develop in this task
works correctly. See Appendix A for an example of what the program will output when working
5. correctly. 2. Extend your Vector <T> class by adding the following two overloaded methods: - void
Sort() Sorts the elements in the entire Vector >> collection. The order of elements is determined
by the default comparer for type T, which is comparer <T, Default. - void Sort( IComparer
comparer ) Sorts the elements in the entire Vector <T> collection using the specified comparer. If
the comparer is null, the order of elements is determined by the default comparer for type T, which
is In this task, the implementation of both methods does not require you to implement any sorting
algorithm. Instead, you should delegate sorting of data elements to Array.Sort, which implements
the Introspective sorting algorithm as the sorting method for the Array class. Similar to the two
methods above, Array.Sort is overloaded and has two versions: One that accepts an array along
with the starting index and the number of elements to define the range for sorting, and the other,
which additionally accepts a comparer object for the purpose of determining the order of elements.
Read about these two methods at https://learn.microsoft.com/en-
us/dotnet/api/system.array?redirectedfrom=MSDN&view=net-7.0#methods Once you have your
both Sort methods set up, you should be able to sort integers and succeed with tests A, B, C, and
D in the attached Tester class. To enable these tests, uncomment the respective code lines in
Tester.cs file.| 3. Explore the Student class provided in Tester.cs file. To make two students
comparable (and thus sorted in the Vector <T> collection like in the List<T> ), you must enable a
mechanism that automatically compares two elements of type Student. Keep in mind that in this
case T is a blueprint type, which during the runtime will be replaced by the Student class. 4. One
way to make two elements comparable in the .NET Framework is to introduce a comparison
method right in the type, for example, in the Student class. This is how we can 'teach' the type to
compare one element (referred to as this) with the other element (referred to as another) of the
same type. In the code, this should be done by implementing the special interface in the class,
which in its turn requires implementation of the CompareTo(T another) method that compares this
(current) element with another element and returns an integer value. The integer values is - less
than zero, when this (current) element must precede another element specified as the argument,-
zero, when this (current) element is seen as equal to another element, - greater Ghan zero, when
this (current) element must succeed another element. Your task now is to modify the Student class
and implement the IComparable > interface along with its underlying CompareTo(Student another)
method so that it can sequence students in the ascending order of the ID attribute. When this is
done, you should be able to sort objects of Student class via the Sort( ) method of the Vector <T>
class. Complete this part to pass Test E by activating its respective code lines in the program
code. 5. The other way to make two elements comparable is to develop a so-called comparator
class, whose purpose is to compare a pair of elements of specific type. Such comparator class
must implement the IComparer T> interface. The IComparable T is similar to the IComparer T>,
except that the CompareTo(T another) method belongs to the type, while Compare (Ta,T b) is part
of an independent class implementing the IComparer T>. The Compare(T a, T b) method implied
by the Comparer T> returns an integer value indicating whether argument a is less than, equal to,
or greater than argument b, exactly as the CompareTo method does. There are a few examples of
classes implementing the IComparer > interface in Tester.cs file that sort integers in ascending,
descending, or in the order that places even integers prior to odd integers. You should learn from
these examples to develop your own comparator for the Student class. Specifically, your task is to
6. provide two comparators for the Student class: - AscendinglDComparer to sort a sequence of
students stored in an instance of the Vector SStudent> class in the ascending order of IDs; -
DescendingNameDescendingldComparer to sort a sequence of students stored in an instance of
the Vector Student> class in the descending order of names, breaking ties by the descending
order of IDs. Once this is done, you should be able to pass tests F and G. Make sure that students
appear in the desired order. Keep in mind that any of these comparators can be the argument of
the Sort(IComparer T> comparer) method of the Vector <T> class; so, test how they work with
your Vector < Student > class. 6. Submit the new version of the Vector <T> class and the updated
Tester.cs file that now constains the extended Student class and both AscendinglDComparer and
DescendingNameDescendingldComparer classes.AStudent student = new Student() { Name =
names [ random. Next (, names. Length )], Id =i}; Console.Writeline("Add student with record: " +
student. ToString()); students. Add(student); Console.Writeline("Sort the students in the order
defined by the DescendingNameDescendingIdComparer class"); // uncomment the line to activate
the test //students. Sort(new DescendingNameDescendingIdComparer ()); Console.
Writeline("Print the vector of students via students. ToString(); "); Console. Writeline(students.
ToString()); Console. Writeline(" : SUCCESS"); result = result +mG; catch (Exception exception)
Console.Writeline(" : FAIL"); Console . Writeline(exception. ToString()); result = result +n";
Console. Writeline(" n Console. Writeline("Tests passed: "+ result); Console.ReadKey();