Answer to Question #334462 in HTML/JavaScript Web Application for Penwell

Question #334462

5. A travelling salesman records his mileage at the end of each week in order to make an accurate claim for expenses.


Write a Java code:

* With a class called Travelling which has a calculateExpenses method which is to prompt the user to input the date and the number of kilometres travelled.

* Perform a validation to ensure that the number of kilometres is an integer greater than 0.


* The method should be written within the same class:

- The first 100 kilometres is paid at R1.20 per kilometre.

- If the of kilometres is more than 100, all additional kilometres are paid at R1.50 per kilometre


* Output:

- The date : 04/28/2022

- The number of kilometres travelled: 200

- The total expenses due : R270.0


1
Expert's answer
2022-04-27T18:25:06-0400
class Travelling {
    calculateExpenses (date, number) {
        if(number <= 0) {
            console.log('Incorrect number of km.')
            return;
        }


        let total = 0;
        for(let i = 1; i <= number; i++) {
            total += i < 100 ? 1.20 : 1.50;
        }


        console.log('The date: ' + date);
        console.log('The number of kilometres travelled: ' + number);
        console.log('The total expenses due : R' + total);
    }
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS