In Java Programing write two classes: 1.) The first is a class called LinearEqua
ID: 3752209 • Letter: I
Question
In Java Programing
write two classes:
1.)
The first is a class called LinearEquation that stores two linear equations.
ax + by = e
cx + dy = f
x = (ed - bf) / (ad - bc)
y = (af-ec) / (ad - bc)
It should have:
Private data fields a, b, c, d, e and f
Constructor that takes in arguments for a through f.
Six getter methods for a, b, c, d, e, and f
A method named isSolvable() that returns true if a*d - b*c is not 0.
Methods getX() and getY() that return the solution for the equation.
this class should include a UML diagram
2.) Second, write a test program (class) that prompts the user for the six values and report if the equation has a solution. If so, display the solution.
Explanation / Answer
SolveEquations.java
public class SolveEquations {
//Declaring instance variables
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;
//Parameterized constructor
public SolveEquations(double a, double b, double c, double d, double e, double f) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
//getters and setters
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public double getB() {
return b;
}
public void setB(double b) {
this.b = b;
}
public double getC() {
return c;
}
public void setC(double c) {
this.c = c;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
public double getE() {
return e;
}
public void setE(double e) {
this.e = e;
}
public double getF() {
return f;
}
public void setF(double f) {
this.f = f;
}
public boolean isSolvable() {
if ((a * d - b * c) != 0)
return true;
else
return false;
}
public double getX() {
return (e * d - b * f) / (a * d - b * c);
}
public double getY() {
return (a * f - e * c) / (a * d - b * c);
}
}
____________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
double a, b, c, d, e, f, x, y;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter a:");
a = sc.nextDouble();
System.out.print("Enter b:");
b = sc.nextDouble();
System.out.print("Enter c:");
c = sc.nextDouble();
System.out.print("Enter d:");
d = sc.nextDouble();
System.out.print("Enter e:");
e = sc.nextDouble();
System.out.print("Enter f:");
f = sc.nextDouble();
//Creating an instance of SolveEquations class
SolveEquations se = new SolveEquations(a, b, c, d, e, f);
//Based on the equations is solvable or not displaying the solutions
if (se.isSolvable()) {
System.out.println("X = " + se.getX());
System.out.println("Y = " + se.getY());
} else {
System.out.println(":: Equations has no solutions ::");
}
}
}
_________________
Output:
Enter a:2
Enter b:4
Enter c:6
Enter d:6
Enter e:2
Enter f:1
X = -0.6666666666666666
Y = 0.8333333333333334
_____Could you plz rate me well.Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.