Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class MyRandomIntArray which encapsulates an integer array and has a cou

ID: 3556647 • Letter: W

Question

Write a class MyRandomIntArray which encapsulates an integer array and has a couple of operations. You will only submit MyRandomIntArray class (as a file named MyRandomIntArray.java) MyRandomIntArray class should have: [+5] a data field which is an int array. The default array size is 10. [+10] a constructor which creates and initializes the array with random generated values between (0-10) (You can either use Math.random() method or Random class to generate the elements) [+10] a constructor which creates and initializes the array with random generated values between (0-10). The constructor will have a parameter which determines the length of the array. (You can either use Math.random() method or Random class to generate the elements) [+5] a method named sum which calculates and returns the sum of all elements in the array. This method will not have any parameters and will return an int. [+5] toString method which returns a string containing all elements seperated by spaces. This method will not have any parameters and wil return String.

Explanation / Answer

// SAVE IT AS MyRandomIntArray.java

//Write a class MyRandomIntArray which encapsulates an integer array and has a couple of operations.
class MyRandomIntArray
{
//MyRandomIntArray class should have:
//[+5] a data field which is an int array. The default array size is 10. [+10]
private int[] data;
//a constructor which creates and initializes the array with random generated values between (0-10)
//(You can either use Math.random() method or Random class to generate the elements) [+10]
public MyRandomIntArray()
{
    data = new int[10];
   for(int i=0; i<10; i++)
       data[i] = (int)(Math.random() * 10);
}
//a constructor which creates and initializes the array with random generated values between (0-10).
//The constructor will have a parameter which determines the length of the array.
//(You can either use Math.random() method or Random class to generate the elements) [+5]
public MyRandomIntArray(int length)
{
       data = new int[length];
   for(int i=0; i<length; i++)
       data[i] = (int)(Math.random() * 10);
}
//a method named sum which calculates and returns the sum of all elements in the array.
//This method will not have any parameters and will return an int.
public int sum()
{
   int sum = 0;
   for(int i=0; i<data.length; i++)
       sum = sum + data[i];
   return sum;
}
//[+5] toString method which returns a string containing all elements seperated by spaces.
//This method will not have any parameters and wil return String.
public String toString()
{
   StringBuffer str = new StringBuffer("");
   for(int i=0; i<data.length; i++)
   {
   str.append(Integer.toString(data[i]));
   if(i!=data.length-1)
   str.append(" ");
   }
    return str.toString();
}
}

public class tester
{
public static void main(String[] args)
{
   MyRandomIntArray myarray = new MyRandomIntArray(7);
   System.out.println("Sum of elements given by " + myarray.sum());
   System.out.println("elements are " + myarray);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote