Search This Blog

Monday, July 20, 2020

Programs List Basics

Speed-Converter

Code to convert kilometers to Miles-PerHour:

Program

public class SpeedConverter {
public static void main(String[] args) {

}

public long toMilesPerHour(double kiloMeterPerHour) {

if (kiloMeterPerHour < 0)
return -1;
long milesPerHour = Math.round(kiloMeterPerHour / 1.609);// convert kilometers to miles
return milesPerHour;
}

}

Optimize code:


public class SpeedConverter {
public static void main(String[] args) {

}

public long toMilesPerHour(double kiloMeterPerHour) {

if (kiloMeterPerHour < 0)
return -1;
return Math.round(kiloMeterPerHour / 1.609);// convert kilometers to miles

}

}









No comments:

Post a Comment