0% found this document useful (0 votes)
3 views

import java.util.;

The document contains a Java class named ArrayActivity1 that manages two arrays: a default array and a custom array filled with random numbers. It includes methods to display the array elements, calculate the average, find the maximum value, sort the array, perform a linear search, tally the frequency of elements, and display elements alongside their reverse and average. The class demonstrates basic array operations and manipulations in Java.

Uploaded by

gouraiahnaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

import java.util.;

The document contains a Java class named ArrayActivity1 that manages two arrays: a default array and a custom array filled with random numbers. It includes methods to display the array elements, calculate the average, find the maximum value, sort the array, perform a linear search, tally the frequency of elements, and display elements alongside their reverse and average. The class demonstrates basic array operations and manipulations in Java.

Uploaded by

gouraiahnaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

*;

public class ArrayActivity1 {


private int[] defaultArray;
private int[] customArray;

// 1. Constructs the default array of size 10


public ArrayActivity1() {
defaultArray = new int[]{1, 3, 7, 19, 15, 19, 7, 3, 19, 48};
}

// 2. Constructs and display an array of random numbers (0-24) of size count


public ArrayActivity1(int count) {
customArray = new int[count];
Random rand = new Random();
for (int i = 0; i < count; i++) {
customArray[i] = rand.nextInt(25);
}
System.out.println(Arrays.toString(customArray));
}

// 3. Print all of the elements of the array in list form


public void display() {
for (int num : defaultArray) {
System.out.println(num);
}
}

// 4. Print all of the elements in reverse order


public void displayReverse() {
for (int i = defaultArray.length - 1; i >= 0; i--) {
System.out.println(defaultArray[i]);
}
}

// 5. Calculate and return the average of all of the elements


public double average() {
int sum = 0;
for (int num : defaultArray) {
sum += num;
}
return (double) sum / defaultArray.length;
}

// 6. Return the maximum value of all of the elements


public int findMax() {
int max = defaultArray[0];
for (int num : defaultArray) {
if (num > max) {
max = num;
}
}
return max;
}

// 7. Sort the array in ascending order and print


public void sort() {
int[] sortedArray = defaultArray.clone();
Arrays.sort(sortedArray);
for (int num : sortedArray) {
System.out.println(num);
}
}

// 8. Return the index number of the first instance of int lookFor


public int linearSearch(int lookFor) {
for (int i = 0; i < defaultArray.length; i++) {
if (defaultArray[i] == lookFor) {
return i;
}
}
return -1;
}

// 9. Print the elements and their frequency


public void tallyList() {
Map<Integer, Integer> frequencyMap = new HashMap<>();
for (int num : defaultArray) {
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
}

for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {


System.out.println(entry.getKey() + " : " + entry.getValue() + "
times");
}
}

// 10. Print original order, reverse order, and average of the two
public void listReverseAvg() {
int[] reverseArray = new int[defaultArray.length];
for (int i = 0; i < defaultArray.length; i++) {
reverseArray[i] = defaultArray[defaultArray.length - 1 - i];
}

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


double avg = (defaultArray[i] + reverseArray[i]) / 2.0;
System.out.println(defaultArray[i] + " " + reverseArray[i] + " " +
avg);

You might also like