Write a program to evaluate the following piecewise function y=f(x).
y=-x+2.5; 0 <= x < 5
y=2-1.5(x-3)(x-3); 5 <= x < 10
y=x/2-1.5; 10 <= x < 20
input
a floating point number N, 0 <= N < 20
output
output N corresponding piecewise function value: f(N). The result is rounded to three decimal places.
Example input
1.0
Sample output
1.500
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
float f;
double sum = 0;
Scanner scan=new Scanner(System.in);
f=scan.nextFloat();
if(f>=0 && f<5)
sum=-f+2.5;
else if(f>=5 && f<10)
sum=2-1.5*(f-3)*(f-3);
else if(f>=10 && f<20)
sum=f/2-1.5;
System.out.println(String.format("%.3f",sum));
}
}