Question

In: Computer Science

explain line by line what it does Description: This function applies discount percentage on an item...

explain line by line what it does

Description: This function applies discount percentage on an item

price unless the item price is less than the whole sale

price. For example, a product at $10 with a wholesale price of $5 and a discount

of 10% returns $9.


function applyDiscount(productPrice : Double,

       wholesalePrice : Double,

       discount : Double)

Return Double

Create variable discountedPrice as double

discountedPrice = productPrice * (1–discount)

if (discountedPrice < wholesalePrice)

     discountedPrice = wholesalePrice

end if

return discountedPrice

End function

Solutions

Expert Solution

// function that applies discount on the productPrice by the given discount percent and returns the price after discount
function applyDiscount(productPrice : Double, wholesalePrice : Double, discount : Double)

Return Double // specifying the return type

Create variable discountedPrice as double // creates a variable discountedPrice of double type

// calculates discountedPrice where productPrice is the price of product
// and discount is the discount percent expressed as double (i.e if discount percent is 10 then discount = 0.1)
discountedPrice = productPrice * (1–discount)

// after calculating the discountedPrice check if discountedPrice < wholesalePrice , then set discountedPrice to wholesalePrice else does nothing
if (discountedPrice < wholesalePrice)
   discountedPrice = wholesalePrice
end if

return discountedPrice // returns the discountedPrice

End function // end of function applyDiscount

Eg:  Using the above given example:

productPrice = 10

wholesalePrice = 5

discount = 0.1 (10%)

discountedPrice = productPrice * (1–discount)

= 10(1-0.1)

= 10 * 0.9

= 9

Since discountedPrice > wholesalePrice (9 > 5) , so discountedPrice will be 9 which will be returned , if suppose discount was 0.6 i.e 60%

discountedPrice = productPrice * (1–discount)

= 10(1-0.6)

= 10 * 0.4

= 4

In this case discountedPrice < wholesalePrice (4 < 5) , so discountedPrice = wholesalePrice = 5 and this will be returned


Related Solutions

What is the CPI? What does the percentage change in the CPI measure? What does the...
What is the CPI? What does the percentage change in the CPI measure? What does the “core CPI” measure? Why might the consumer price index overstate changes in the cost of living? Why have economists defined and used price indexes other than the CPI? What does indexing mean? What is hyper-inflation, and what can cause it?
What info does the Linest array function give you with respect to the linear regression line?
What info does the Linest array function give you with respect to the linear regression line?
For the Chevron Company: What is the Description and Dollar Value of the first line on...
For the Chevron Company: What is the Description and Dollar Value of the first line on the Income Statement (it may be net sales)? Locate the Balance Sheet (it may have a different heading, such as the Statement of Financial Position). What are the names of the assets and their values (if more than five line items, report only the first five line items and dollar values)? Statement of Owner’s Equity: Post the line items and dollar values in the...
If the coefficient of determination is 0.497, what percentage of the data about the regression line...
If the coefficient of determination is 0.497, what percentage of the data about the regression line is explained? 24.7% 49.7% 50.3% 50.0%
Define and explain what Aggregate Production Function is. What does it include? Describe each of the...
Define and explain what Aggregate Production Function is. What does it include? Describe each of the factors and what impact it specifically has on productivity. How do each of the factors interact with each other? What countries would relate to the following examples: LDC (Less Developed Countries); NI (Newly Industrialized); and D (Developed)?
Define and explain what Aggregate Production Function is. What does it include?   Describe each of the...
Define and explain what Aggregate Production Function is. What does it include?   Describe each of the factors and what impact it specifically has on productivity.   How do each of the factors interact with each other? What countries would relate to the following examples: LDC (Less Developed Countries); NI (Newly Industrialized); and D (Developed)?
if the percentage recovery of acetanilide was 50% , what does that mean?
if the percentage recovery of acetanilide was 50% , what does that mean?
how does the effective interest method for a bond discount or premium differ from straight-line amortization?...
how does the effective interest method for a bond discount or premium differ from straight-line amortization? why does GAPP require this method?
What line item on income statement is most affected by their depreciation policy? Why? What “red...
What line item on income statement is most affected by their depreciation policy? Why? What “red flags” might one look for in order to assess whether a company is overly conservative or overly aggressive in taking depreciation expenses?
What is the function (what is it, what does it do, how does it do it)...
What is the function (what is it, what does it do, how does it do it) of a defrost high limit thermostat and a defrost termination thermostat?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT