Question

In Java: Get an integer n from an input dialog or the console window (with user...

In Java:

Get an integer n from an input dialog or the console window (with user input), print out the following pattern on the console window (you may refer to displaying number-pyramid example shown in class).

For example, when n is 3, there are 5 rows in total.

              *

***

           *****

             ***

               *

When n is 4, there are 7 rows in total.

              *

            ***

          *****

        *******

          *****

            ***

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class PrintDiamond {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter n: ");
       int n = sc.nextInt();
       printDiamondPattern(1, n);
   }

   public static void printDiamondPattern(int thisRow, int num) {
       // the termination condition
       if (thisRow == num * 2)
           return;

       // the number of spaces
       int numberOfSpaces = 0;
       if (num < thisRow) {
           // finds the number of spaces
           numberOfSpaces = thisRow - num;
       } else {
           numberOfSpaces = num - thisRow;
       }

       // finds number of stars
       int numberOfStars = num - numberOfSpaces;

       // generate the string before printing it
       StringBuffer outputBuffer = new StringBuffer(num);
       for (int i = 0; i < numberOfSpaces; i++) {
           outputBuffer.append(" ");
       }
       for (int i = 0; i < numberOfStars; i++) {
           outputBuffer.append("* ");
       }

       // printing the string
       System.out.println(outputBuffer.toString());

       // recursion
       printDiamondPattern(thisRow + 1, num);
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
In Java: Get an integer n from an input dialog or the console window (with user...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Request an integer n from the console. Write a function that accepts n as input and...

    Request an integer n from the console. Write a function that accepts n as input and outputs the multiplication table of n from 1 to n to the console. Example Output (input in bold italics) Enter an integer: 9 1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • Write a program that takes a positive integer n as input from the user and displays...

    Write a program that takes a positive integer n as input from the user and displays an n by n checkerboard. (Hint: your main method could do user input then call the constructor for your GUI with n as the argument.) For example, when n = 5, there are 25 alternating white and black squares inside a square boarder. The lower right corner of the overall pattern must be white. Make sure that adjacent squares are different colors regardless of...

  • Implement a Java program using simple console input & output and logical control structures such that...

    Implement a Java program using simple console input & output and logical control structures such that the program prompts the user to enter 5 integer scores between 0 to 100 as inputs and does the following tasks For each input score, the program determines whether the corresponding score maps to a pass or a fail. If the input score is greater than or equal to 60, then it should print “Pass”, otherwise, it should print “Fail”. After the 5 integer...

  • Write a java program that will print if n numbers that the user will input are...

    Write a java program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times asking for an integer...

  • JAVA LANGUAGE!!!! Make a program that displays n prime numbers. n is a user input.   ...

    JAVA LANGUAGE!!!! Make a program that displays n prime numbers. n is a user input.    For example, If n is 7, the program displays as follows. Enter a positive integer: 7 2, 3, 5, 7, 11, 13, 17

  • Write code to read in an even number from the user (using the console and Scanner...

    Write code to read in an even number from the user (using the console and Scanner class). Use a loop to continue to ask for user input until you get an even number. Use a Java-provided exception class to account for the user entering non-numeric data.

  • Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete...

    Homework 3: Input Validation 1   Objectives control structures console-based user input using Scanner class writing complete programs using two classes: client and supplier 2   User Interface Specification This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows: When the program starts, display a one-line introduction to the user Display a menu with 5 options 1. validate zip...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • Project 4: Month-end Sales Report with array and validation Input dialog box for initial user prompt...

    Project 4: Month-end Sales Report with array and validation Input dialog box for initial user prompt with good data and after invalid data Input OK Cancel Input dialog boxes with sample input for Property 1 Ingut X Input Enter the address for Property 1 Enter the value of Property 1: OK Cancel Input dialog boxes after invalid data entered for Property 1 Error Please enter anmumber greater than D Ester the walue of Peoperty t Console output END SALES REPORT...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT