
Concept explainers
Write a program that includes in its MainClass the following methods:
1. A method called printArray that takes an array of double values and prints the array
elements on the same line with one space between each two elements, then prints a new
line.
2. A method called getRandomElement that takes an array of double numbers
and returns a random element from the array.
3. A method called shiftArray that takes a double value and an array of double
values. The method must shift the array in its place to the right, and insert the double
value at index 0. Note that this method must not create a new array, it must rotate the
array in its place.
For example, if the passed array is 6.5, 2.1, 5.8, 1.5, 8.9 and the passed double value is
3.3, after invoking the method the array must become:
3.3, 6.5, 2.1, 5.8, 1.5
4. A method called occurrences that takes a double value and an array of doubles
as parameters, and returns the number of occurrences of the double value in the array.
For example, if the double number 4.1 and the array {1.5, 4.1, 1.5, 2.3, 4.1, 4.1, 5.2}
are passed, the method must return the number 3.
5. A method named findElement that takes a double number and an array of
double numbers as parameters and returns an array that includes the indices of all the
number occurrences in the array.
This method must invoke the method occurrences to get how many times the value
appears in the array.
For example, if the double number 4.1 and the array {1.5, 4.1, 1.5, 2.3, 4.1, 4.1, 5.2}
are passed, the method must return the array {1, 4, 5}.
If the passed number does not occur in the array, the method must return an array of
zero elements
6. A method called print2DArray, that takes a two-dimensional array of double
values, and prints its elements as a matrix of rows and columns.
7. A method called create2DArray that takes two integers, the first represents
the number of rows and the second represents the number of columns. It must return a
two-dimensional array of double values with the entered number of rows and columns
filled with random double values between 0 and 10.
8. In your main method:
- Declare, create, and initialize the following arrays of double values with the following
names:
list1:
3.5 6.5 1.5 6.5 6.5 8.5 5.5 1.5
list2: an array whose size and elements values are read from the user.
- Print elements of the arrays list1 and list2 by invoking the printArray method.
- Print a random element from list2 by invoking getRandomElement method.
- Shift the array list1 three times to the right and fill the value 2.5 in the first three
indices by invoking the shiftArray method.
- Print the indices of the value 6.5 in the array list1 by invoking the findElement method.
- Create a two-dimensional array by invoking the create2DArray method then print it
to the console by invoking the print2DArray method

Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 3 images

- Write a class ArrayMethods, where the main method asks the user to enter the number of inputs and stores them in an array called myList. Create a method printArray that prints the array myList. Create another method copyArray that copies the array using System.arraycopy, and multiplies each element in the copied array by 2, then finally returns the array to the main method. Invoke the printArray again to print the array returned by copyArray.arrow_forwardCode these three methods.arrow_forwardWrite a java class method named findFourLetterWord that searches an array of words (String objects) for a wordthat’s four characters long. The array will be the method’s only parameters. findFourLetterWord will return thefirst four-letter word that it finds. If none of the words have the proper length, the method will return null.arrow_forward
- Rename the show method to people Override the show method in student and in the other class you created (this will be the polymorphic method) In main program, create an array of 3 objects of class people Instantiate each array position with a different object (people, student, other) With a loop, display all the objects in the array.arrow_forwardWrite a Java method that takes an array of type int and returns a boolean value. The method returns true if the number of positive elements in the parameter array is larger than the number of negative elements. The method returns false if any of the following conditions is satisfied: • The parameter array is of length of 0 or 1 • The number of positive elements of the parameter array is less than or equal to the number of negative elements • The number of positive elements or negative elements is equal to zero The method's header is as follows. public static boolean verify (int[] x) Sample run Result int[] x = (1, 2, 3}; verify(x); {5}; verify(x); int[] x = {1, 2, 3, -5, -1}; boolean b = verify(x); int[] x = new int[0]; verify(x); false boolean b = int[] x = boolean b = false true false boolean b = BIU A Paragraph +varrow_forwardIn Java please. Add comments too! thank you!arrow_forward
- An incomplete program named w_pl java, which must include three methods, is provided. You must implement two methods within the program as described below. The third method, which is a main method, is already written in the program and you can use it to test your program. The two methods you mast inplement are: • find method • Signature: public statie vold find(int[] a, int x) • Behavior: • Reccives an array of integers a and an integer x • Performs a linear search on a for x. • If x is in a, print the indexes of the aray slots where x is stored (there may be multiple x's in a) on the screen. • Ifx is not in a, print the message "x does not exist" on the screen. Note that you should not use Java's built-in method. Your program must scan and search the array. • isPrifie method • Signature: public statie boolean isPrefix(String s1, String s2) • Behavior: • Receives two strings sl and s2. Assume that length of sl length of s2. Returns true if sl is a prefix of s2 and returns false…arrow_forwardWrite a class method named countOccurences that returns the number of times a particular integer occurs in an array of integers. The integer and the array will both be parameters.arrow_forwardThe Delicious class – iterative methods The Delicious class must:• Define a method called setDetails which has the following header: public void setDetails(String name, int price, boolean available) This must find in the ArrayList the Chicken with the given name, change itspricePerKilo to the given price parameter and set whether it is in stock or not. A value of true for the available parameter means the chicken is in stock and a value of false means it is sold out (not in stock). Note that there will only be at most one Chicken object of the given name stored in the ArrayList.• Define a method called removeChicken that takes a single String parameter representing a chicken’s name. This method must remove from the ArrayList the Chicken object (if any) with the given name. The method must return true if a cheese with the given name was found and removed, and false otherwise.The Delicious class – challenge method In the Delicious class complete the findClosestAvailable method: This…arrow_forward
- Write a java class method named countOccurences that returns the number of times a particular integer occurs in an array of integers. The integer and the array will both be parameters.arrow_forwardjava Create a static method that: is called appendPosSum returns an ArrayList of Integers takes one parameter: an ArrayList of Integers This method should: Create a new ArrayList of Integers Add only the positive Integers to the new ArrayList Sum the positive Integers in the new ArrayList and add the Sum as the last element For example, if the incoming ArrayList contains the Integers (4,-6,3,-8,0,4,3), the ArrayList that gets returned should be (4,3,4,3,14), with 14 being the sum of (4,3,4,3). The original ArrayList should remain unchanged. public static void main(String[] args) { Scanner in = new Scanner(System.in); int size = in.nextInt(); ArrayList<Integer> list = new ArrayList<>(); for(int i=0; i < size; i++) { list.add(in.nextInt()); } System.out.println(appendPosSum(list));arrow_forward!Java on Eclpisearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





