Java Question Set I – Conditional Statements in Java

5. Some credit card companies pay back a small portion of the charges a customer makes over a year. A particular credit card company’s pay back policy is as follows:

 

  • 0.25% for charges up to Rs. 500
  • 0.50% for the next Rs.1000 (that is, the portion between Rs. 500 and Rs. 1500)
  • 0.75% for the next Rs.1000 (that is, the portion between Rs. 1500 and Rs. 2500)
  • 1.0% for charges above Rs2500

Define the program, which accepts a charge amount and computes the corresponding pay back amount.

Examples to understand the question:

  1. A customer whose charge is Rs. 400 a year, then he will get Rs.1.00, which is 0.25 % of 400.
  2. When a customer’s charge is Rs.1, 400 a year, then he will receive Rs. 5.75, which is Rs.1.25 till Rs.500 and 0.50% of 900 = Rs.4.50

Logic of the Solution:

  1. Declare charges  and pb  as double
  2. Initialize both the variables with 0, so we don’t get scrap value while carrying out arithmetical operations on them
  3. Print a message for the user using System.out.println  and ask the user for total charges
  4. Store the amount entered by the user into charges  variable
  5. We will be using the nested if else to calculate the payback amount
  6. If the charges are lower than 500 then
    1. Take out 0.25 percent of the amount
    2. And print the Payback amount to the screen
  7. Otherwise, if the amount is more than 500 then
    1. Payback amount will be 0.25 till 500
    2. Now deduct 500 from the total charges
    3. If the deducted charges are smaller than 1000 then
      1. 0.50% will be calculated on the amount and add the new payback amount to the previous one.
    4. Otherwise, if it is still more than 1000 then
      1. Payback amount will be 0.50 till next 1000 and add the new payback amount to the previous one.
      2. Now deduct 1000 from the total charges again
      3. If the deducted charges are smaller than 1000 then
        1. 0.75% will be calculated on the amount and add the new payback amount to the previous one.
      4. Otherwise, if it is still more than 1000 then
        1. Payback amount will be 0.75 till next 1000 and add the new payback amount to the previous one.
        2. Now deduct 1000 from the total charges again
      5. If there are still some remaining charges then
        1. Payback amount will be 1% of all remaining charges and added to the previous payback amount calculated.
  8. Now, simply print the pb variable

Output:

charges-program

Alternative logics that can be applied:

This program can be simplified even further and that can reduce the number of code lines, we’ll soon be posting an alternate solution for this question. If you already got a good logic in mind, then help us by leaving a comment below.

Continue to next page for another question.

0 Responses on Java Question Set I - Conditional Statements in Java|

Leave a Message

Your email address will not be published. Required fields are marked *

CommentLuv badge

Pin It on Pinterest