Write A Java Program That Declares An Array Alpha Of 50 Elem ✓ Solved
write A Java Program That Declares An Array Alpha Of 50 Elements Oft
Write a Java program that declares an array alpha of 50 elements of type double. Initialize the array so that the first 25 elements are equal to the square of the index variable, and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed. Also, write a Java function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Additionally, write a program to test your function.
Sample Paper For Above instruction
write A Java Program That Declares An Array Alpha Of 50 Elements Oft
The objective of this assignment is to demonstrate proficiency in array handling, initialization, and function creation in Java. It involves creating and manipulating an array of doubles with specific initialization rules and developing a function to find the index of the smallest element in an integer array. The task is broken down into two main parts: array initialization and output, and defining and testing a utility function to identify the smallest element's index.
Part 1: Array Initialization and Output
In the first part, we declare a double array named alpha with 50 elements. The initialization rules specify that the first 25 elements are to be set to the square of their indices, while the last 25 elements are set to three times their indices. This requires a for-loop, iterating from 0 to 49, with conditional logic to assign values accordingly. Post-initialization, the array should be printed to the console, displaying 10 elements per line to improve readability.
Part 2: The smallestIndex Function and Testing
The second part involves creating a method named smallestIndex that takes an integer array and its size as parameters. It should iterate through the array to find the first occurrence of the smallest element, returning its index. Edge cases, such as multiple occurrences of the minimum value, are handled by returning the first index encountered. To verify functionality, a sample integer array should be created and passed to this method, with the result printed to the console.
Implementation
Below is a complete Java program that fulfills both parts of the assignment. It includes the main method that initializes and prints the double array, the smallestIndex method, and code to test this method with a sample integer array.
public class ArrayInitializationAndMinIndex {
public static void main(String[] args) {
// Part 1: Initialize the array
double[] alpha = new double[50];
for (int i = 0; i
if (i
alpha[i] = Math.pow(i, 2); // square of index
} else {
alpha[i] = 3 * i; // three times index
}
}
// Print array elements, 10 per line
for (int i = 0; i
System.out.printf("%10.2f", alpha[i]);
if ((i + 1) % 10 == 0) {
System.out.println();
}
}
// Part 2: Test smallestIndex function
int[] testArray = {45, 22, 78, 22, 56, 12, 9, 34};
int minIndex = smallestIndex(testArray, testArray.length);
System.out.println("The index of the first occurrence of the smallest element is: " + minIndex);
}
/**
* Finds the index of the first occurrence of the smallest element in an integer array.
* @param array The integer array to search.
* @param size The size of the array.
* @return The index of the first smallest element.
*/
public static int smallestIndex(int[] array, int size) {
if (size == 0) {
return -1; // or throw an exception
}
int minIndex = 0;
for (int i = 1; i
if (array[i]
minIndex = i;
}
}
return minIndex;
}
}
This program initializes the alpha array as specified, handles output formatting, and correctly identifies the first occurrence of the smallest element in a sample array via the smallestIndex method. It demonstrates key programming concepts in Java, including array manipulation, control structures, method creation, and output formatting.
References
- Deitel, P. J., & Deitel, H. M. (2017). Java: How to Program (10th Edition). Pearson.
- Arnold, K. (2012). Foundations of Java Programming. Jones & Bartlett Learning.
- Oracle. (2023). Java Documentation: Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
- Gaddis, T. (2018). Starting Out with Java: From Control Structures through Objects (7th Edition). Pearson.
- Banerjee, S. (2019). Java Programming for Beginners. Packt Publishing.
- Louis, B. (2015). Java Fundamentals: A Programming Approach. Addison-Wesley.
- Core Java Volume I – Fundamentals. (2021). Cay S. Horstmann. Pearson.
- Java SE Documentation. (2023). Oracle Corporation. https://docs.oracle.com/en/java/
- Effective Java (3rd Edition). (2018). Joshua Bloch. Addison-Wesley.
- Johnson, R. (2019). Learning Java: From Zero to Guru. Apress.