Starting from:

$25

COMP248- Project 3 Solved

Purpose: 
 
The purpose of this assignment is to help you learn Repetition statements (Loop), algorithms and pseudocode and flow control statements: for, while, do/while. 
CEAB/CIPS Attributes:            Design/Problem analysis/Communication Skills



 

General Guidelines When Writing Programs:  Please refer to the handout of Assignment #1.

Question 1 – Programming given a flowchart (3 pts)

Step 1: The flowchart of a program is given below in Figure 1.  Before you start programming trace through the flowchart for n=10 and write down how the variables are changing and what output is generated. What is this algorithm doing? Once you have answered that question for yourself proceed to the next step.

 

Step 2: Write a program based on the flowchart. Figure 2 shows you a sample output screen to illustrate the expected behaviour of your program. User input is in green.

 

Your program should proceed as follows:

1.     Ask the user to enter the number of sequences (n) that the program is to generate. 

2.     Print the output sequence (the □ will be replaced with the values the program generates based on the flowchart).

3.     Ask the user if s/he wants to continue (Yes/No). Continue running the program until the user enters No. 

4.     When the user chooses to terminate the program (enters No) print the number of iterations that have been done. 



 

Figure 1- Flowchart for Question 1

             

-----------------------------------------------------

   Welcome to The Program 

-----------------------------------------------------

Please enter the number of sequences

5

 

The result is:

□,□,□,□,□   
 

Do you want to repeat? (Yes/No)

Yes

 

Please enter the number of sequences

3

 

The result is:

□,□,□    
 

Do you want to repeat? (Yes/No)

No

 

Thank you for choosing our program, have a nice day!  
Figure 2- Sample output for question 1

 

 

Question 2 – Cashier Calculator (9 pts)

For this question you are required to write a Cashier Calculator, which will compute the total price of some items including taxes (Tax1 & Tax2).

The subtotal, taxes, and total price are calculated as follows:

SubTotal =   where n = number of items bought and P(i) = the price of item i.

            Tax1 = SubTotal × tax1Rate                            

                             (e.g., tax1Rate =0.06 stands for 6%)

Tax2 = (SubTotal + Tax1) × tax2Rate 

                 (e.g., tax2Rate=0.075 stands for 7.5%)

 

Total = SubTotal + Tax1 + Tax2

 

Your program will require the user to enter the following:

1)    The number of items

2)    The price of each item

3)    The Tax1 rate in percentage

4)    The Tax2 rate in percentage

 

The following constraints are given:

x The minimum number of items is 1 and the maximum number of items is 10.

x The minimum price of each item is $1 and the maximum price of each item is $1,000. The price may be a decimal number.

x The minimum tax rate for Tax1 and Tax2 is 0 and the maximum tax rate is 100.  Suppose the tax rate is 12.345%, the user will enter “12.345”. You will need to divide it by 100 to compute the tax rates. You can assume that the user will not enter the “%” symbol.

x All of the above minimum and maximum values are constants, i.e., they remain unchanged throughout the entire program. You should know how to define them.

 

The program must behave in the following manner:

x The program will require the user to enter the needed values one after another. If any of the entered values is invalid, then the program will detect that and go into a loop requiring the user to enter a valid value. The program must keep track of the total number of invalid values that the user has entered.

x Using SubTotal and tax1Rate, the program should compute Tax1. x Using Tax1 and tax2Rate, the program should compute Tax2.

x The program will calculate the SubTotal, Tax1, Tax2 and Total then display the following:

o    How many times the user has entered invalid values for all the required inputs.

If the user did not enter any invalid value, then no message should be displayed. In other words, “You have entered 0 invalid inputs” should NOT be displayed if the user has entered all values correctly.

o    The subtotal  o The Tax1 o The Tax2 o The total  

x The displayed subtotal, Tax1, Tax2, and total must have 2 decimal places. 

 

Here is a sample output to illustrate the expected behavior of your program. User input is in green.

-$-$-$-------------------------------------$-$-$-

      Welcome to Cashier Calculator Program

-$-$-$-------------------------------------$-$-$- 

Please enter the number of items bought [1...10]: 0 Please enter the number of items bought [1...10]: 2

 

Please enter the price of items 1 [1...1000]: 0

Please enter the price of items 1 [1...1000]: 100.50 

Please enter the price of items 2 [1...1000]: 500 

Please enter the tax rate of Tax1 [0...100]: 11111

Please enter the tax rate of Tax1 [0...100]: 5 

Please enter the tax rate of Tax2 [0...100]: 7.5

 

Here are your results

---------------------

You have entered 3 invalid inputs.

Subtotal: $ 600.50 

Tax 1: $ 30.03 

Tax 2: $ 47.29  TOTAL: $ 677.81 

 

Hope you shopping trip was an enjoyable one!


Figure 3- Sample output for question 2

 

 

Question 3 – Producing a flowchart given code (3 pts)

 

Given the code below:

a)     What is the output of the program? 

b)    Draw an appropriate flowchart for this program. 

 

import java.util.Scanner; class A3Q3 

{

      public static void main (String[] args)

      {     //Variable declaration  

            Scanner scanner = new Scanner(System.in);

            int i = 0 ,value = 0, n;

            String string = "";

             

             

            System.out.println("Enter the value of n:");

            n = scanner.nextInt();

             

            for (i = 1; i <= n; i++)      

            {                          

                  int counter=0;             

                  for(value = 1; value <= i; value++)

                  {

                        if(i%value==0)

                        {

                              counter = counter + 1;

                        }

                  }

                  if (counter == 2)

                  {

                        string = string + i + " ";

                  }      

            }      

             

            System.out.println("Output is :");

            System.out.println(string);

             

            scanner.close();

      }

}

 
 

How to submit the answers to this question?

Option 1: Draw your flowchart by hand and write up your answers to questions a) and b); and submit a photo of your answers which you zip together with your .java files for questions 1 and 2.

Option 2: Use one of the free s/w for flowchart drawing, or Word or even Power Point.  Don’t forget to include the answers to questions a) and b) of this questions.

             

More products