1) A one variable quadraticexpression is an arithmetic expression of then formax
ID: 3610980 • Letter: 1
Question
1) A one variable quadraticexpression is an arithmetic expression of then formax2+bx+c, where a, b and c are some fixed numbers calledcoefficientsand x is a variable that can take on different values. Specify,design and implement a class that can store information about aquadratic expression. The constructor should set all 3 coefficientsto zero, and another method should allow you to change thesecoefficients. There should be accessor methods to retrieve thecurrent values of the coefficients. There should also be a methodto allow you to evaluate the quadratic expression at a particularvalue of x. Write a main program to take input and show theoutput.
Alsowrite the methods to perform these indicated operations:
Quadratic result;
result = Quadratic.Sum (quadObj1 + quadObj2)
Youneed to write Sum method. Inthis case, the coefficients are added.
Quadratic result;
result = Quadratic.Scale(quadObj1 * multiplier)
Youneed to write Scale method. In this case, the coefficients of quadObj1 are multiplied bymultiplier.This should be a double.
Explanation / Answer
import java.io.*; public class Quadratic{ public double a; public double b; public double c; public Quadratic() { this.a = 0; this.b = 0; this.c = 0; } public Quadratic(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } public double getA() { return a; } public double getB() { return b; } public double getC() { return c; } public double calculate(double x) { double sum =((a * x * x) + (b * x) +c); return sum; } public Quadratic sum( Quadratic q1, Quadratic q2) { double a= (q1.getA() + q2.getA()); double b= (q1.getB() + q2.getB()); double c= (q1.getC() + q2.getC()); Quadratic q3= new Quadratic(a, b, c); return q3; } public Quadratic scale( Quadratic q1, double multiplier) { double a= (q1.getA() * multiplier); double b= (q1.getB() * multiplier); double c= (q1.getC() * multiplier); Quadratic q3= new Quadratic(a, b, c); return q3; } public static void main(String[] args){ Quadratic q1= new Quadratic(1, 1, 1); Quadratic q2 = new Quadratic(2, 2, 2); double number = 0; System.out.print("Enter the number to evaluate the quadraticexpression: "); try{ number = Double.parseDouble (newBufferedReader(new InputStreamReader(System.in)).readLine()); } catch (Exception e){ e.printStackTrace(); } System.out.println(); System.out.println(q1.calculate(number)); System.out.println(); Quadratic q3 = q1.sum(q1, q2); System.out.println(q3.calculate(number)); System.out.println(); q3 = q1.scale(q1, 3); System.out.println(q3.calculate(number)); System.out.println(); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.