In: Computer Science
Write one Java application based on the following directions and
algorithms. Make sure the results from each of the bulleted items
is separate and easy to read/understand.
// Java program to perform operations on array
public class ArrayManip
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String strValues[] = new String[4];
strValues[0] = "Color1";
strValues[1] = "Song1";
strValues[2] = "Restaurant1";
System.out.println("String values in the array : ");
System.out.println(strValues[0]);
System.out.println(strValues[1]);
System.out.println(strValues[2]);
System.out.println(strValues[3]); // The element that is not filled contains null
System.out.println();
// Create an array that will hold 5 string values.
String movies[] = new String[5];
// for loop to prompt the user to enter and store their 5 favorite movies
for(int i=0;i<movies.length;i++)
{
System.out.print("Enter movie-"+(i+1)+" : ");
movies[i] = scan.nextLine();
}
System.out.println("For each loop to print the movies : ");
// Print the values of the array using a For-Each loop.
for(String s: movies)
{
System.out.println(s);
}
System.out.println();
// Create an array to hold 7 doubles.
double dblValues[] = new double[7];
// loop to prompt the user to enter a value and store it in the array
for(int i=0;i<dblValues.length;i++)
{
System.out.print("Enter double value-"+(i+1)+" : ");
dblValues[i] = scan.nextDouble();
}
// In a separate loop, compute the sum of the values.
double sum = 0;
for(int i=0;i<dblValues.length;i++)
{
sum += dblValues[i];
}
System.out.println("Sum of elements : "+sum); // print the sum of the values
System.out.println("Elements in reverse order : ");
//In a third, separate loop, display the array values in reverse order.
for(int i=dblValues.length-1;i>=0;i--)
{
System.out.print(dblValues[i]+" ");
}
System.out.println("\n");
int intValues[] = {23,0,-5}; // Create an array that will hold 3 integers with the values 23, 0, and -5
// loop to print if the values are positive, negative or zero
for(int i=0;i<intValues.length;i++)
{
if(intValues[i] < 0)
System.out.println(intValues[i]+" is negative");
else if(intValues[i] > 0)
System.out.println(intValues[i]+" is positive");
else
System.out.println(intValues[i]+" is zero");
}
}
}
//end of program
Output:
The element in the String array which was not filled stores null.