Question

In: Computer Science

Multi-Dimensional Arrays Create main class named MultiArray.    Create a method which outputs (prints) all the values...

Multi-Dimensional Arrays

Create main class named MultiArray.    Create a method which outputs (prints) all the values in the array. Call this method from each of the other methods to illustrate the data in the array. Note: Be sure to populate, and update the values in the multi-array, and then print the contents of the multi-array (Don't just print the design patterns.) The goal is to practice navigating the multi-array.

Declare a multi-dimensional array of integers, which is 10 rows of 10 columns ( 10 by 10).

In your code, set the initial values to ones’ such as this:

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1 1 1

Create a method named “patternRow.” Using control loops, (if , then / then / else ) set the data values to 1’s and 0’s, alternating by rows, such as this:

0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1

0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1

Create a method named “patternCheckered.” This method should set the values to alternative 1’s and 0’s, such as this:

0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0

Create a method named “userChoice.” Prompt the user to enter three numbers. Set the array data to be the sequence of numbers they enter, such as this:

Please enter your choice: 1

Please enter your choice: 7

Please enter your choice: 5

1 7 5 1 7 5 1 7 5 1
7 5 1 7 5 1 7 5 1 7 …

Create a method named “sumArray” which sums all the numbers together. Output totals for each row, and a grand total.

Solutions

Expert Solution

Copyable code:

// import required packages
import java.util.Scanner;
// define a class MultiArray
public class MultiArray
{
   // declare the global array variable and initialize the values
   public static int[][] multary= {{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},
   {1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},
   {1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1},{1,1,1,1,1,1,1,1,1,1}};
   // main method to perfom the operations in array
   public static void main(String[] args)
   {
       System.out.println("Initial values of MultiArray:");
       // call the method and display the initial values
       showValue();
       // call the method and perform the patternRow operation
       patternRow();
       // call the method and perform the patternCheckered operation
       patternCheckered();
       // call the method and perform the userChoice operation
       userChoice();
       System.out.println("Sum of each row and Grand total:");
       // call the method and compute sum of each row and grand total
       sumArray();
   }  
  
   // method definition to display the array values
   public static void showValue()
   {
       int lp1,lp2;
       // loop to iterate the array
       for(lp1=0;lp1<10;lp1++)
       {
           for(lp2=0;lp2<10;lp2++)
           {
               // code to display the values
               System.out.print(multary[lp1][lp2]+" ");
           }
           System.out.println();
       }
   }
   // method to set the data values to 1’s and 0’s in alternative rows
   public static void patternRow()
   {
       int lp1,lp2;
       // loop to iterate the array
       for(lp1=0;lp1<10;lp1++)
       {
           for (lp2=0;lp2<10;lp2++)
           {
               // code to fil the array with 1's and 0's inalternative rows
               if(lp1%2 == 0)
               {
                   multary[lp1][lp2] = 0;
               }
           }
       }
       System.out.println("Values of MultiArray after patternRow:");
       showValue();
   }
  
   // method to set the data values alternatively 1’s and 0’s
   public static void patternCheckered()
   {
       int lp1,lp2,fill=0,fill1=0;
       //loop to iterate the array
       for(lp1=0;lp1<10;lp1++)
       {          
           // condition test to fill the array as patternCheckered
           if(multary[lp1][0]== 0)
           {
               fill=0;
               fill1=1;
           }
           // condition test to fill the array as patternCheckered
           else if(multary[lp1][0] == 1)
           {
               fill=1;
               fill1=0;
           }
           // code to fill the array as patternCheckered
           for (lp2=0;lp2<10;lp2++)
           {
               if(lp2%2 == 0)
               {
                   multary[lp1][lp2] = fill;
               }
               else
               {
                   multary[lp1][lp2] = fill1;
               }
           }
       }
       System.out.println("Values of MultiArray after patternCheckered:");
       showValue();
   }
  
   // method to get the values from user and fill it in array
   public static void userChoice()
   {
       Scanner sn = new Scanner(System.in);
       int tmp1, tmp2, tmp3,cnt=1;
       // code to get the user choice values
       System.out.println("Please enter your choice:");
       tmp1 = sn.nextInt();
       System.out.println("Please enter your choice:");
       tmp2 = sn.nextInt();
       System.out.println("Please enter your choice:");
       tmp3 = sn.nextInt();
       //loop to fill the user choice values in array
       for(int lp1=0;lp1<10;lp1++)
       {
           for(int lp2=0;lp2<10;lp2++)
           {
               // check the occurence count and fill the user choice value1
               if(cnt==1)
               {
                   multary[lp1][lp2] = tmp1;
                   cnt++;
               }
               // check the occurence count and fill the user choice value2
               else if(cnt==2)
               {
                   multary[lp1][lp2] = tmp2;
                   cnt++;
               }
               // check the occurence count and fill the user choice value3
               else if(cnt==3)
               {
                   multary[lp1][lp2] = tmp3;
                   cnt=1;
               }             
           }
       }
       System.out.println("Values of MultiArray after Userchoice:");
       showValue();
   }
  
   // method to compute sum of row and grand total
   public static void sumArray()
   {
       int grndtot = 0;
       int sumrow = 0;
       for(int lp1=0;lp1<10 ;lp1++)
       {
           for(int lp2 = 0;lp2<10 ;lp2++)
           {
               sumrow = sumrow + multary[lp1][lp2];
           }
           grndtot=grndtot+ sumrow;
           System.out.println("Sum of row " + lp1 +" is " + sumrow );
           sumrow = 0;
       }
       System.out.println("Grand total is "+grndtot);
   }
}

Note: please give me a positive rating thank you:)


Related Solutions

Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the...
2-Dimensional Array Operations. Use a Java class with a main method. In this class (after the main method), define and implement the following methods: public static void printArray. This method accepts a two-dimensional double array as its argument and prints all the values of the array, separated by a comma, each row in a separate line. public static double getAverage. This method accepts a two-dimensional double array as its argument and returns the average of all the values in the...
1. Create a PHP program containing an multi dimensional array of all the first and last...
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order. 2. Split the array from #1 into two arrays; one containing first names, the other containing last names.
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT