Question

Java question Q1) Use the following code snippet which generates a random sized array with random...

Java question

Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems:

public int [] createRandomArray() {

        int size = (int) (Math.random() * 10) + 1;

        int[] array = new int [size];

        for (int i = 0; i < array.length; i++) {

            array[i] = (int) (Math.random() * 10 ) + 1;

        }

        return array;

    }

Assignment name: ArrayManiuplation2 Write a program which has the following methods:

1. Takes in an integer array as parameter and returns the sum of all the even numbers in the array.

2. Takes in an integer array as parameter and returns the difference between the largest number of the array and the smallest number of the array.

3. Takes in an integer array and prints out each even number as it encounters it.

4. Takes in an integer array and determines whether the last element and the first element are equal to one another. Returns a boolean.

5. Takes in an integer array as a parameter determines if the number 2 is an element.

6. Write a driver class in another file which exercises all the methods that you created.

Q2) Assignment name: MultiDimensionArrays

Write a program which populates a 5x5 two dimensional array with random integers. Write a driver class which prints out each element in the 2D array.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1)

Code

All the methods area written in the ArrayManiuplation2.java
public class ArrayManiuplation2
{
public int [] createRandomArray()
{
int size = (int) (Math.random() * 10) + 1;
int[] array = new int [size];
for (int i = 0; i < array.length; i++)
{
array[i] = (int) (Math.random() * 10 ) + 1;
}
return array;
}
public int sumEven(int []a)
{
int sum=0;
for(int i=0;i<a.length;i++)
sum+=a[i];
return sum;
}
public int diffLargestSmallesy(int []a)
{
int max=a[0];
int min=a[0];
for(int i=1;i<a.length;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
return max-min;
}
public void printEven(int []a)
{
for(int i=0;i<a.length;i++)
if(a[i]%2==0)
System.out.print(a[i]+" ");
}
public boolean checkFirstLastElement(int []a)
{
return a[0]==a[a.length-1];
}
public boolean check2Element(int []a)
{
for(int i=0;i<a.length;i++)
if(a[i]==2)
return true;
return false;
}
}

Main.java that will test all the method that we created above


public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
ArrayManiuplation2 obj = new ArrayManiuplation2();
int []array=obj.createRandomArray();
System.out.println("The array is : ");
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+" ");
}
System.out.println("\n\nSum of all the even numbers is: "+obj.sumEven(array));
System.out.println("\nThe difference between the largest number and the smallest number of the array: "+obj.diffLargestSmallesy(array));
System.out.println("\nAll the even number in the array are:");
obj.printEven(array);
System.out.println("\nLast anad first element are same ot not? "+obj.checkFirstLastElement(array));
System.out.println("\n2 is the element of the array? "+obj.check2Element(array));
}
  
}

output

Q2)

MultiDimensionArrays.java


public class MultiDimensionArrays
{
int[][] a = new int[5][5];
public int [][] createRandom2DArray()
{
int[][] a = new int[5][5];
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
a[i][j]=(int) (Math.random() * 10 ) + 1;
return a;
}
}

Driver.java


public class Driver {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MultiDimensionArrays obj = new MultiDimensionArrays();
int [][]array2D=obj.createRandom2DArray();
System.out.println("2D array with random values is: \n");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
System.out.print(array2D[i][j]+"\t");
System.out.println("");
}
}
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Java question Q1) Use the following code snippet which generates a random sized array with random...
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
  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are...

    In Java* ​​​​​​​ Write a program that reads an arbitrary number of 20 integers that are in the range 0 to 100 inclusive. The program will ask a user to re-enter an integer if the user inputs a number outside of that range. The inputted integers must then be stored in a single dimensional array of size 20. Please create 3 methods: 1. Write a method public static int countEven(int[] inputArray) The method counts how many even numbers are in...

  • In Java write the following array- processing methods into the same application, Lab13.java. Use the main...

    In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1...

    Using JAVA, write an application that uses an Array of 30 Numbers (random integers from 1 - 100) and returns the maximum number, minimum number, average of all numbers, and prints a Bar Chart to show the number distribution (1-9, 10-19,20-29, …80-89, 90-100). Note; maximum number, minimum number, average number, and the Bar Chart must use implemented as separate methods in a separate class. Method call should pass an array as an argument. Methods should accept an array as an...

  • The code snippet below is an example of pass by reference. We also provide a sample...

    The code snippet below is an example of pass by reference. We also provide a sample method setZeroAt, which sets 0 at a position i in the array, to illustrate pass by reference. public class Sample { public static void setZeroAt(int [] arr, int i){ arr[i] = 0; } public static void main(String[] args) { Scanner input = new Scanner(System.in); int sample[] = {1, 2, 3}; setZeroAt(sample, 1); //Now sample looks like {1, 0, 3} } } Write a Java...

  • Write a JAVA program with methods that initializes an array with 20 random integers between 1...

    Write a JAVA program with methods that initializes an array with 20 random integers between 1 and 50 and then prints four lines of output, containing 1)The initialized array. 2)Every element at an even index. 3)Every even element. 4)All elements in reverse order. Requirements (and hints): 1. Build a method that takes any integer array as input and prints the elements of the array. ALL printing in this lab must be done using a call to the printArray method and...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

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