1) Edit your file in your IDE and make the following changes/additions: a. Name
ID: 3711411 • Letter: 1
Question
1) Edit your file in your IDE and make the following changes/additions:
a. Name the file to Lab13XXxx.java, where XXxx is your initials and the next 2 letters of your last name.
b. Name your class to Lab13XXxx.
c. Have the first statement print out your name on the screen.
d. Write a static method that will take in an integer parameter and return an integer array filled with random numbers. The value of the integer parameter is the size of the array that you will return.
e. Display all the random numbers in their order in the array via another static method you write. This method takes two parameters – the int [] to print, and a second integer that is the number of values to display on each line.
f. Write a simple static method that takes an int [] as its only parameter and returns the largest value in the array.
g. Your main method then needs to:
i. Call the method that generates the random array and assign that array to a local variable.
ii. Pass that array and a value of your choosing to the output method.
iii. Call the method to find the maximum value. Display that value with a descriptive caption.
/Can comment out or leave package for now /7package com.company /Rename this class and source file per the lab spec public class Main ICan change this constant to a different amount public static final int NUM VALUES10; public static void main(String[] args) I Can change the quantity from 10 int [ data randomArray(NUM_VALUES) // You can change the # of items per line printArray(data, 3); System.out.println("Largest value is: findLargest (data)); public static int [] randomArray (int amount) //replace this with an array to return return null; public static void printArray (int [ array, int numPerLine) public static int findLargest(int [ array) /replace this with largest value return 0;Explanation / Answer
Main.java
import java.util.Random;
public class Main {
public static final int NUM_VALUES = 10;
public static void main(String[] args) {
int[] data = randomArray(NUM_VALUES);
printArray(data, 3);
System.out.println(" Largest value is "+findLargest(data));
}
public static int[] randomArray(int n) {
int a[] = new int[n];
Random r = new Random();
for(int i=0;i<a.length;i++) {
a[i]=r.nextInt(100)+1;
}
return a;
}
public static void printArray(int array[], int numPerLIne) {
for(int i=0;i<array.length;i++) {
if(i%numPerLIne==0) {
System.out.println();
}
System.out.print(array[i]+" ");
}
}
public static int findLargest(int[] array) {
int max = array[0];
for(int i=0;i<array.length;i++) {
if(max<array[i]) {
max = array[i];
}
}
return max;
}
}
Output:
11 41 37
26 19 24
13 54 41
63
Largest value is 63
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.