Objective and Overview:
The exercises in this document reviews problem solving skills and basic programming concepts of:
Conditions, loops, methods, files handling and arrays in Java.
Simple data structures.
Students are required to individually implement the solutions to
each exercise given below. The successful completion of
these exercises enables the student to have the working knowledge of conditions, loops, methods and arrays in Java.
Problem 1:
Write the following methods in a Java project:
a) A Java method to generate randomly the element of an array (array received as a parameter).
b) A Java method to display the elements of an array (received as a parameter) on the screen (10 elements per
line).
c) A Java method to determine and return the number of common elements between two arrays (received as
parameters).
d) A Java method to sort an array (received as a parameter) in ascending order.
e) A Java method to reverse an array (received as a parameter).
f) A main method to test the above methods.
For the assignment, to test or execute the program two arrays of size 20 is taken.
The full working JAVA code is stated below:
Code:
import java.io.*;
import java.util.*;
public class Exercise{
public static void reverseTheArray(int[] arr){
for(int i=19;i>=0;i--){
System.out.print(arr[i]+" ");
}
System.out.println("");
}
public static void sortTheArray(int[] arr){
for(int i=0;i<20;i++){
for(int j=i+1;j<20;j++){
if(arr[i]>arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i=0;i<20;i++){
System.out.print(arr[i] + " ");
}
System.out.println("");
}
public static void returnCommonElements(int[] arr1,int[] arr2){
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
if(arr1[i] == arr2[j]){
System.out.print(arr1[i]+"
");
}
}
}
System.out.println("");
}
public static void generateRandomElementsforTheArray(int[] arr){
Random random = new Random();
for(int i=0;i<20;i++){
//will populate the array with numbers less than 30
arr[i] =
random.nextInt(30);
}
}
public static void displayTheArray(int[] arr){
int count = 0;
for(int i=0;i<20;i++){
System.out.print(arr[i]+" ");
count++;
if(count == 10){
//as we have to print 10 integer in a line
System.out.println("");
count=0;
}
}
}
public static void main(String[] args) {
//f) A main method to
test the above methods.
Scanner scan = new
Scanner(System.in);
//Two array of size
created for test the methods.
int[] arr1 = new int[20];
int[] arr2 = new
int[20];
//a) A Java method to generate
randomly the element of an array (array received as a
parameter).
// We have to populate two array,
so method is called twice. (arr1.arr2)
generateRandomElementsforTheArray(arr1);
generateRandomElementsforTheArray(arr2);
//b) A Java method to display the elements of an array (received as a parameter) on the screen (10 elements per line).
System.out.println("The
first array :");
displayTheArray(arr1);
System.out.println("The
second array :");
displayTheArray(arr2);
//c) A Java method to determine and return the number of common elements between two arrays (received as parameters).
System.out.println("The
common elements are : ");
returnCommonElements(arr1,arr2);
//d) A Java method to sort an array (received as a parameter) in ascending order.
System.out.println("Array 1 after sorting : ");
sortTheArray(arr1);
System.out.println("Array 2 after sorting : ");
sortTheArray(arr2);
//e) A Java method to reverse an array (received as a parameter).
System.out.println("The
reversed array : (array1)");
reverseTheArray(arr1);
System.out.println("The
reversed array : (array1)");
reverseTheArray(arr2);
}
}
Screenshot of full working code with output is attached below:
Get Answers For Free
Most questions answered within 1 hours.