Finding the vertex focus and directrix of a parabola: In the previous article, we have seen Java Program to Fins n’th Pentagonal Number
In this article we are going to see how to find the Vertex, Focus, Directrix of a Parabola Using Java programming language.
Java Program to Find the Vertex, Focus, Directrix of a Parabola
Before Jumping into the program directly let’s see how we can Find the Vertex, Focus, Directrix of a Parabola.
Explanation:
The standard form of a parabola equation is y = ax2+bx+c
Vertex = (-b/2a, 4ac-b2/4a)
Focus = (-b/2a, 4ac-b2 + 1/4a)
Directrix = y = c-(b2+1)4a
Example:
If a=5, b=3, c=2
Vertex = (-b/2a, 4ac-b2/4a) = (-0.3,1.55)
Focus = (-b/2a, 4ac-b2 + 1/4a) = (-0.3,1.6)
Directrix = y = c-(b2+1)4a = -198
Let’s see different ways to find the Vertex, Focus, Directrix of a Parabola.
Method-1: Java Program to Find the Vertex, Focus, Directrix of a Parabola By Using Static Value
Approach:
- Declare an integer variable say ‘
a
‘, ‘b
‘, ‘c
‘ and assign the value to it, which holds the value for the constants of the parabolic equation which is in the form of y=ax2+bx+c - Find the vertex, focus and directrix using the formula.
Vertex = (-b/2a, 4ac-b2/4a)
Focus = (-b/2a, 4ac-b2 + 1/4a)
Directrix = y = c-(b2+1)4a
- Print the result.
Program:
class Main { public static void main(String[] args) { //value of a, b, c declared double a = 4; double b = 2; double c = 1; //finding vertex System.out.println("Vertex: (" + (-b / (2 * a)) + ", " + (((4 * a * c) - (b * b)) / (4 * a)) + ")"); //finding focus System.out.println("Focus: (" + (-b / (2 * a)) + ", " + (((4 * a * c) - (b * b) + 1) / (4 * a)) + ")"); //finding directrix System.out.println("Directrix:" + " y= " + (int)(c - ((b * b) + 1) * 4 * a)); } }
Output: Vertex: (-0.25, 0.75) Focus: (-0.25, 0.8125) Directrix: y= -79
Method-2: Java Program to Find the Vertex, Focus, Directrix of a Parabola By Using User Input Value
Approach:
- Declare an integer variable say ‘
a
‘, ‘b
‘, ‘c
‘ and take the values as user input, which holds the value for the constants of the parabolic equation which is in the form of y=ax2+bx+c - Find the vertex, focus and directrix using the formula.
Vertex = (-b/2a, 4ac-b2/4a)
Focus = (-b/2a, 4ac-b2 + 1/4a)
Directrix = y = c-(b2+1)4a
- Print the result.
Program:
import java.util.*; class Main { public static void main(String[] args) { //Scanner class object created Scanner s = new Scanner(System.in); System.out.println("Enter the value of constant “a” in the parabolic standard equation form: "); double a = s.nextDouble(); System.out.println("Enter the value of constant “b” in the parabolic standard equation form: "); double b = s.nextDouble(); System.out.println("Enter the value of constant “c” in the parabolic standard equation form: "); double c = s.nextDouble(); //finding vertex System.out.println("Vertex: (" + (-b / (2 * a)) + ", " + (((4 * a * c) - (b * b)) / (4 * a)) + ")"); //finding focus System.out.println("Focus: (" + (-b / (2 * a)) + ", " + (((4 * a * c) - (b * b) + 1) / (4 * a)) + ")"); //finding directrix System.out.println("Directrix:" + " y= " + (int)(c - ((b * b) + 1) * 4 * a)); } }
Output: Enter the value of constant “a” in the parabolic standard equation form: 4 Enter the value of constant “b” in the parabolic standard equation form: 3 Enter the value of constant “c” in the parabolic standard equation form: 2 Vertex: (-0.375, 1.4375) Focus: (-0.375, 1.5) Directrix: y= -158
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Related Java Articles: