loading...

Discussion on: How Do You Approach Your Coding Problems

Collapse
harshrathod50 profile image
Harsh Rathod

A Java programmer's approach:

Understand the problem:

  1. Make a function to convert Celcius to Fahrenheit.
  2. The function argument may be a floating-point number.
  3. The function should return a precise answer.

Code the problem:

  1. Use proper function and variable names.
  2. My function takes an argument which is of type double because even if I pass it any other numerical primitive data type, it will be upcasted to double, by the compiler.
  3. Then comes the logic which solves the problem. Try to be concise and optimize the logic if possible. Why write more, if I can get the problem solved in lesser code.
  4. My function returns a double because it gives me a more precise answer.
public class Demo {
    public static double toFahrenhite(double val) {
        return val*1.8+32;
    }
    public static void main(String[] args) {
        System.out.println(toFahrenhite(26));
    }
}

Output:

78.80000000000001

Know the limitations of your solution:

  1. My code can only handle numbers.
  2. My code can't handle exceptions.