Use the IO module to read inputs. Use System.out.println() to print your answer.
ID: 641371 • Letter: U
Question
Use the IO module to read inputs. Use System.out.println() to print your answer.
Write a program that calculates the intersection between 2 equations:
a degree-2 (quadratic) polynomial i.e.
where d, f, and g are constants
and a degree-1 (linear) equation i.e.
y = mx + b
where m is the slope and b is a constant
The above is just text, not code that could appear in a Java program.
Ask the user for the constant values in each equation. Output the intersection(s) as ordered pair(s) (x,y), or "none" if none exists.
Explanation / Answer
Program that calculates the Intersection between 2 Equations:
public static void main(String[] args) {
// Declare variable
Double d, f, g, m, b, A, B, C, X1, Y1;
System.out.println("Intersection of y = dx^2 + fx + g and y = mx + b " );
System.out.println("Enter the constand d " );
d=IO.readDouble ();
System.out.println("Enter the constand f " );
f=IO.readDouble ();
System.out.println("Enter the constand g " );
g=IO.readDouble ();
System.out.println("Enter the constand m " );
m=IO.readDouble ();
System.out.println("Enter the constand b " );
b=IO.readDouble ();
A=d;
B=f-m;
C=g-b;
X1 = 0.0;
Y1 = 0.0;
if (A == 0) {
//if A=0, both equations are linear equations
if (B == 0) {
if (C == 0) {
// if all A, B, C =0, infinite solutions
System.out.println("The intersection(s) is/are: ");
System.out.println("infinite intersections" );
}
else {
//if A=B=0, but C!=0, there is no solutions
System.out.println("The intersection(s) is/are: ");
System.out.println("none ");
}
}
else {
X1 = -C/B;
Y1 = m*X1 + b;
System.out.println("The intersections is/are: ");
System.out.println("("+X1+","+Y1+")");
}
}
else {
if ((B*B - 4.0*A*C)<0) {
//if (B*B - 4.0*A*C)<0, there is no real solutions
System.out.println("Warning: There are two non-real (complex) intersection");
System.out.println("and will not considered here");
return;
}
X1 = (-B + Math.sqrt(B*B - 4.0*A*C))/(2.0*A);
Y1 = m*X1 + b;
System.out.println("The intersection(s) is/are: ");
System.out.println("("+X1+","+Y1+")");
X1 = (-B - Math.sqrt(B*B - 4.0*A*C))/(2.0*A);
Y1=m*X1+b;
System.out.println("("+X1+","+Y1+")");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.