COMP 248 Assignment 3 solution

$24.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (3 votes)
Question 1 – 1-D Integer Arrays
Darts thrown at a dart board land in different regions of the board, and a value or score is assigned
accordingly. The total score is the sum of the values scored for each toss. Figure shows two views of a
dart board which shows the regions on the board and the corresponding values or scores which we will
consider for this question.
Point Values Regions
Write a Java program that simulates the tossing of a darts until the score first equals or exceeds 1000 –
when the game ends. Assume that each region is as likely to be hit by a dart.
At the end of the game display the number of times each region was hit, the total score for each region
and the total score for the game.
You will need two 1-D arrays: one to keep track of the point values of each region and one to keep track
of the number of times each region was hit. You will also need to use a Random number generator to
simulate the tossing of the dart. We recommend using the nextInt() method of the Random class.
Here are a couple of sample runs to illustrate the expected behaviour of your program.
(Note there is no user input for this question)
2
2
4 3
1
5
7
6
10
9
4
8
4
5
5 5
7
3
3
3
1
4
1
4
1
4
|—————–>>>><<<<<--------------------| | Welcome to the Simplified Dart Game! | |----------------->>>><<<<<--------------------| Region Hits Points ------------------------- 1 25 175 2 26 130 3 33 165 4 32 160 5 39 117 6 25 75 7 27 81 8 30 30 9 36 36 10 31 31 It took 304 tosses for a total of 1000 That was an effortless game of darts! |----------------->>>><<<<<--------------------| | Welcome to the Simplified Dart Game! | |----------------->>>><<<<<--------------------| Region Hits Points ------------------------- 1 32 224 2 24 120 3 33 165 4 26 130 5 34 102 6 30 90 7 31 93 8 22 22 9 31 31 10 26 26 It took 289 tosses for a total of 1003 That was an effortless game of darts! Question 2 – 2-D character arrays, switch and nested loops You are asked to write a Java program which will produced patterned squares. The size of the square must be between 4 and 20 inclusive The choices are as follows: 1. A full square – you need the size of the square and the character to fill the square with. 2. A hollow square - you need the size of the square and the character for the border around the square. 3. An X - you need the size of the square, the character for the X and the character to fill the spaces around the X. 4. A square with horizontal bars - you need the size of the square, the character for the even rows and the character for the odd rows. 5. A square with vertical bars - you need the size of the square, the character for the even columns and the character for the odd columns. 6. A square with diagonal bars - you need the size of the square, the character for the even diagonals and the character for the odd diagonals. 7. An integer filled square – you need the size of the square and the starting integer value. Here are 2 examples: Figure 1. 5x5 square with starting number 1 8. A checkered square - you need the size of the square, and 2 characters for the checkers. The size must be a multiple of 4 for the pattern to work. See the sample output screen to see the expected behaviour of your program and samples of the square patterns. Here are a few restrictions/guidelines for the implementation of your solution: 1. Your program is to repeat until the user says s/he wants to quit (Choice 9 in the menu). 2. The pattern must be stored in an array of the requested size and displayed from the array. (You may want to set up a static method to print the array). 3. All user input must be validated: a. You can assume the user will enter an integer for the menu choice and an integer when prompted for the size of the square. b. Make sure the user enters a choice between 1 and 9 for the pattern they would like c. Make sure the size of the requested square is between 4 and 20 inclusive; for the checkered pattern make sure also that the size is a multiple of 4. d. For the integer filled array make sure the starting integer is between 0 and 50 inclusive. e. You are required to use a Switch statement in your implementation. f. It is recommended that you leave a space between each characters in the squares and have each integer in the integer square take up 3 spaces. Printf might be handy for this. These space recommendations result in esthetically better looking squares. Figure 2. 6x6 square with starting number 10 4. Notice that for choice 7 you will need an integer array, while for the other choices you will need a character array. Recommendation: Take the time to write a complete algorithm before starting to program this question. I also suggest you implement one pattern at a time. Following is a sample run to illustrate the expected behaviour of your program as well as the different squares. Note: user input is highlighted in green. |-------****-------****-------****-------****-----****-----| | Welcome to the Decorated Square Drawing Program! | |-------****-------****-------****-------****-----****-----| What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 11 Sorry! That is an illegal choice. Enter your choice (1 to 9): 1 How many rows and columns (min 4 & max 20)? 4 Which character do you want to fill your square with? @ Here is your pattern @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 2 How many rows and columns (min 4 & max 20)? 6 Which character do you want for the border? = Here is your pattern = = = = = = = = = = = = = = = = = = = = What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 3 How many rows and columns (min 4 & max 20)? 5 Which character do you want for the X? x Which character do you want around the X? . Here is your pattern x . . . x . x . x . . . x . . . x . x . x . . . x What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 4 How many rows and columns (min 4 & max 20)? 6 Which character do you want for the even rows? < Which character do you want for the odd rows? / Here is your pattern / / / / / / < < < < < < / / / / / / < < < < < < / / / / / / < < < < < < What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 5 How many rows and columns (min 4 & max 20)? 6 Which character do you want for the even columns? < Which character do you want for the odd columns? \ Here is your pattern \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < \ < What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 6 How many rows and columns (min 4 & max 20)? 6 Which character do you want for the even diagonals? \ Which character do you want for the odd diagonals? 7 Here is your pattern \ 7 \ 7 \ 7 7 \ 7 \ 7 \ \ 7 \ 7 \ 7 7 \ 7 \ 7 \ \ 7 \ 7 \ 7 7 \ 7 \ 7 \ What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 7 How many rows and columns (min 4 & max 20)? 5 What is the starting number for your integer filled square (between 0 and 50 inclusive): 1 Here is your pattern 1 25 24 22 19 2 6 23 21 18 3 7 10 20 17 4 8 11 13 16 5 9 12 14 15 What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 8 How many rows and columns (max 20 and multiple of 4)? 21 How many rows and columns (max 20 and multiple of 4)? 19 How many rows and columns (max 20 and multiple of 4)? 12 Which character do you want for the 1st checker? . Which character do you want for the 2nd checker? 2 Here is your pattern 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 . . 2 2 Submitting Assignment 3 Please check your Moodle course webpage for instructions on how to submit the assignment. Evaluation Criteria for Assignment 3 (20 points) Source Code Comments for all 3 questions (1.5 pts.) Description of the program (authors, date, purpose) 0.5 pts. Description of variables and constants 0.5 pts. Description of the algorithm 0.5 pts. Programming Style for all 3 questions (1.5 pts.) Use of significant names for identifiers 0.5 pts. Indentation and readability 0.5 pts. Welcome Banner/Closing message 0.5 pts. Question 1 (5 pts.) Setting up arrays 0.5 pts. Throwing dart and recording 2.5 pts. Keep going until total at least 1000 0.5 pts Display results 1.5 pt. Question 2 (12 pts.) Reading menu choices & validating 1 pt. Prompting for correct values for each square 1 pt. Creating patterns 1 to 6 & 8 1pt/each 7 pts. Creating pattern 7 2 pts. Format of output 1 pt. TOTAL 20 pts. What type of square would you like? 1 - Full square 2 - Hollow square 3 - An X 4 - Horizontal Bars 5 - Vertical Bars 6 - Diagonal Bars 7 - Integer Filled Square 8 - Checkered (must be a multiple of 4) 9 - Quit Enter your choice (1 to 9): 9 Hope you enjoyed your patterns!! Come back soon ...