namespace Assignment4_2
{
class SumArray
{
public class ProjectArray_Nonyelu
{
private int[] data;
public void SumArray(int[] d)
{
data = d;
}
public int[] SumOfArray()
{
Console.Write("Enter an array size: ");
int size = int.Parse(Console.ReadLine());
int[] anArray = new int[size];
for (int i = 0; i < size; i++)
{
Console.Write("anArray[" + i + "] = " );
anArray[i] = int.Parse(Console.ReadLine());
}
return anArray;
}
public static int[] SumOfArray(int[] d)
{
int[] a = { 5, 4, 6, 8 };
int x = 0, sum = 0;
for (int i = 0; i < a.Length; i++)
sum += a[i];
{
Console.WriteLine(sum);
}
return a;
}
public int MaxOfArray()
{
int[] c = new int[5];
int max;
for (int i = 0; i < 5; i++)
{
c[i] = int.Parse(Console.ReadLine());
}
max = c[0];
for (int i = 0; i < 5; i++)
{
if(c[i] > max)
{
max = c[i];
}
}
return max;
}
public static int[] MaxOfArray(int[] a)
{
int[] b = new int[5];
int max;
Console.Write("Enter 5 numbers: ");
for (int i = 0; i < 5; i++)
{
b[i] = int.Parse(Console.ReadLine());
}
max = b[0];
for (int i = 0; i < 5; i++)
{
if(b[i] > max)
{
max = b[i];
}
}
Console.WriteLine("The largest number is {0}", max);
return b;
}
public void DisplayArray(String m)
{
Console.Write(m + "{");
for (int i = 0; i < data.Length; i++)
{
if(i != 0)
Console.Write(",");
Console.Write(data[i]);
}
Console.WriteLine("}");
}
public static void DisplayArray()
{
int[] a = { 3, 4, 5, 6 };
Console.Write("{");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a);
}
}
public static int[] ReadIntArray()
{
Console.WriteLine("Enter an array size:");
int size = int.Parse(Console.ReadLine());
int[] anArray = new int[size];
for (int i = 0; i < size; i++)
{
Console.WriteLine("anArray [" + i + "] = ");
anArray[i] = int.Parse(Console.ReadLine());
}
return anArray;
}
}
public class TestProjectArray_Nonyelu
{
}
static void Main(string[] args)
{
}
}
}
What I have tried:
I've tried to call all of the methods, but I keep getting errors. I've cancelled it out in the main.
I posted the directions of the assignment, so you can see how I wrote my code. If you can let me know my mistakes I'd really appreciate it.
Create a console application, Assignment4_YourLastName, then add two classes ProjectArray_yourLastName and TestProjectArray_yourLastName to your project.
I. Make your class ProjectArray_yourLastName have:
1. a private variable data of int array type;
2. a constructor which takes one parameter to initialize data;
3. an instance method SumOfArray which returns the sum of data elements;
4. a static method SumOfArray which takes one parameter of int array type and returns the sum of its parameter’s elements;
5. an instance methods MaxOfArray which returns the maximum element of data elements;
6. an static methods MaxOfArray takes one parameter of int array type and returns the maximum element of its parameter’s elements;
7. an instance method DisplayArray of displaying a message and data
8. a static method DisplayArray of displaying a message and an array
9. a static method ReadIntArray of reading an array and returning the array
II. Make your class TestProjectArray_yourLastName only has the Main method. In the Main method, add statements to call every method you declared in class ProjectArray_yourLastName at least once. Note: there are instance methods and static methods. They are declared and used in different ways!!!!!!!