Write one Java application based on the following directions and algorithms. Mak
ID: 3788958 • Letter: W
Question
Write one Java application based on the following directions and algorithms. Make sure the results from each of the numbered items is separate and easy to read/understand.
1.Create an array that will hold 4 string values. Write three separate statements to store your favorite color, your favorite song, and your favorite restaurant in the array elements. I do not want you to use a loop in this logic; use hard-coded index values instead. Print *ALL FOUR* of the values of the array using individual index values. What is stored in the element you did not fill?
2.Create an array that will hold 5 string values. Use a for loop to prompt the user to enter their 5 favorite movies and store each one in a separate element of the array. Print the values of the array using a For-Each loop.
3.Create an array to hold 7 doubles. Use any looping structure you want to prompt the user to enter a value and store it in the array. In a separate loop, compute the sum of the values. In a third, separate loop, display the array values in reverse order. Also display the sum.
4.Create an array that will hold 3 integers. Without prompting the user for input or using a loop, fill that array with the values 23, 0, and -5. Write decision logic to determine whether the values in each array element is negative, positive, or equal to zero. Make the decision logic loop 3 times, since we want to test 3 different values.
Explanation / Answer
Hi, Please find my implementation.
import java.util.Scanner;
public class ArrayOperation {
public static void main(String[] args) {
//1.
String[] strArray = new String[4];
strArray[0] = "Yellow"; // favorite color
strArray[1] = "Wakka Wakka"; // song
strArray[2] = "Pettu"; //resturant
System.out.println(strArray[0]);
System.out.println(strArray[1]);
System.out.println(strArray[2]);
System.out.println(strArray[3]);
System.out.println();
// null is stored at 3rd location
//2.
// to take user input
Scanner sc = new Scanner(System.in);
// creating array
String[] strArr = new String[5];
// taking user input
for(int i=0; i<5; i++){
System.out.print("Enter "+(i+1)+" value: ");
strArr[i] = sc.nextLine();
}
// printing
for(int i=0; i<5; i++){
System.out.println(strArr[i]);
}
System.out.println();
//3.
double[] dArr = new double[7];
// taking user input
for(int i=0; i<7; i++){
System.out.print("Enter "+(i+1)+" double value: ");
dArr[i] = sc.nextDouble();
}
// computing sum
double sum = 0;
for(int i=0; i<7; i++){
sum = sum + dArr[i];
}
System.out.println("Sum: "+sum);
for(int i=6; i>=0; i--){
System.out.println(dArr[i]);
}
System.out.println();
//4.
int[] iArr = new int[3];
for(int i=0; i<3; i++){
if(i==0)
iArr[i] = 23;
else if(i == 1)
iArr[i] = 0;
else
iArr[i] = -5;
}
for(int i=0; i<3; i++){
if(iArr[i] > 0)
System.out.println("Value at "+(i+1)+"th index is positive");
else if(iArr[i] <0)
System.out.println("Value at "+(i+1)+"th index is negative");
else
System.out.println("Value at "+(i+1)+"th index is zero");
}
}
}
/*
Sample run:
Yellow
Wakka Wakka
Pettu
null
Enter 1 value: pk
Enter 2 value: mk
Enter 3 value: vk m
Enter 4 value: rk
Enter 5 value: nk
pk
mk
vk m
rk
nk
Enter 1 double value: 3
Enter 2 double value: 2.3
Enter 3 double value: 8.9
Enter 4 double value: 6
Enter 5 double value: 7.8
Enter 6 double value: 9.0
Enter 7 double value: 1.2
Sum: 38.2
1.2
9.0
7.8
6.0
8.9
2.3
3.0
Value at 1th index is positive
Value at 2th index is zero
Value at 3th index is negative
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.