Using Java write a program that performs the following:
1. Define a class that contains an array of type int as a data
member.
2. Define a constructor that creates an array of 1024 elements and
assigns it to the class data member.
The constructor shall populate the elements with random numbers
ranging in value from 1 to 1024.
3. Define and implement a search() method that take a parameter of
type int and returns the index location if the value in the
parameter is in the array, or -1 if it is not found.
4. Implement a main() method to exercise this class.
class ArrayExample{
//Array of type int
private int[] arr;
//Constructor
public ArrayExample(){
//Create an array of 1024 elements
arr = new int[1024];
//Populate array with random numbers ranging in value from 1 to 1024
for(int i = 0; i < 1024; i++){
arr[i] = (int)(Math.random()*1024 + 1);
}
}
public int search(int key){
for(int i = 0; i < 1024; i++){
//If key found then return the index
if(arr[i] == key)
return i;
}
//If key not found then return -1
return -1;
}
}
public class Main {
// declaration of main method
public static void main(String[] args) {
ArrayExample ex = new ArrayExample();//Create an object of ArrayExample class
//ex.search(5);
System.out.println("ex.search(5) returned " + ex.search(5));
System.out.println("ex.search(500) returned " + ex.search(500));
System.out.println("ex.search(1024) returned " + ex.search(1024));
}
}
SAMPLE OUTPUT:

Using Java write a program that performs the following: 1. Define a class that contains an...
Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...
help
6. (15 points) Define class GradeStat to describe the grades of all people in a group. (1) Declare data member grades as an array of integers (we can declare it as a double type, but use int type can be simpler). (2) Write a constructor taking an array of int as input parameter. Use it to initialize data member. Note that each element in the array should be a non- negative int not larger than 100, that is, an...
C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the following attributes: Data members (private): string: username int array: ratings Number of elements should be size int: numRatings Number of books in the database Int: size The capacity of the ratings array (50). Constant Member functions (public): Default constructor Sets username to an empty string, numRatings to 0, size to 50, and all the elements of ratings array to the value 0 Parameterized constructor...
5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...
Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...
C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define a class counterType to implement a counter. Your class must have a private data member counter of type int. Define a constructor that accepts a parameter of type int and initializes the counter data member. Add functions to: Set counter to the integer value specified by the user. Initialize counter to 0. Return the value of counter with a function named getCounter. Increment and...
JAVA HELP (ARRAYS)
Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...
In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...
Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...
Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...