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

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)
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...
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...
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
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
In Java please. Prompt the user for two string inputs. Perform the following actions on these...
In Java please. Prompt the user for two string inputs. Perform the following actions on these inputs, and print the result of each action: Part I: Concatenate the two strings into one. Compare the two strings and check whether they are the same (ignore the case). Look up the method equalsIgnoreCase() from Java API Convert all the characters in a string to uppercase Look up the method toUpperCase() from Java API Part II: Replace all the 'd' characters with ‘e'...
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle....
PUT IN JAVA PROGRAMMING The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT