the out put should be look like Question 1. (Arrays java, 20 marks) Write a prog
ID: 3573014 • Letter: T
Question
the out put should be look like
Question 1. (Arrays java, 20 marks) Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from the array that have even numbered indexes. Do not use an if statement here. Prompt the user to enter a number and count the occurrences of that number in the array. Use a separate for loop for each task. All of this work can be done in a single main method Sample output will be posted to D2L Question 2. (ArrayLists.java, 20 marks) Repeat all of the functionality of Question 1 but this time use ArrayLists instead of Arrays. Also, add a prompt to allow the user to delete the value at a specified index. Check that the index is valid and if it is not, do nothing. You can do all the work in a single main method. Sample output will be posted to D2L.Explanation / Answer
Arrays.java
import java.util.Scanner;
public class Arrays {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int n = scan.nextInt();
int a[] = new int[n];
for(int i=0; i<a.length; i++){
System.out.print("Enter the element: ");
a[i]=scan.nextInt();
}
System.out.println("The array elements on single line: ");
for(int i=0; i<a.length; i++){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("The array elements in reverse: ");
for(int i=a.length-1;i>=0; i--){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Even numbers in array are: ");
for(int i=0; i<a.length; i++){
if(a[i]%2 ==0)
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("Even index elements in array are: ");
for(int i=0; i<a.length; i+=2){
System.out.print(a[i]+" ");
}
System.out.println();
System.out.print("Enter the number that to be searched in an array: ");
int search = scan.nextInt();
int count = 0;
for(int i=0; i<a.length; i++){
if(a[i] == search){
count++;
}
}
System.out.println("Number of occurances in ann array is "+count);
}
}
Output:
Enter the array size: 5
Enter the element: 1
Enter the element: 2
Enter the element: 3
Enter the element: 4
Enter the element: 5
The array elements on single line:
1 2 3 4 5
The array elements in reverse:
5 4 3 2 1
Even numbers in array are:
2 4
Even index elements in array are:
1 3 5
Enter the number that to be searched in an array: 3
Number of occurances in ann array is 1
ArrayLists.java
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayLists {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter an integer for the number of elements in your array: ");
int n = scan.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0; i<n; i++){
System.out.print("Please enter the value for element number "+(i+1)+": ");
list.add(scan.nextInt());
}
System.out.println("Printing the array in order.");
for(int i=0;i<list.size(); i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("Printing the array reversed: ");
for(int i=list.size()-1;i>=0; i--){
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("Printing the even values.: ");
for(int i=0; i<list.size(); i++){
if(list.get(i)%2 ==0)
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("Printing the values with even index (assuming 0 is even): ");
for(int i=0; i<list.size(); i+=2){
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.print("Please enter a value to count the occurrences: : ");
int search = scan.nextInt();
int count = 0;
for(int i=0; i<list.size(); i++){
if(list.get(i) == search){
count++;
}
}
System.out.println("Number of occurances in ann array is "+count);
System.out.println("Enter an index to remove: ");
int index = scan.nextInt();
System.out.println("Removed a "+list.get(index));
list.remove(index);
System.out.println();
System.out.println("Re-printing previous outputs with the new array:");
System.out.println("Printing the array in order.");
for(int i=0;i<list.size(); i++){
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("Printing the array reversed: ");
for(int i=list.size()-1;i>=0; i--){
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("Printing the even values.: ");
for(int i=0; i<list.size(); i++){
if(list.get(i)%2 ==0)
System.out.print(list.get(i)+" ");
}
System.out.println();
System.out.println("Printing the values with even index (assuming 0 is even): ");
for(int i=0; i<list.size(); i+=2){
System.out.print(list.get(i)+" ");
}
System.out.println();
count = 0;
for(int i=0; i<list.size(); i++){
if(list.get(i) == search){
count++;
}
}
System.out.println("Number of occurances in ann array is "+count);
}
}
Output:
Please enter an integer for the number of elements in your array: 10
Please enter the value for element number 1: 7
Please enter the value for element number 2: 9
Please enter the value for element number 3: 7
Please enter the value for element number 4: 6
Please enter the value for element number 5: 2
Please enter the value for element number 6: 5
Please enter the value for element number 7: 5
Please enter the value for element number 8: 8
Please enter the value for element number 9: 2
Please enter the value for element number 10: 10
Printing the array in order.
7 9 7 6 2 5 5 8 2 10
Printing the array reversed:
10 2 8 5 5 2 6 7 9 7
Printing the even values.:
6 2 8 2 10
Printing the values with even index (assuming 0 is even):
7 7 2 5 2
Please enter a value to count the occurrences: : 8
Number of occurances in ann array is 1
Enter an index to remove:
5
Removed a 5
Re-printing previous outputs with the new array:
Printing the array in order.
7 9 7 6 2 5 8 2 10
Printing the array reversed:
10 2 8 5 2 6 7 9 7
Printing the even values.:
6 2 8 2 10
Printing the values with even index (assuming 0 is even):
7 7 2 8 10
Number of occurances in ann array is 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.