1
\$\begingroup\$

I am doing some online exercises, and I came across this: "Your job is to write a program for a speed camera. For simplicity, ignore the details such as camera, sensors, etc and focus purely on the logic. Write a program that asks the user to enter the speed limit. Once set, the program asks for the speed of a car. If the user enters a value less than the speed limit, program should display Ok on the console. If the value is above the speed limit, the program should calculate the number of demerit points. For every 5km/hr above the speed limit, 1 demerit points should be incurred and displayed on the console. If the number of demerit points is above 12, the program should display License Suspended."

        Console.Write("Speed Limit :");
        int speedLimit = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("SPEED LIMIT : " + speedLimit);
        Console.WriteLine("***********************");

        Console.Write("Car Speed : ");
        int carSpeed = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("CAR SPEED : " + carSpeed);
        Console.WriteLine("***********************");
        Console.WriteLine("***********************");

        int demeritPoints = 0;

        if (carSpeed < speedLimit)
        {
            Console.WriteLine("Ok. You are good to go");
        }
        else
        {
            int speedDifference = carSpeed - speedLimit;

            for (int counter = 5; counter <= speedDifference; counter += 1)
            {
                if (counter % 5 == 0)
                {
                    demeritPoints++;
                }
            }
        }
        Console.Write("Demerit Points :");
        Console.WriteLine(demeritPoints);

        if (demeritPoints >= 12)
        {
            Console.WriteLine("Your license is suspended");
        }
        else
        {
            Console.WriteLine("You are not suspened");
        }

The program will look like this.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "You are not suspened" People don't get suspended. Licenses do. Might want to fix that, including the spelling. \$\endgroup\$
    – Mast
    Oct 25, 2018 at 15:07

1 Answer 1

2
\$\begingroup\$

Not much to say I think, but here goes:

  • You're using clear, descriptive variable names, which is good. Such names make code easier to understand, which is especially important when you're working on large, complex projects.
  • Calculating the number of demerit points can be simplified to int demeritPoints = speedDifference / 5;.
  • Your code always displays the demerit points and suspension message, but from the requirements it looks like it should only do so when the speed exceeds the limit.
  • Instead of writing "SPEED LIMIT : " + speedLimit, you can use an interpolated string: $"SPEED LIMIT: {speedLimit}", which makes formatting a little easier.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.