1. Use the IO module to read inputs. Use System.out.println() to print your answ
ID: 641316 • Letter: 1
Question
1.
Use the IO module to read inputs. Use System.out.println() to print your answer.
Write a program that generates a canonical-form, degree-3 (cubic) polynomial given its roots. For example, if the roots are 5, -3, and 2, the polynomial equation is
The canonical form of the polynomial is therefore
The above is just text, not code that could appear in a Java program.
Ask the user for three roots (integers). Output the canonical form of the polynomial with those roots (as text) using System.out.println()
2.
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
import java.util.Scanner;
public class Polynomial {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int a, b, c;
System.out.print("Enter a: ");
a = in.nextInt();
System.out.print("Enter b: ");
b = in.nextInt();
System.out.print("Enter c: ");
c = in.nextInt();
a *= -1;
b *= -1;
c *= -1;
int x = a + b + c;
int y = (a * b) + (b * c) + (c * a);
int z = a * b * c;
String sx = "", sy = "", sz = "";
if(x >= 0){
sx += '+';
}
if(y >= 0){
sy += '+';
}
if(z >= 0){
sz += '+';
}
sx += x;
sy += y;
sz += z;
System.out.println("The canonical form of the polynomial is therefore");
System.out.print("x^3" + sx + "x^2" + sy + "x" + sz + "=0");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.