Expression building for Unit conversion – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #JAVA#3773 siteicon   siteicon   siteicon  

Expression building for Unit conversion

Building of expressions can be done for different units using different divisors.

Learning Objectives

  • Making simple formulas.
  • Reuse of left hand side variable for recalculation with new units in a formula.

Source Code

TC++ #3773

Source Code

Run Output

Speed (in m/s) = 3.0
Speed (in km/hr) = 10.8

Code Understanding

double d,t,s; //Three double variables declared
d assigned 1200 which is distance in meters and t assigned 400 which is time in seconds

This formula will thus  give speed in m/s in variable s based on values filled in d and t variable.

System.out.println(“Speed (in m/s) = “+s);
Speed in m/s will be printed

Since the s above has already been printed for value in m/s we can again build a new formula for km/hr speed and fill its value in s. For km/hr formula we simply divide meter value in d with 1000 as 1 km=1000 m and t with 3600 as 1 hour = 3600 seconds. For this division we do it in separate brackets so that precedence of operator rules are properly followed.
System.out.println(“Speed (in km/hr) = “+s);

System.out.println(“Speed (in km/hr) = “+s);
With this we print the speed in km/hr as the s will now contain value based on recalculation based on new formula.


  • Reuse of variables should normally be done only when it is being used for similar purposes.
  • Reuse of variables may be avoided if the use of variables is required for displaying all values together.

Suggested Filename(s):


sunmitra| Created: 5-Mar-2018 | Updated: 5-Mar-2018|