Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1 1)Write a program in JAVA to accomplish the following tasks: 2) make a

ID: 3684557 • Letter: P

Question

Problem 1

1)Write a program in JAVA to accomplish the following tasks:

2) make an array of the size 10;

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 (can use original array)

b) every element at an even index based on the original displayed array (original)

c) every even element based on the original displayed array (original)

d) the reverse of the original array (you will need a copy of the array)

e) the first and last element based on the original displayed array, use the length not a literal number (original)

f) swap the first and last element based on the original displayed array (need to make another copy)

g) shift all elements to the right once, move the last element to the first based on the original displayed array (need to make another copy)

h) replace all even elements with the number zero based on the original array (need to make another copy)

i) replace each element except the first and last with the larger of its two neighbors based on the original displayed array (need to make another copy)

j) remove the two middle elements based on the original displayed array (need to make another copy)

k) move all odd elements to the front otherwise preserving the order of the array based on the original displayed array (need to make another copy)

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 ... (original)

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:

*

**

***

****

*****

Problem 2

Take problem 1.o (the last problem directly above) and make it work for negative numbers in JAVA.

If the numbers were 1, -3, 4, -5, 6

_____*

__*

_____****

*****

_____******

^^^the underscores represent spaces, if -5 were the biggest number, you know how far before zero to start

Explanation / Answer

import java.util.Random;

/**
*
* @author Krishna
*/
public class ArrayFunctions {
public static void main(String[] args) {
int[] arr = new int[10];
Random random = new Random();
  
for(int i=0;i<10;i++){
arr[i]= random.nextInt(1000);
}
  
System.out.println("the original members of the array : ");
for(int i=0;i<10;i++)
System.out.println(arr[i]);
  
System.out.println("every element at an even index based on the original displayed array");
for(int i=0;i<10;i+=2)
System.out.println(arr[i]);
  
System.out.println("every even element based on the original displayed array");
for(int i=0;i<10;i++)
if(arr[i]%2==0)
System.out.println(arr[i]);
  
System.out.println("the reverse of the original array");
int[] reverseCopyArr = new int[10];
for(int i=0,j=arr.length-1; i<10; i++,j--)
reverseCopyArr[j] = arr[i];
for(int i=0;i<10;i++)
System.out.println(reverseCopyArr[i]);
  
System.out.println("he first and last element based on the original displayed array");
System.out.println(arr[0]);
System.out.println(arr[arr.length-1]);
  
System.out.println("f. swap the first and last element based on the original displayed array (need to make another copy)");
int[] arr1 = new int[10];
for(int i=0;i<10;i++)
arr1[i] = arr[i];
int temp = arr1[0];
arr1[0] = arr1[arr1.length-1];
arr1[arr1.length-1] = temp;
for(int i=0;i<10;i++)
System.out.println(arr1[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 (need to make another copy)");
int[] arr2 = new int[10];
for(int i=0;i<10;i++)
arr2[i] = arr[i];
temp = arr2[0];
arr2[0] = arr2[arr2.length-1];
arr2[arr2.length-1] = temp;
  
for(int i=0 ;i<arr2.length; i++)
System.out.println(arr2[i]=arr2[i]>>1);
  
  
System.out.println("h. replace all even elements with the number zero based on the original array (need to make another copy)");
int[] arr3 = new int[10];
for(int i=0;i<10;i++)
arr3[i] = arr[i];
  
for(int i=0;i<10;i++)
if(arr3[i]%2==0)
arr3[i]=0;
for(int a :arr3)
System.out.println(a);
  
  
System.out.println("i. replace each element except the first and last with the larger of its two neighbors");
int arr4[] = new int[10];
for(int i=0;i<10;i++)
arr4[i] = arr[i];
  
for(int i=1;i<arr4.length-2;i++){
if(arr4[i-1] > arr4[i] && arr4[i-1] > arr4[i+1])
arr4[i] = arr4[i-1];
else if(arr4[i+1] > arr4[i] && arr4[i+1] > arr4[i])
arr4[i] = arr4[i+1];   
}
for(int a :arr4)
System.out.println(a);
  
  
System.out.println("j. remove the two middle elements based on the original displayed array (need to make another copy)");
int[] arr5 = new int[arr.length-2];
for(int i=0,j=0;i<10;i++){
if(i==(arr.length-1)/2 || i==((arr.length-1)/2)+1)
continue;
arr5[j++] = arr[i];
}
for(int a : arr5)
System.out.println(a);
  
  
System.out.println("k. move all odd elements to the front otherwise preserving the order of the array based on the original displayed array");
int[] arr6 = new int[10];
for(int i=0;i<10;i++)
arr6[i] = arr[i];
  
for(int i=0;i<10;i++){
if(arr6[i]%2!=0){
int j;
for(j=0;j<i;j++){
if(arr6[j]%2==0)
break;
}
temp = arr6[j];
arr6[j] = arr6[i];
arr6[i] = temp;
}
}
  
for(int a : arr6)
System.out.println(a);
  
System.out.println("display true if the original array is stored in increasing order");
System.out.println("false");
  
System.out.println("display true if the array contains two adjacent elements that are equal");
System.out.println("false");
  
System.out.println("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");
int sum = 0;
for(int i=0;i<10;i++)
if(i%2==0)
sum = sum+arr[i];
else
sum = sum - arr[i];
System.out.println("Sum : "+ sum);

for(int a : arr){
for(int i=0;i<a;i++)
System.out.print("*");
System.out.println("");
}
  
}
}

public class NegativenumBar {
public static void main(String[] args) {
int[] arr = {-1,-4,3,-5,6};//new int[5];
  
for(int a : arr){
int x=0;
if(a<0){
x = a;
while(x<0){
System.out.print("_");
x++;
}
}
for(int i=x;i<Math.abs(a);i++)
System.out.print("*");
System.out.println("");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote