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

import java.util.Random;

The document contains a Java class named 'Numbers' that manages an array of integers with various functionalities. It includes methods for constructing the array, displaying elements, calculating the average, finding the maximum value, performing a linear search, tallying frequencies, and sorting the array. Additionally, it provides methods to display elements in reverse order and to list elements alongside their averages.

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.Random;

The document contains a Java class named 'Numbers' that manages an array of integers with various functionalities. It includes methods for constructing the array, displaying elements, calculating the average, finding the maximum value, performing a linear search, tallying frequencies, and sorting the array. Additionally, it provides methods to display elements in reverse order and to list elements alongside their averages.

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.

Random;

public class Numbers {


private int[] numbArray;

// 1. Constructs a default array of size 10


public Numbers() {
numbArray = new int[10];
numbArray[0] = 1;
numbArray[1] = 3;
numbArray[2] = 7;
numbArray[3] = 19;
numbArray[4] = 15;
numbArray[5] = 19;
numbArray[6] = 7;
numbArray[7] = 3;
numbArray[8] = 19;
numbArray[9] = 48;
}

// 2. Constructs an array of random numbers (min 0 - max 24) with array size
count
public Numbers(int count) {
Random random = new Random();
numbArray = new int[count];
for (int i = 0; i < count; i++) {
numbArray[i] = random.nextInt(25);
}
}

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


public void display() {
for (int num : numbArray) {
System.out.print(num + " ");
}
System.out.println();
}

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


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

// 5. Calculates and returns the average of all of the elements


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

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


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

// 7. Returns the index number of the first instance of int lookFor, -1 if not
found
public int linearSearch(int lookFor) {
for (int i = 0; i < numbArray.length; i++) {
if (numbArray[i] == lookFor) {
return i;
}
}
return -1;
}

// 8. Prints the elements and their frequency


public void tallyList() {
int[] frequency = new int[50];
for (int num : numbArray) {
frequency[num]++;
}
System.out.println("Number\tFrequency");
for (int i = 0; i < frequency.length; i++) {
if (frequency[i] > 0) {
System.out.println(i + "\t" + frequency[i]);
}
}
}

// 9. Prints a column of numbers in the original order, reverse order, and


their average
public void listReverseAvg() {
System.out.println("LIST\tREVERSE\tAVERAGE");
for (int i = 0; i < numbArray.length; i++) {
int reverseNum = numbArray[numbArray.length - 1 - i];
double avg = (numbArray[i] + reverseNum) / 2.0;
System.out.println(numbArray[i] + "\t" + reverseNum + "\t" + avg);
}
}

// 10. Sorts the array in ascending order


public void sort() {
for (int i = 0; i < numbArray.length - 1; i++) {
for (int j = i + 1; j < numbArray.length; j++) {
if (numbArray[i] > numbArray[j]) {
int temp = numbArray[i];
numbArray[i] = numbArray[j];
numbArray[j] = temp;
}
}
}

You might also like