JAVA ONLY Write a program to accomplish the following tasks: 1) make a random nu
ID: 3573569 • Letter: J
Question
JAVA ONLY
Write a program to accomplish the following tasks:
1) make a random number between 10 and 20;
2) make an array of the size of the random number;
3) fill the array with random integers from 0 to 999;
4) print lines of output for each of these:
a) the original members of the array
b) every element at an even index based on the original displayed array
c) every even element based on the original displayed array
d) the reverse of the original array
e) the first and last element based on the original displayed array, use the length not a literal number
f) swap the first and last element based on the original displayed array
g) shift all elements to the right once, move the last element to the first based on the original displayed array
h) replace all even elements with the number zero based on the original array
i) replace each element except the first and last with the larger of its two neighbors based on the original displayed array
j) remove the two middle elements based on the original displayed array
k) move all odd elements to the front otherwise preserving the order of the array based on the original displayed array
l) display true if the original array is stored in increasing order
m) display true if the array contains two adjacent elements that are equal
n) compute the alternating sum of all elements in the array, for example: 1 2 3 4 5 would be computed as 1 - 2 + 3 - 4 + 5 ...
o) display a bar chart equal to the value of the number in each element, for example: 1 2 3 4 5 would display as:
*
**
***
****
*****
Explanation / Answer
import java.util.Random;
public class NumberArray {
/**
* @param args
*/
public static void main(String[] args) {
Random random = new Random();
// 1) make a random number between 10 and 20;
// r can take value 0-9
int r = random.nextInt(9);
// Generate a random number between 10 and 20 ie. from 11-19 by adding r
// to 11
int randomInt = 11 + r;
System.out.println("Size of the Array: " + randomInt);
// 2) make an array of the size of the random number;
int[] original = new int[randomInt];
// 3) fill the array with random integers from 0 to 999;
for (int i = 0; i < original.length; i++) {
original[i] = random.nextInt(1000);
}
// 4) Print original numbers
// a) the original members of the array
System.out.print("Original numbers: ");
for (int i = 0; i < original.length; i++) {
System.out.print(original[i] + " ");
}
System.out.println("");
// b) every element at an even index based on the original displayed
// array
System.out.print("Elements at even index: ");
for (int i = 0; i < original.length; i++) {
if (i % 2 == 0) {
System.out.print(original[i] + " ");
}
}
System.out.println("");
// c) every even element based on the original displayed array
System.out.print("Even Elements: ");
for (int i = 0; i < original.length; i++) {
if (original[i] % 2 == 0) {
System.out.print(original[i] + " ");
}
}
System.out.println("");
// d) reverse of the original array
System.out.print("Revers of the original array: ");
for (int i = original.length - 1; i >= 0; i--) {
System.out.print(original[i] + " ");
}
System.out.println("");
// e) first and last element based on the original displayed array
System.out.println("First element of the original array: "
+ original[0]);
System.out.println("Last element of the original array: "
+ original[original.length - 1]);
// f) first and last element swapped based on the original displayed
// array
int[] swappedArray = new int[randomInt];
// Copy the original array to swappedArray
for (int i = 0; i < original.length; i++) {
swappedArray[i] = original[i];
}
// Swap the first and last element of the swapped Array
int temp = swappedArray[0];
swappedArray[0] = swappedArray[swappedArray.length - 1];
swappedArray[swappedArray.length - 1] = temp;
System.out.print("Swapped first and last elements: ");
for (int i = 0; i < swappedArray.length; i++) {
System.out.print(swappedArray[i] + " ");
}
System.out.println("");
// g) shift all elements to the right once, move the last element to the
// first based on the original displayed array
int[] shiftedArray = new int[randomInt];
// Copy elements from the original array to shiftedArray by shifting 1
// position to right
for (int i = 0; i < original.length - 1; i++) {
shiftedArray[i + 1] = original[i];
}
shiftedArray[0] = original[original.length - 1];
System.out.print("Elements shifted one position to right: ");
for (int i = 0; i < shiftedArray.length; i++) {
System.out.print(shiftedArray[i] + " ");
}
System.out.println("");
// h) replace all even elements with the number zero based on the
// original array
int[] evenElementsWithZero = new int[randomInt];
// Set even elements from the original array to 0
for (int i = 0; i < original.length; i++) {
if (original[i] % 2 == 0) {
evenElementsWithZero[i] = 0;
} else {
evenElementsWithZero[i] = original[i];
}
}
System.out.print("Elements with zero for even elements: ");
for (int i = 0; i < evenElementsWithZero.length; i++) {
System.out.print(evenElementsWithZero[i] + " ");
}
System.out.println("");
// i) replace each element except the first and last with the larger of
// its two neighbors based on the original displayed array
int[] largerNeighbors = new int[randomInt];
// Copy elements from the original array to shiftedArray by shifting 1
// position to right
largerNeighbors[0] = original[0];
largerNeighbors[original.length - 1] = original[original.length - 1];
for (int i = 1; i < original.length - 1; i++) {
largerNeighbors[i] = Math.max(original[i - 1], original[i + 1]);
}
System.out
.print("Elements with larger of its two neighbors except for the first and last: ");
for (int i = 0; i < largerNeighbors.length; i++) {
System.out.print(largerNeighbors[i] + " ");
}
System.out.println("");
// j) remove the two middle elements based on the original displayed
// array
int middleElement = 0;
if (randomInt % 2 == 1) {
middleElement = (randomInt / 2);
} else {
middleElement = (randomInt / 2) - 1;
}
int[] arrayMiddleElementsRemoved = new int[randomInt - 2];
for (int i = 0; i < middleElement; i++) {
arrayMiddleElementsRemoved[i] = original[i];
}
for (int i = middleElement + 2; i < original.length; i++) {
arrayMiddleElementsRemoved[i - 2] = original[i];
}
System.out.print("Elements with middle elements removed: ");
for (int i = 0; i < arrayMiddleElementsRemoved.length; i++) {
System.out.print(arrayMiddleElementsRemoved[i] + " ");
}
System.out.println("");
// k) replace each element except the first and last with the larger of
// its two neighbors based on the original displayed array
int[] oddElements = new int[randomInt];
int oddIndex = 0;
int[] evenElements = new int[randomInt];
int evenIndex = 0;
// Copy elements from the original array to shiftedArray by shifting 1
// position to right
for (int i = 0; i < original.length; i++) {
if (original[i] % 2 == 1) {
oddElements[oddIndex] = original[i];
oddIndex++;
} else {
evenElements[evenIndex] = original[i];
evenIndex++;
}
}
System.out.print("Elements with odd elements at first : ");
for (int i = 0; i < oddIndex; i++) {
System.out.print(oddElements[i] + " ");
}
for (int i = 0; i < evenIndex; i++) {
System.out.print(evenElements[i] + " ");
}
System.out.println("");
// l) Display true original array is stored in increasing order
boolean increasingOrder = true;
for (int i = 0; i < original.length - 1; i++) {
if (original[i] > original[i + 1]) {
increasingOrder = false;
break;
}
}
System.out.println("Is original array stored in increasing order: "
+ increasingOrder);
// m) display true if the array contains two adjacent elements that are
// equal
boolean adjacentEqual = false;
for (int i = 0; i < original.length - 1; i++) {
if (original[i] == original[i + 1]) {
adjacentEqual = true;
break;
}
}
System.out
.println("Does if the array contains two adjacent elements that are equal: "
+ adjacentEqual);
// n) compute the alternating sum of all elements in the array
int sum = 0;
int sign = 1;
for (int i = 0; i < original.length; i++) {
sum += sign * original[i];
sign = (sign == -1) ? 1 : -1;
}
System.out.println("Alternating sum of the array: " + sum);
System.out.println("Bar Chart: ");
// o) display a bar chart equal to the value of the number in each
// element
for (int i = 0; i < original.length; i++) {
int num = original[i];
for (int k = 0; k < num; k++) {
System.out.print("*");
}
System.out.println("");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.