Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user for a positive integer no greater than 15. Th

ID: 3696080 • Letter: W

Question

Write a program that asks the user for a positive integer no greater than 15. The program should then store a square on a file using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program store the following pattern in a file named “pattern.dat”:

XXXXX

XXXXX

XXXXX

XXXXX

XXXXX The program should then read the stored pattern from the file you just created and display the pattern on the screen.

Explanation / Answer

public static void main(String[] args) { // Create a Scanner object for keyboard input. Scanner keyboard = new Scanner(System.in); // Get a number from the user. System.out.print("Enter a number between 1-15: "); int number = keyboard.nextInt(); //validate users input validateNumber(keyboard, number); //produce matrix outputMatrix("X", number); //close scanner keyboard.close(); } /** * Method should validate a input number and continue to ask if the number * is now within range * * @param keyboard * @param number */ static void validateNumber(Scanner keyboard, int number) { // Validate the input. while (number < 1 || number > 15) { // Get a number from the user. System.out.println("Sorry, that's an invalid number."); System.out.print("Enter an integer in the range of 1-15: "); number = keyboard.nextInt(); } } /** * Method should output a row/column of char specified * * @param charToOutput * @param number */ static void outputMatrix(String charToOutput, int number) { // display square made of char for (int row = 0; row
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote