Question

Using Java write a program that performs the following: 1. Define a class that contains an...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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:

ex.search (5) returned -l ex.search (500) returned 703 ex.search (1024) returned 433 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Using Java write a program that performs the following: 1. Define a class that contains an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • help 6. (15 points) Define class GradeStat to describe the grades of all people in a...

    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...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    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)...

  • C++ program please provide code only for class counterType.h, counterTypeImp.cpp, and main.cpp. Separately code it. Define...

    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...

    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...

    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...

    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...

    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...

  • In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following...

    In C++ and comment so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep copy and supports...

  • language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or...

    language is java Restrictions: You are not allowed to use anything from the String, StringBuilder, or Wrapper classes. In general, you may not use anything from any other Java classes, unless otherwise specified. You are not allowed to use String literals in your code ("this is a string literal"). You are not allowed to use String objects in your code. The methods must be implemented by manipulating the data field array, The CSString Class: NOTE: Pay very careful attention to...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT