Question

In: Computer Science

Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...

Write a program in Java to do the following:
-Create a one-dimensional array of 7 integers as follows:

Assign {35,20,-43,-10,6,7,13}
-Create a one dimensional array of 7 Boolean values as follows:

Assign {true,false,false,true,false,true,false}
-Create a one dimensional array of 7 floating-point values as follows:

Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f}
-Declare sum as integer and set it to 0.
-Declare sumf as float and set it to 0.0f.
-Use a for loop to go through each element of the Boolean array, and if an element is “true” then add the corresponding element in the integer array to sum, and also add the corresponding element in the floating-point array to sumf.

-Print all three arrays, sum, and sumf.

Solutions

Expert Solution

TestArray.java

public class TestArray {

   public static void main(String[] args) {
       int[] ilist = {35,20,-43,-10,6,7,13};
       Boolean[] array = {true,false,false,true,false,true,false};
       float[] farray = {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f};
       int sum=0;
       float sumf=0.0f;

     for (int i = 0; i < array.length; i++) {
         System.out.print(ilist[i] + " ");
      }
      System.out.println();
    
       for (int i = 0; i < array.length; i++) {
         System.out.print(array[i] + " ");
       }
      System.out.println();
        for (int i = 0; i < array.length; i++) {
         System.out.print(farray[i] + " ");
      }
      System.out.println();
      for (int i = 0; i < array.length; i++) {
          if(array[i]==true){
              sum=sum+ilist[i];
              sumf=sumf+farray[i];
          }
       
      }
      System.out.println("sum = "+sum + " ");
         System.out.println("sumf = "+sumf + " ");
           }
      }

Output:-

35 20 -43 -10 6 7 13 
true false false true false true false 
12.0 1.5 -3.5 -2.54 3.4 45.34 22.13 
sum = 32 
sumf = 54.8 

Related Solutions

C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional...
WRITE A JAVA PROGRAM: For this lab, you will create a 3 x 7 two dimensional array to store how many pounds of food three monkeys eats each day in a week. The 2D array is then passed to functions to find total, average and least amount of food consumption, etc. The program then need to display: the average amount of food the three monkeys ate per day the least amount of food eaten all week by any one monkey....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT