1. Create a new class called Driver. When you define the Constructor initialize
ID: 3629881 • Letter: 1
Question
1. Create a new class called Driver. When you define the Constructor initialize three arrays of type int as local variables. The arrays can have any combination of values but should have at least one element in them. For example you could create the following 3 arrays:
• a1: 16, 12, 25, 44, 78, 2, 105
• a2: 587, 23, 8975, 19, 1024, 8946, 32
• a3: 42
2. Define a method called findMin that takes an array of int as a parameter and returns the minimum value in the array.
3. Create a for loop that looks at each element in the array to find the smallest value in the array.
4. Add the following lines to your constructor and test the results.
System.out.println(findMin(a1)); // result should be 2
System.out.println(findMin(a2)); // result should be 19
System.out.println(findMin(a3)); // result should be 42
5.Write a method called countLastDigits that accepts and array of integers as a parameter and examines its elements to determine how many of the integers end in 0, how many end in 1, how many end in 2, and so on.Your method will return an array of counters. If a number ends with 0 then the counter at position [0] should be increased by 1. If a number ends with 1 then the counter at position [1]
should be increased by 1, and so on.
6. Your method will return an array of counters. If a number ends with 0 then the counter at position [0] should be increased by 1. If a number ends with 1 then the counter at position [1] should be increased by 1, and so on.
For the values assigned to a1 in the task above you should return the following array of values:
[0, 0, 2, 0, 1, 2, 1, 0, 1, 0]
Explanation / Answer
This is the first 6 parts
import java.util.Random;
class Driver{
int a1[];
int a2[];
int a3[];
Driver(int a1Count,int a2Count, int a3Count){
Random r = new Random();
a1=new int[a1Count];
a2=new int[a2Count];
a3=new int[a3Count];
for (int x=0;x<a1.length;x++)
a1[x]=r.nextInt(50)+1;
for (int x=0;x<a2.length;x++)
a2[x]=r.nextInt(50)+1;
for (int x=0;x<a3.length;x++)
a3[x]=r.nextInt(50)+1;
/*
a1=new int[7];
a1[0]=16;
a1[1]=12;
a1[2]=25;
a1[3]=44;
a1[4]=78;
a1[5]=2;
a1[6]=105;
a2=new int[7];
a2[0]=587 ;
a2[1]=23;
a2[2]=8975;
a2[3]=19;
a2[4]=1024;
a2[5]=8946;
a2[6]= 32;
a3=new int[1];
a3[0]=42;
*/
System.out.println("Printing a1:");
printArr(a1);
System.out.println("Printing a2:");
printArr(a2);
System.out.println("Printing a3:");
printArr(a3);
System.out.println(findMin(a1)); // result should be 2
System.out.println(findMin(a2)); // result should be 19
System.out.println(findMin(a3)); // result should be 42
System.out.println("The lastDigitCount for a1=:");
printArr(countLastDigits(a1));
System.out.println("The lastDigitCount for a2=:");
printArr(countLastDigits(a2));
System.out.println("The lastDigitCount for a3=:");
printArr(countLastDigits(a3));
}
public int findMin(int []arr){
int min=Integer.MAX_VALUE;
for(int x=0;x<arr.length;x++) {
if(arr[x]<min){
min=arr[x];
}
}
return min;
}
public void printArr(int arr[]){
System.out.print("[");
for(int x=0;x<arr.length;x++){
System.out.print(arr[x]);
if (x<arr.length-1){
System.out.print(", ");
}
}
System.out.println("]");
}
public int[] countLastDigits(int []arr){
int tmp[]=new int[10];
for(int x=0;x<tmp.length;x++)
tmp[x]=0;
for(int x=0;x<arr.length;x++){
tmp[arr[x]%10]+=1;
}
return tmp;
}
public static void main(String[] args){
Driver d= new Driver(7,20,13);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.