Question

In: Computer Science

1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2....

1. Add code to the constructor which instantiates a testArray that will hold 10 integers.

2. Add code to the generateRandomArray method which will fill testArray with random integers between 1 and 10

3. Add code to the printArray method which will print each value in testArray.

4. Write an accessor method, getArray which will return the testArray

Here's the code starter code:

import java.util.ArrayList;

public class TestArrays

{

private int[] testArray;

public TestArrays()

{

}

public void generateRandomArray()

{

}

public void printArray()

{

}

}

Solutions

Expert Solution

Below is the code and screenshots .

TestArrays.java

-----------------------------------------------------

/*
* This class fills the array with 1 to 10 random numbers
* and prints the each value in the array.
*/
import java.util.ArrayList;

public class TestArrays
{
   private int[] testArray;
   private int arraylength;

   public TestArrays(int x)
   {
       arraylength= x;

   }
    /*
     * Method to fill the array with random numbers from 1 to 10.
     */
   public void generateRandomArray()
   {
       System.out.println("Populating array with random values");
       testArray = new int[arraylength];
      
       for(int i=0;i<testArray.length;i++)
       {
           testArray[i] = (int) (Math.random()*10);
       }
   }
    /*
     * This method prints the values in the array one by one.
     */
   public void printArray()
   {
       System.out.println("Going to print the array values :");
       for(int i=0; i<testArray.length; i++)
       {
           System.out.println(testArray[i]);
       }

   }
  
   /**
   * getter method to return the array. This will return array object.
   * @return the testArray
   */
   public int[] getTestArray() {
       return testArray;
   }

   public static void main(String [] args) {
       TestArrays t = new TestArrays(10);
       t.generateRandomArray();
       t.printArray();
      
   }

}

----------------------------------------------------------------------------------

Output:

I have writtne this code in eclipse -> It gets compiled automatically.

To run the program -> right click on it and Run as --> Java Applicaiton -

Populating array with random values
Going to print the array values :
8
2
3
7
3
6
4
8
5
3


Related Solutions

(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three...
(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c public void setA(double newA) public void setB(double newB) public void setC(double newC) (6) public static QuadraticExpression scale( double r, QuadraticExpression q) returns...
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
1. Write a shell code that prints the integers from 80-90 2. Write a shell code...
1. Write a shell code that prints the integers from 80-90 2. Write a shell code that prints the sum of odd integers that are inputted by the user 3. Write a shell code that reads a set of integers {125, 0, 122, 129, 0, 117} and prints {125, 122, 129, 117} Programming Language: Linux
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add...
Question 1 Refer to the operations below: Add (10 + 5) Add (4+8) Add (7*2) Add (90 – 3) Print list Print peek Remove an item from the list Print list 1.1 Implement the operations above into a Queue structure called q1. 1.2 Implement the operations above into a Stack structure called s1. Name your program Question1_1 for the queue structure and Question1_2 for the stack structure JAVA Language to be used. Please give step by step explanation on how...
Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the...
Project 10-1 Convert lengths In this assignment, you’ll add code to a form that converts the value the user enters based on the selected conversion type. The application should handle the following conversions: From To Conversion Miles - Kilometers: 1 mile = 1.6093 kilometers Kilometers - Miles: 1 kilometer = 0.6214 miles Feet - Meters: 1 foot = 0.3048 meters Meters - Feet: 1 meter = 3.2808 feet Inches - Centimeters: 1 inch = 2.54 centimeters Centimeters - Inches: 1...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize...
Step 1: Extend the dispenserType class per the following specifications. Add a parameterized constructor to initialize product name (name), product cost (cost) and product quantity (noOfItems). This constructor will be invoked from main.cpp. One such call to this constructor will look like: dispenserType sanitizer("hand sanitizer", 50, 100); Add the declaration and definition of this parameterized constructor to .h and .cpp files. Step2: Define a new class, cashRegister. Start by creating cashRegister.h and cashRegister.cpp files. Be sure to update your CMakeLists.txt...
Design a matlab code that: 1-play sound 2- add noise to the sound 3- filter the...
Design a matlab code that: 1-play sound 2- add noise to the sound 3- filter the noised signal using fir or iir filter Note: FIR: Finite impulse response filter IIR: Infinite impulse response filter
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
1. Which is the subchapter of the Code containing Code Section 303? 2. What is the...
1. Which is the subchapter of the Code containing Code Section 303? 2. What is the Internal Revenue Code section for a qualified dependent? 3. J. C. has been a professional gambler for many years. He loves this line of work and believes the income is tax-free. - Is J.C. Correct? What court cases can you find that supports your position?
Overview For this program, add code to program 2 that will (potentially) allow for a discount...
Overview For this program, add code to program 2 that will (potentially) allow for a discount to be applied to the cost of the tickets to the movie theater. Basic Program Logic The program should start the same as program 2 by asking the user for the number of tickets to purchase for adult and children. The program should then ask the user if they have a discount coupon. This is a string value. A value of "Y" indicates the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT