C++ : Design a class named LinearEquation for a 2 X 2 system of linear equations
ID: 3769981 • Letter: C
Question
C++: Design a class named LinearEquation for a 2 X 2 system of linear equations:
ax + by = e
cx + dy = f
x = (ed - bf)/(ad-bc)
y = (af - ec)/(ad-bc)
The class contains:
- Private data fields a, b, c, d, e, and f.
- A constructor with the arguments for a, b, c, d, e, and f.
- Six get functions for a, b, c, d, e, and f.
- A function named isSolvable() that returns true if ad - bc is not 0.
- Functions getX() and getY() that return the solution for the equation.
Write a test program that prompts the user to
enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that the equation has no solution
Explanation / Answer
Main Program:
CODE:
public class LinearEquation {
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;
private double x;
private double y;
LinearEquation(double aa, double bb, double cc, double dd, double ee, double ff) {
a = aa;
b = bb;
c = cc;
d = dd;
e = ee;
f = ff;
}
boolean isSolvable() {
if (a*d-b*c != 0) return true;
else return false;
}
double getX() {
double x = ((e*d)-(b*f)/(a*d-b*c));
return x;
}
double getY() {
double y = ((a*f-e*c)/(a*d-b*c));
return y;
}
double getA() {
return a;
}
void setA(double newA){
a = newA;
}
double getB(){
return b;
}
void setB(double newB){
b = newB;
}
double getC() {
return c;
}
void setC(double newC){
c = newC;
}
double getD() {
return d;
}
void setD(double newD) {
d = newD;
}
double getE() {
return e;
}
void setE(double newE) {
e = newE;
}
double getF() {
return f;
}
void setF(double newF) {
f = newF;
}
}
Test Program:
CODE:
import java.util.Scanner;
public class Linearrr {
public static void main(String[] thy){
Scanner ipt = new Scanner(System.in);
System.out.println("Enter an Integer for Each of The Following Varables: a, b, c, d, e, f: ");
double a = ipt.nextDouble();
double b = ipt.nextDouble();
double c = ipt.nextDouble();
double d = ipt.nextDouble();
double e = ipt.nextDouble();
double f = ipt.nextDouble();
LinearEquation leq=new LinearEquation(a,b,c,d,e,f);
leq.isSolvable();
leq.getX();
leq.getY();
ipt.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.