
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
In this article, we will learn two different approaches to computing the focal length of a spherical mirror using Java. While the second method makes use of Object-Oriented Programming (OOP) concepts for a more generic and flexible approach the first method is a simple functional implementation.
Finding Focal Length
Spherical mirrors play a vital role in optical systems, from telescopes and microscopes to vehicle rearview mirrors. These mirrors, either concave or convex, focus or diverge light, respectively, based on their geometry. The focal length, a key characteristic of spherical mirrors, determines how the mirror bends light.
The focal length of a spherical mirror can be mathematically expressed as ?
? = ?2
where ? represents the focal length, and ? denotes the radius of curvature of the mirror.
Using Naive Approach
A basic Java program can be written to calculate the focal length of both concave and convex spherical mirrors. The program uses simple arithmetic based on the above formula, where the radius of curvature is passed as input to the functions.
Following are the steps for computing the focal length of a spherical mirror in Java ?
- The method concave_f_len calculates the focal length for a concave mirror by dividing the radius of curvature (val) by 2.
- The method convex_f_len calculates the focal length for a convex mirror by also dividing the radius of curvature by 2, but it returns a negative value, as the focal length for convex mirrors is always negative.
- The main method demonstrates the usage of both methods by passing a radius of curvature value (55 units) and printing the corresponding focal lengths.
Example
Below is an example for computing the focal length of a spherical mirror ?
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; // Example radius of curvature // Calculate focal length for concave mirror System.out.print("The focal length of spherical mirror (concave) is : " + concave_f_len(val) + " units\n"); // Calculate focal length for convex mirror 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
Using Object-Oriented Concepts
While the first approach works well for a basic understanding, using Object-Oriented Programming (OOP) principles can enhance the program, making it more modular, flexible, and reusable. We can create a SphericalMirror class that handles both concave and convex mirrors through inheritance and polymorphism.Following are the steps for computing the focal length of a spherical mirror using Object-Oriented Programming (OOP) concepts ?
- We define an abstract class SphericalMirror with a protected radius field and an abstract method getFocalLength().
- Two subclasses, ConcaveMirror and ConvexMirror, extend the SphericalMirror class. Each subclass implements the getFocalLength() method to calculate the focal length specific to that mirror type.
- This approach uses polymorphism, where we define a reference of type SphericalMirror but assign it to objects of the subclasses ConcaveMirror or ConvexMirror.
Example
Below is an example for computing the focal length of a spherical mirror using object-oriented programming (OOP) principles ?
// Abstract class for Spherical Mirror abstract class SphericalMirror { protected float radius; // Constructor to initialize radius public SphericalMirror(float radius) { this.radius = radius; } // Abstract method for focal length public abstract float getFocalLength(); } // Class for Concave Mirror class ConcaveMirror extends SphericalMirror { public ConcaveMirror(float radius) { super(radius); } // Override method for concave mirror focal length @Override public float getFocalLength() { return radius / 2; } } // Class for Convex Mirror class ConvexMirror extends SphericalMirror { public ConvexMirror(float radius) { super(radius); } // Override method for convex mirror focal length @Override public float getFocalLength() { return -radius / 2; } } public class MirrorDemo { public static void main(String[] args) { float radius = 55; // Example radius of curvature // Create instances of concave and convex mirrors SphericalMirror concaveMirror = new ConcaveMirror(radius); SphericalMirror convexMirror = new ConvexMirror(radius); // Display the focal lengths System.out.println("The focal length of spherical mirror (concave) is : " + concaveMirror.getFocalLength() + " units"); System.out.println("The focal length of spherical mirror (convex) is : " + convexMirror.getFocalLength() + " units"); } }
Output
The focal length of spherical mirror (concave) is : 27.5 units
The focal length of spherical mirror (convex) is : -27.5 units
Conclusion
Both approaches are valid for calculating the focal length of spherical mirrors. The first, simpler approach works well for basic use cases, while the second approach, leveraging Object-Oriented Programming principles, is more scalable and maintainable. By using inheritance and polymorphism, the second approach provides a better structure, especially when dealing with more complex scenarios involving multiple mirror types or additional properties.