COMP 248 Assignment 2 solution

$24.99

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

Description

5/5 - (4 votes)
Question 1 – Nested selection statements
The Quebec government is trying to crack down on texting and driving. “The law is very clear: you must
not hold a cell phone in your hand while driving. Failure to abide by this rule is an offence subject to a
fine and demerit points.” (https://saaq.gouv.qc.ca/en/road-safety/behaviours/distractions/cell-phones-texting/what-the-law-says/).
Write a program that determines the penalty for a driver holding a cell phone while driving based on
Nancy’s Crazy criteria.
If the driver is driving and holding a cell phone
 On the highway the fine is $80. If it is the drivers 1st offence s/he gets 1 demerit point otherwise
2 demerit points.
 In a school zone the fine is $100 (the maximum). If the driver has been driving for less than 24
months s/he loose his/her license on the spot otherwise s/he get 4 demerit points (the
maximum).
 If the car is stopped, for example at a stop sign or a traffic light, and the cellphone is an iPhone,
the fine is $100 and s/he gets 2 demerit point otherwise the fine is $80 and s/he gets 1 demerit
points. (Crazy Nancy does not like iPhones.)
 All other cases the fine is $90 and the driver gets 3 demerit points.
After determining where the driver was stopped and what their fine if they have not yet lost their
license, ask the user how many demerit points the driver originally had. When a driver has 12 demerit
points they lose their license. Determine if the driver should lose their license given where they were
when they were holding their cell phone. Output how many demerit points the driver now has.
Assume you have a perfect user who enters valid input.
The following are recommended statements for solving this problems: switch, if/else and nested if/else.
Here are a few sample outputs to illustrate the expected behavior of your program.
Note: user input is highlighted in green.
——-****——-****——-****——-****—–****—–
Welcome to the Fine and Demerit Point Evaluator!
based on Crazy Nancy’s Criteria
——-****——-****——-****——-****—–****—–
Welcome Officer – I need some information before I tell you what the fine and demerit points are.
Here are the possible locations
1 – Driver was stopped on the highway
2 – In a school zone
3 – Car is stopped at a Stop sign or traffic light
4 – None of the above
Please enter the digit corresponding to your case: 1
Officer, is this the driver’s 1st offence (answer with y for yes and anything else for no)? y
Last question officer! How many demerit points did the driver have prior to being stopped? 5
–> Write a ticket for $80 and inform the driver that they now have 6 demerit points.
Good job officer! Crazy Nancy’s tells you to keep up the good work!!!!
——-****——-****——-****——-****—–****—–
Welcome to the Fine and Demerit Point Evaluator!
based on Crazy Nancy’s Criteria
——-****——-****——-****——-****—–****—–
Welcome Officer – I need some information before I tell you what the fine and demerit points are.
Here are the possible locations
1 – Driver was stopped on the highway
2 – In a school zone
3 – Car is stopped at a Stop sign or traffic light
4 – None of the above
Please enter the digit corresponding to your case: 2
Officer, how many months has the driver been driving? 20
–> Officer, write a ticket for $100, take away their driver’s license and make arrangements to
have the car towed right away.
Good job officer! Crazy Nancy’s tells you to keep up the good work!!!!
Question 2: Integer Divisions & While loop
You are asked to write a Java program which will serve as a tutorial for the addition of two whole
numbers with at most 3-digits for primary school children. Your program is to read in two integers and
show how the addition is done along with the carry. Assume that the numbers being added have at
most 3 digits and are positive (so no validation of user input is required). This program is to repeat until
the user says otherwise.
Hint: You will need to use integer division and modulo to solve this problem (% and /).
Below is a sample output screen to illustrate the expected behavior of your program. Your output does
not need to be formatted in exactly the same way. Just make sure the required information appears.
Note: user input is highlighted in green.
——-****——-****——-****——-****—–****—–
Welcome to the Fine and Demerit Point Evaluator!
based on Crazy Nancy’s Criteria
——-****——-****——-****——-****—–****—–
Welcome Officer – I need some information before I tell you what the fine and demerit points are.
Here are the possible locations
1 – Driver was stopped on the highway
2 – In a school zone
3 – Car is stopped at a Stop sign or traffic light
4 – None of the above
Please enter the digit corresponding to your case: 3
Officer, is the cellphone in question an iPhone (answer with y for yes and anything else for no)? n
Last question officer! How many demerit points did the driver have prior to being stopped? 9
–>Write a ticket for $80 and inform the driver that they now have 10 demerit points.
Good job officer! Crazy Nancy’s tells you to keep up the good work!!!!
——-****——-****——-****——-****—–****—–
Welcome to the Fine and Demerit Point Evaluator!
based on Crazy Nancy’s Criteria
——-****——-****——-****——-****—–****—–
Welcome Officer – I need some information before I tell you what the fine and demerit points are.
Here are the possible locations
1 – Driver was stopped on the highway
2 – In a school zone
3 – Car is stopped at a Stop sign or traffic light
4 – None of the above
Please enter the digit corresponding to your case: 4
Last question officer! How many demerit points did the driver have prior to being stopped? 10
–>Write a ticket for $90. Also the driver has 13 demerit points.
Please take away their driver’s license and make arrangements to have the car towed right away.
Good job officer! Crazy Nancy’s tells you to keep up the good work!!!!
Welcome to Nancy’s Addition Tutorial Program!
———————————————
Enter two numbers with at most 3-digits each, separated by a space and press enter: 123 456
You requested the following operation:
num1: 123
num2: + 456
——
1st addition:
last digit of each number
3 + 6 = 9 so answer is 9 with a carry of 0
2st addition:
the carry from previous addition plus the middle digit of each number
0 + 2 + 5 = 7 so answer is 7 with a carry of 0
3rd addition:
the carry from previous addition plus the first digit of each number
0 + 1 + 4 = 5 so answer is 5
Final answer:
num1: 123
num2: + 456
——
Answer: 579
Do you want to try another one? (y or Y to repeat) y
Enter two numbers with at most 3-digits each, separated by a space and press enter: 888 201
You requested the following operation:
num1: 888
num2: + 201
——
1st addition:
last digit of each number
8 + 1 = 9 so answer is 9 with a carry of 0
2st addition:
the carry from previous addition plus the middle digit of each number
0 + 8 + 0 = 8 so answer is 8 with a carry of 0
3rd addition:
the carry from previous addition plus the first digit of each number
0 + 8 + 2 = 10 so answer is 10
Final answer:
num1: 888
num2: + 201
——
Answer: 1089
Do you want to try another one? (y or Y to repeat) y
Enter two numbers with at most 3-digits each, separated by a space and press enter: 12 999
You requested the following operation:
num1: 12
num2: + 999
——
1st addition:
last digit of each number
2 + 9 = 11 so answer is 1 with a carry of 1
Here is a recommended skeleton of the algorithm to solve this problem
1. Display welcome message/welcome banner
2. Prompt user for 2 numbers to add
3. 1
st addition: Add the last digit of each number, extract the result of the 2-digit addition and the
carry and show this information to the user in a clear message.
4. 2
nd addition: Add the carry from the 1st addition to the middle digit of each number, extract the
result of the addition of the 2 digits and the carry and show this information to the user in a
clear message.
5. Add the carry from the 2nd addition to the 1st digit of each number and display the result in a
clear message.
6. Finish off by showing the result of the addition
7. Ask the user if they want to try another addition.
o If yes repeat from step 2
o If no display a closing message
Make sure that in your output, the numbers in the addition are right justified. The use of printf will
help you do this.
Recommendation: Take the time to write a complete algorithm before starting to program this
question.
Question 3 – Nested While loops
A Parkside Triangle is generated from two numbers – the size and the seed. The size determines the
number of rows and the seed determines the first number in the triangle.
For example, if the user enters size 4 and seed 4 the triangle is built as follows:
Row 1 – will have one number and will be the seed 4
Row 2 – will have 2 numbers and they will be 5 and 6 5 6
Row 3 – will have 3 numbers and they will be 7 8 9 7 8 9
Row 4 (last row since size is 4) – will have 4 numbers 1 2 3 and 4 1 2 3 4
Note that the digits used in the triangle ate 1 to 9. There is no zero.
2st addition:
the carry from previous addition plus the middle digit of each number
1 + 1 + 9 = 11 so answer is 1 with a carry of 1
3rd addition:
the carry from previous addition plus the first digit of each number
1 + 0 + 9 = 10 so answer is 10
Final answer:
num1: 12
num2: + 999
——
Answer: 1011
Do you want to try another one? (y or Y to repeat) n
Hope you are more comfortable with additions now! If not, don’t hesitate to come back 🙂
Here are two other Parkside Triangle examples.
Triangle given that size = 6 and seed = 1 Triangle given that size = 7 and seed = 9
Write a program that
1. Displays a welcome message of your choice.
2. Prompts the user for the size and seed. Make sure that 5  size  10 and 1  seed  9.
3. Next, print the triangle starting with the seed on the 1st line
4. Display a closing message.
Below are 2 sample output screens to illustrate the expected behavior of your program. Note: user input
is highlighted in green.
1
2 3
4 5 6
7 8 9 1
2 3 4 5 6
7 8 9 1 2 3
9
1 2
3 4 5
6 7 8 9
1 2 3 4 5
6 7 8 9 1 2
3 4 5 6 7 8 9
—————————————————–
Welcome to Nancy’s Parkside’s Triangle Producer
—————————————————–
Size (must be between 5 and 10 inclusive): 6
Seed (must be between 1 and 9 inclusive): 3
3
4 5
6 7 8
9 1 2 3
4 5 6 7 8
9 1 2 3 4 5
All done!
—————————————————–
Welcome to Nancy’s Parkside’s Triangle Producer
—————————————————–
Size (must be between 5 and 10 inclusive): 2
Size (must be between 5 and 10 inclusive): 5
Seed (must be between 1 and 9 inclusive): 11
Seed (must be between 1 and 9 inclusive): 8
8
9 1
2 3 4
5 6 7 8
9 1 2 3 4
All done!
Submitting Assignment 2
Please check your Moodle course webpage for instructions on how to submit the assignment.
Evaluation Criteria for Assignment 2 (20 points)
Source Code
Comments for all 3 questions (3 pts.)
Description of the program (authors, date, purpose) 1 pts.
Description of variables and constants 1 pt.
Description of the algorithm 1 pts.
Programming Style for all 3 questions (3 pts.)
Use of significant names for identifiers 1 pt.
Indentation and readability 1 pt.
Welcome Banner/Closing message 1 pt.
Question 1 (4 pts.)
Prompting user/reading data 1 pt.
Determine fine 1 pt.
Determine demerit points / lost license 1 pt.
Display correct results 1 pt.
Question 2 (6 pts.)
Read in numbers 0.5 pts.
1
st addition 1.5 pts.
2
nd addition 1.5 pts.
3
rd addition 1 pt.
Format of output 0.5 pts.
Repetition 1 pt.
Question 3 (4 pts.)
Prompting user/reading data/validating input 1 pt.
Printing of triangle 3 pts.
TOTAL 20 pts.