Question: CS 1083 Project 1: Print Text Patterns The Patterns Class Objectives Other objectives include: Use static methods. Use local variables. Use arithmetic expressions. Use Scanner
CS 1083 Project 1: Print Text Patterns
The Patterns Class
Objectives
Other objectives include:
Use static methods.
Use local variables.
Use arithmetic expressions.
Use Scanner to input values.
Use a class constant.
Use for loops.
Hand-in Requirements
All projects and laboratories will be submitted electronically through Blackboard. Zip up your entire project directory to submit as the source. (Right click on the project folder and follow Send To > Compressed (zipped) Folder or 7-Zip > Add to "project1.zip".) The project folder should include the following:
Patterns.java
PatternsOutput.txt
Tasks
Write a program that prints
Project 1 written by YOURNAME
and calls two methods:
Print a triangle pattern described below. This method should ask the user for the width of the pattern, and then print the corresponding pattern.
Print a sequence of text boxes. The drawBox method on p. 32 prints one text box with a fixed size, but your method should print a user-specified number of text boxes from left to right. The user should also specify the number of rows and columns in the boxes. This method should ask the user to enter the number of boxes, number of rows, and number of columns and then print that many text boxes horizontally, each box with the specified numbers of rows and columns.
Here is an example of what your output should look like (user input is in bold and underlined):
Enter width of pattern: 9
/|\
//|\\
///|\\\
////|\\\\
Enter number of boxes: 3
Enter number of rows: 4
Enter number of columns: 7
+-----+ +-----+ +-----+
| | | | | |
| | | | | |
+-----+ +-----+ +-----+
Details
The Console
You will need to use Scanner to obtain input from the keyboard. You should declare a class constant of type Scanner named CONSOLE at the beginning of your class; you should store new Scanner(System.in) in CONSOLE. See examples in the Chapter 2 lecture notes and in the Laboratory 2 assignment.
Triangle Pattern
The first two lines of code in this method should prompt the user for the width of the pattern and should input the value from the user. Below, it is assumed that the width is stored in width.
The rest of the code should print the text pattern. Note that the number of lines in the pattern (the height) is width / 2. The first line of the pattern starts with height - 1 spaces, the second line starts with height - 2 spaces, the third line starts with height - 3 spaces, and so on. Each line in the pattern contains slashes /, a vertical bar | (if width is odd), and backslashes \. The first line in the pattern contains 1slash, width % 2 vertical bars, and 1 backslash. The second line contains 2 slashes, width % 2 vertical bars, and 2 backslashes. The third line contains 3 slashes, width % 2 vertical bars, and 3 backslashes. This suggests the following pseudocode:
Pseudocode for Printing the Triangle Pattern
Assign width / 2 to height.
For each value of line from 1 to height:
Print the appropriate number of spaces (based on the values of height and line).
Print the appropriate number of slashes (based on the value of line).
Print the appropriate number of vertical bars (based on the value of width).
Print the appropriate number of backslashes (based on the value of line, also requires an escape sequence).
Print " " (a newline).
Print steps A-D should be implemented by for loops. It's ok if you want to read ahead and use an if statement for step C.
Pattern of Text Boxes
The first six lines of code in this method should prompt the user for the number of boxes, columns, and rows, and input the values from the user. Below, it is assumed that these values are stored in numBoxes,numRows, and numColumns.
The rest of the code should print the boxes. In your program, you will need a double for loop for each line. Consider the pseudocode for printing the first line.
Pseudocode for Printing the First Line of the Boxes
For each value of box from 1 to numBoxes:
Print "+" (a plus sign).
For each value of column from 2 to numColumns - 1:
Print "-".
Print "+ "
Print " " (a newline).
Note that the newline is printed outside the for loops. The last line follows the same pseudocode. The pseudocode for each middle line is similar, but you must make sure you print numRows - 2 of these lines.
Pseudocode for Printing All the Lines of the Boxes
Print the first line of the boxes (see above pseudocode).
For each value of row from 2 to numRows - 1:For each value of box from 1 to numBoxes:
Print "|" (a vertical bar).
Print numColumns - 2 spaces.
Print "| ".
Print " " (a newline).
Print the last line of the boxes.
PatternsOutput.txt
When creating the file PatternsOutput.txt, use the values shown in the examples: the width of the triangle pattern should be 9, the number of boxes should be 3, the number of rows should be 4, and the number of columns should be 7.
Rubric
[6 Points] If your program has a method that correctly prints the triangle pattern.
[1 Points] For prompting the user for the width and inputting the value from the user.
[5 Points] For for-loops that correctly print the pattern (the correct number of lines and the correct number and location of spaces, slashes, vertical bars, and backslashes on each line).
[10 Points] If your program has a method that correctly prints a sequence of text boxes horizontally.
[3 Points] For prompting the user for the number of boxes, rows, and columns and inputting the values from the user.
[3 Points] For for-loops that correctly print the top (and bottom) lines of the boxes (the correct number and location of dashes, the correct number and location of pluses, spaces between the boxes).
[4 Points] For for-loops that correctly print the middle lines of the boxes (the correct number of lines, the correct number and location of vertical bars, the correct number and location of spaces inside and outside the boxes). .
[4 points]
If the main method of your program prints "Project 1 written by [...]" and calls the two other methods.
If your submission was a zip file named project1.zip containing a folder named project1, which contains the other files.
If your Java program was in a file named Patterns.java.
If the output of your Java program was in a file named PatternsOutput.txt.
If your program contains a comment that describes what the program does and contains a descriptive comment for each method.
If your program is indented properly.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
