Question
Rules for naming variables and constants: 1. Use words from the problems description as names of variables and constants within the program. Do not abbreviate
Rules for naming variables and constants: 1. Use words from the problems description as names of variables and constants within the program. Do not abbreviate any words. 2. Type names of all variables in camelCase. 3. Type names of all constant in ALL_UPPER_CASE with underscores separating individual words within the constants name. 4. If a variable or constant represents a percentage of any sort, then its name must say so, by including the word percentage or percent or rate. Here are some examples: a. Variables: taxPercentage, commissionRate, b. Constants: INTEREST_RATE, PERCENTAGE_DISCOUNT 5. If a variable or constant represents a fee/price/cost of any sort, then its name must say so, by including one of those words or some other word that means the same thing. Here are some examples: a. Variables: ticketPrice, feePerPerson, dailyCharge, totalCost b. Constants: BASE_FEE, SERVICE_CHARGE, REGULAR_PRICE 6. If a variable or constant represents a maximum or minimum of any sort, then its name must say so, by including one of those words or some other word that means the same thing. Here are some examples: a. Variables: highestPrice, lowestFee b. Constants: MINIMUM_QUANTITY, MAXIMUM_RIDERS 7. Based on the above, we can derive rules for every category of variable and constant in general, make every name as specific as possible without including too many words in the name. Words and patterns that are banned from names of variables and constants: 1. Digits and numbers spelled out are not allowed. Here are some examples: price1, firstCost, DISCOUNT_1, QUANTITY_ONE. 2. Any mention of tiers or levels or anything similar that simply suggests a list without showing how each item in the list relates to the others. Here are some examples: LEVEL_1_FEE, COMMISSION_TIER_1, groupOneCost. 3. In general, in any place where you think of using any of the above, or anything similar to them, instead come up with a set of names that not only shows that there is a list of connected items, but also how those relate to each other, e.g., LOWEST_FEE, MIDDLE_FEE, HIGHEST_FEE these not only show that there are three levels of fees, but also which one is less/greater than the other. Rules for calculating anything: 1. Do NOT expect the user to calculate anything at all. If they could do any calculations themselves, then they have no need for our program! For example, as we saw in examples from Unit 1, any percentages that a program uses would have to be stored as a decimal fraction, e.g., 0.2 instead of 20%. When your program asks a user to enter any percentage, then, do not expect them to figure out that 0.2 is the same as 20% and then type in 0.2 as input instead, we let them simply type 20, and then we write a line of code to divide what they entered by 100, to convert it into a decimal fraction that our program requires. 2. You, the programmer, DO NOT calculate anything manually and type it into the program either. Make the program calculate everything that is required. 3. Every line of calculation code must read logically and be self-explanatory, i.e., anyone must be able to read it and understand what is being done without any explanation from you and without having to spend time trying to figure it out. It makes sense to me is not enough it is your job as a person who is writing code that someone else will have to maintain and update, perhaps years after you have moved on to another job. When to declare and use constants: 1. If there is any quantity at all that is given to you outright in the problem description, then declare a constant for it. 2. Once you declare a constant to represent some quantity, then use that constant in every place in your code where you would have typed that quantity. Literal numbers that are typed into code are called magic numbers, e.g., the 0.05 in this example it does not explain that 0.05 is the percentage of the increase being applied, and worse, whether this is a weekly/monthly/annual increase. increase = current * 0.05 DO NOT use any magic numbers within your code at all. The correct way to write the above example would be a. Declare a constant for the increase rate: const floating MONTHLY_INCREASE_RATE = 0.05 b. Use that constant in the calculation: increase = current * MONTHLY_INCREASE_RATE 3. Some quantities explain themselves and do not have to be declared as named constants, e.g., if you simply need to set something to 0, then simply do that; if you are calculating half of something, then simply divide by 2; if you are converting the users entry of a percentage into a decimal fraction as described earlier, then simply divide by 100. When to declare and use variables: 1. If there is any quantity mentioned in the problem description as having to be entered by the user, or as having to be calculated and output by the program, then declare a variable for it. 2. If in order to calculate something, you have to calculate some other intermediate result first, then declare variables for those intermediate results, e.g., in order to calculate an average, we have to first calculate a sum/total of the numbers that we are trying to average, so, declare a variable for the sum and a different one for the average. User-friendly practices to apply in every program: 1. As shown in the examples from Unit 1 (and every Unit after that), at the beginning of every program, briefly explain what the program will do for the user, and what input they should expect to have to enter. 2. Prompt the user for each input, one at a time. 3. As mentioned earlier, do not make the user have to calculate anything every calculation that is required is our responsibility as programmers. 4. Repeat the users input back to them before you output the results the program has generated you saw this in the Unit 1 examples, and will in the Unit 2 examples. This allows them to double-check their entries and make sure they did not make any mistakes when typing in their input. 5. Explain each output clearly. 6. Include units for each output, i.e., sq ft for area of a floor, $ and % symbols in the proper position for those (i.e., $ symbols usually come before the number that is being output, e.g., $595.75, but % symbols belong after the number, e.g., 3.5%). If you do not know the units for something, then find out by searching online. 7. Include as much detail as possible, e.g., if the program is calculating a discounted price, then output the price before discount, how many dollars the discount was, and the price after the discount, all separately
Problem 1 Write a program that allows the user to enter the radius and height of a cylinder. The program then calculates and outputs the volume and surface area of the cylinder. Volume of a cylinder =2h Surface area =2rh+2r2, where r represents the radius and h the height. (Use 3.14159 for ) In this program, declare the following variablesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started