Question

In: Computer Science

Write a user-defined function named printBox that, when called, will output the following sequence of characters:...

  1. Write a user-defined function named printBox that, when called, will output the following sequence of characters:

OOOOOOOO

OOOOOOOO

OO            OO

OO            OO

OOOOOOOO

OOOOOOOO

  1. Write a user-defined function named printArrow that, when called, will accept an integer called arrowSize and will output a sequence of *’s such that there are arrowSize number of rows with the number of * incrementing, and then arrowSize-1 rows with the number of * decrementing:
    1. If arrowSize = 3, then the output is:

*

**

***

**

*

  1. If the arrowSize = 5, then the output is:

*

**

***

****

*****

****

***

**

*

And so on…

(Hint: you will need two for-loops in a for-loop)

Solutions

Expert Solution


import java.util.*;
public class Solution {
// printBox method
   static void printBox() {
       for(int i=0; i<6; i++) {
           for(int j=0; j<8; j++) {
               if((i==2 && j>1 && j<6) || (i==3 && j>1 && j<6)) // Check the condition

System.out.print(" ");
               else

System.out.print(" O ");
           }
           System.out.println(); // For enter the new line.
       }
   }
// printArrow method taking arrowSize as a parameter
   static void printArrow(int arrowSize) {
       int i=0, j=0, k=0, l=1;
       for(i=0; i<arrowSize*2; i++) { // Outer loop
           if(i<=arrowSize) {
               for(j=0; j<i; j++) { // Inner loop
                   System.out.print("* ");
               }
               System.out.println();
           }else {
               for(k=0; k<arrowSize-l; k++) { // Inner loop
                   System.out.print("* ");
               }
               l++;
               System.out.println(); // For enter the new line.
           }
       }
      
   }

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       printBox(); // Call the printBox method.
       printArrow(scan.nextInt()); // Call the printArrow method.
       printArrow(scan.nextInt()); // Call the printArrow method.

   }

}

Output :



Related Solutions

Write a user defined function named “findTwosComplement” in C++, which will find 2’s complement of a...
Write a user defined function named “findTwosComplement” in C++, which will find 2’s complement of a signed number. Input must be passed by reference to the function and it will return the answer. There is no need to call this function anywhere. You can use following operators only: <<>> ~ & ^ | Decimal
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Write a user-defined MATLAB function, with two input and two output arguments that determines the height...
Write a user-defined MATLAB function, with two input and two output arguments that determines the height in centimeters (cm) and mass in kilograms (kg)of a person from his height in inches (in.) and weight in pounds (lb). (a) Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. (b) Determine your own height and weight in SI units.
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and...
Please solve the following problem for MATLAB Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the...
(EXCEL) Create a user-defined function called Hexagon that takes one argument called side and returns the area of a regular hexagon given the length of the side. Show that the function works in a worksheet by inserting the formula somewhere.
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
1. Write a function named “Number” that reads in a list of numbers until the user...
1. Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false. 2. Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration: struct Student { string name; int ID; } Student roster[30]; // Code to...
MATLAB Write a user defined function for a projectile motion. If a ball is launched from...
MATLAB Write a user defined function for a projectile motion. If a ball is launched from initial position(0,0) with a velocity v0 at angle θ, determine your horizontal and vertical position. Please plot x vs. t, y vs. t and y vs. x.
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT