Question

In: Computer Science

JAVA If a user inputs a specific number of rows, columns and sections for a rectangle...

JAVA


If a user inputs a specific number of rows, columns and sections for a rectangle using three symbols, how would you print using for loops and a string? Not using an array


Example:

If 4 was inputted for rows, 12 was inputted for columns, 6 was inputted for sections, and the following symbols (that are all chosen by the user) were “$” “*” and “%” the following would print:


$$**%%$$**%%

$$**%%$$**%%

$$**%%$$**%%

$$**%%$$**%%


Has to account for all numbers including odd. And the three symbols repeat.

Solutions

Expert Solution

Below is the code for the above code:

import java.util.Scanner;

public class Rectangle {

   /**main method to start the program*/
   public static void main(String[] args) {
      
      
       int rows;
       int columns;
       int section;
       char symbol1;
       char symbol2;
       char symbol3;
      
       /**taking input from the users*/
       Scanner input = new Scanner(System.in);
       System.out.println("Enter the number of rows");
       rows = input.nextInt();
       System.out.println("Enter the number of columns");
       columns = input.nextInt();
       System.out.println("Enter the sections");
       section = input.nextInt();
       System.out.println("Enter the symbol 1");
       symbol1 = input.next().charAt(0);
       System.out.println("Enter the symbol 2");
       symbol2 = input.next().charAt(0);
       System.out.println("Enter the symbol 3");
       symbol3 = input.next().charAt(0);
      
       int innerlooptimes = (int) Math.ceil(section/3);

       /**outer loop will run for row times*/
       for(int i = 0;i<rows; i++ )
       {
           /**2nd inner class will run for column/section times*/
           for(int j=0; j<(int)Math.ceil(columns/section); j++)
           {
               /**there are 3 symbols, so 3rd inner loop will run for 3 times*/
               for(int k=0;k<3;k++)
               {
                  
                   if(k%3==0)
                   {
                       /**most inner loop will run for section/3 times*/
                       for(int p =0; p<innerlooptimes;p++)
                       System.out.print(symbol1);
                   }
                   else if(k%3==1)
                   {
                       for(int p =0; p<innerlooptimes;p++)
                       System.out.print(symbol2);
                   }
                   else
                   {
                       for(int p =0; p<innerlooptimes;p++)
                       System.out.print(symbol3);
                   }
               }
           }
           System.out.println();
       }
      
       /**if you calculate number of time inner loops will run , (Column/section)*(3)*(section/3) = columns
       * */
   }

}

output:


Related Solutions

Write the following program in Java. Ask the user to enter the number of rows and...
Write the following program in Java. Ask the user to enter the number of rows and columns for a 2-D array. Create the array and fill it with random integers using (int)(Math.random() * 20) (or whatever number you choose). Then, provide the user with several menu choices. 1. See the entire array. 2. See a specific row. 3. See a specific column. 4. See a specific value. 5. Calculate the average of each row. 6. Calculate the total of each...
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given).
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code: X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line. (a) (3%) Write a single line of code which saves the first two rows of array A...
in java Print a list of seats in a theater. Rows are numbered, columns lettered, as...
in java Print a list of seats in a theater. Rows are numbered, columns lettered, as in 1A or 3E. Print a space after each seat. Create a nested loop to print the following: 1A 1B 1C 1D 3A 3B 3C 3D 5A 5B 5C 5D
Write a script to display the following patterns on the screen. Number of rows and columns...
Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop. **** **** **** a) Display the source code in an editor (#4-11) b) Execute your script in the terminal, and display the command and the result (#4-12)
Write code that will produce the following pyramid for a user designated number of rows (user...
Write code that will produce the following pyramid for a user designated number of rows (user input). 1 11 121 12321 1235321 (Based on Fibonacci numbers) *USE JAVA*
A) Suppose owls is a MATLAB array with 251 rows and 51 columns representing the number...
A) Suppose owls is a MATLAB array with 251 rows and 51 columns representing the number of owls counted in the 251 counties in Texas had over the years 1960-2010. Write code to define a MATLAB variable to find the median number of owls counted in each county. B) Suppose cattle is a MATLAB array with 251 rows and 51 columns representing the number of cattle in the 251 counties in Texas had over the years 1950-2000. Write code to...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
1 How can we specify the number of rows and columns in a grid layout? 2....
1 How can we specify the number of rows and columns in a grid layout? 2. What happens when we don't have enough items to be displayed in a grid layout with a specified size? 3. How can we specify a grid layout component to occupy more than one columns or rows? 4. What happens if the components in a linear and grid layout exceed the layout size? How can we handle this problem so that all components can be...
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and its vertical...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT