- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for focal length of a spherical mirror
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.
Advertisements