DPIT111 Assignment 2 - Week 8

Question 1 (4 mark) : Implement a program which decides the unit ticket price according to the age of each customer and then calculates the total ticket price of all customers.

REQUIREMENTS

• The user input is always correct (input verification is not required).

• Your code must use while loop or do-while loop.

• Your code includes main method and a method called calcUnitPrice whose argument and return value are both in int data type

• The main method asks the user to enter the number of customers for booking tickets, reads the entered number, performs the loop to ask the age of each customer, decide the unit price for each customer by calling the method calcUnitPrice, displays the unit price for each customer, the total price and the end statement

• The method calcUnitPrice is to decide the corresponding unit price according to the input age, the flowchart is depicted as follows.

• Your code must work exactly like the following example (the text in bold indicates the user input).

 

Example of the program output: Example 1:

Please input the number of customers: 4 Please input the age of customer 1: 1 The unit price of customer 1 is: 0 Please input the age of customer 2: 5 The unit price of customer 2 is: 5 Please input the age of customer 3: 2 The unit price of customer 3 is: 0 Please input the age of customer 4: 60 The unit price of customer 4 is: 10

The total price is: 15

Thank you for your purchase and support!

 

Example 2:

Please input the age of customer 1: 20 The unit price of customer 1 is: 10 Please input the age of customer 2: 15 The unit price of customer 2 is: 8

The total price is: 18

Thank you for your purchase and support!

 

 

 

Question 2 (1 mark) : Rewrite Question 1 with for loop.

 

 

Question 3 (4 mark) : Implement a program which help the user to do some addition exercises.

 

REQUIREMENTS

• The user input is always correct (input verification is not required).

• Your code must use while loop.

• The main method asks the user to continue exercise or not, and reads the entered string, when continuing exercise, each time two random numbers (0-99) are generated as the operands of addition, the program will check whether the answer is correct or not and displays corresponding information; when finishing exercise, the number of questions asked, the number of correct answers, and the accuracy will be displayed.

• The accuracy shall be displayed with one decimal point precision.

• Your code must work exactly like the following example (the text in bold indicates the user input).

 

Example of the program output:

Example 1:

Will you continue addition exercises (Yes/No): Yes

79 + 44 = 123

Your answer is correct!

Will you continue addition exercises (Yes/No): No

You have answered 1 questions (1 correct), your accuracy is: 100.0%

 

Example 2:

Will you continue addition exercises (Yes/No): Yes

93 + 41 = 133

Your answer is wrong! The answer should be 134. Will you continue addition exercises (Yes/No): Yes 84 + 10 = 94

Your answer is correct!

Will you continue addition exercises (Yes/No): Yes

63 + 93 = 156

Your answer is correct!

Will you continue addition exercises (Yes/No): Yes

94 + 42 = 136

Your answer is correct!

Will you continue addition exercises (Yes/No): No

You have answered 4 questions (3 correct), your accuracy is: 75.0%

 

Example 3:

Will you continue addition exercises (Yes/No): Yes 22 + 84 = 12

Your answer is wrong! The answer should be 106. Will you continue addition exercises (Yes/No): Yes 51 + 65 = 12

Your answer is wrong! The answer should be 116. Will you continue addition exercises (Yes/No): Yes 38 + 72 = 110

Your answer is correct!

Will you continue addition exercises (Yes/No): No

You have answered 3 questions (1 correct), your accuracy is: 33.3%

 

 

Question 4 (1 mark) : Rewrite Question 3 with do-while loop.

 

 

 

Question 5 (2 mark) : Implement a program to display the following pattern using for loops.

REQUIREMENTS

• The user input is always correct (input verification is not required).

• The main function asks the user to input the number of lines and displays the pattern accordingly.

• Your code must use for loops.

• Your code must work exactly like the following example (the text in bold indicates the user input).

Example of the program output: Example 1:

Please enter the number of lines: 7

*-*-*-*-*-*-*

 *-*-*-*-*-*

  *-*-*-*-*

   *-*-*-*

    *-*-*

     *-*

      *

 

Example 2:

Please enter the number of lines: 5

*-*-*-*-*

 *-*-*-*

  *-*-*

   *-*

    *

Need a custom answer at your budget?

This assignment has been answered 6 times in private sessions.

© 2024 Codify Tutor. All rights reserved