Following is the Java code for focal length of a spherical mirror −
Example
import java.util.*;
import java.lang.*;
public class Demo{
public static float concave_f_len(float val){
return val/2 ;
}
public static float convex_f_len(float val){
return - (val/2 ) ;
}
public static void main(String argc[]){
float val = 55 ;
System.out.print("The focal length of spherical mirror (concave) is : " + concave_f_len(val) + "units\n");
System.out.println("The focal length of spherical mirror (convex) is : "+ convex_f_len(val) + "units");
}
}Output
The focal length of spherical mirror (concave) is : 27.5units The focal length of spherical mirror (convex) is : -27.5 units
A class named Demo defines a static function that returns a float value. It takes a float value as the parameter and divides it by 2 and returns the result. Similarly, another static function takes in a float value as parameter and divides by 2 and negates the value and returns the output. In the main function, a value for the float value that was passed to both the static functions is defined. Next, the function is called and relevant message is displayed on the console.