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...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure...
(In C++) Task 1: Implement a code example of Constructor Chaining / Constructor Overloading. Make sure to label class and methods with clear descriptions describing what is taking place with the source code. Attach a snipping photo of source code and output
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...
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
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...
in java Create a class called Customer in three steps: • (Step-1): • Add a constructor...
in java Create a class called Customer in three steps: • (Step-1): • Add a constructor of the class Customer that takes the name and purchase amount of the customer as inputs. • Write getter methods getName and getPrice to return the name and price of the customer. You can write a toString() method which returns printed string of Customer name and its purchase. • (Step-2): Create a class called Restaurant. • Write a method addsale that takes customer name...
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...
Task #1 Writing a Copy Constructor 1.Copy the files Address.java(Code Listing 8.1), Person.java(Code Listing 8.2),Money.java(Code Listing...
Task #1 Writing a Copy Constructor 1.Copy the files Address.java(Code Listing 8.1), Person.java(Code Listing 8.2),Money.java(Code Listing 8.3), MoneyDemo.java(Code Listing 8.4), andCreditCardDemo.java(Code Listing 8.5) from the Student CD or as directed by your instructor. Address.java, Person.java, MoneyDemo.java, and CreditCardDemo.java are complete and will not need to be modified. We will start by modifying Money.java. 2.Overload the constructor. The constructor that you will write will be a copy constructor. It should use the parameter Money object to make a duplicate Moneyobject, by...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT