I need help with an exercise in Java. Here is the exercise. Please provide a bas
ID: 3792292 • Letter: I
Question
I need help with an exercise in Java. Here is the exercise. Please provide a basic and simple code. Thank you:
Design a class named LinearEquation for a 2-by-2 system of linear equations:
ax + by = e x = (ed – bf)/ (ad – bc)
cx + dy = f y = (af – ec)/ (ad – bc)
The class contains:
Private double data fields a, b, c, d, e, and f.
A constructor with the arguments for a, b, c, d, e, and f.
Six getter methods for a, b, c, d, e, and f.
Methods getX() and getY() that return the solution for the equation.
A method named isSolvable() that returns true if ad - bc is not 0. The method isSolvable() will be called in the main, if it returns true display the calculated values for x and y by calling getX() and getY(). If the call to isSolvable() returns false, display a message that “the equation has no solution”.
Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the result. After the values for a through f are entered, create a LinearEquation object. Check to see if ad - bc is 0, report that “The equation has no solution”, otherwise print out the values for x and y.
Explanation / Answer
// LinearEquation.java
import java.util.Scanner;
class LinearEquation
{
private double a;
private double b;
private double c;
private double d;
private double e;
private double f;
LinearEquation(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;
}
double getA()
{
return a;
}
void setA(double a)
{
this.a = a;
}
double getB()
{
return b;
}
void setB(double b)
{
this.b = b;
}
double getC()
{
return c;
}
void setC(double c)
{
this.c = c;
}
double getD()
{
return d;
}
void setD(double d)
{
this.d = d;
}
double getE()
{
return e;
}
void setE(double e)
{
this.e = e;
}
double getF()
{
return f;
}
void setF(double f)
{
this.f = f;
}
boolean isSolvable()
{
if (a*d-b*c == 0)
{
return false;
}
else
return true;
}
double getX()
{
return ((e*d)-(b*f)/(a*d-b*c));
}
double getY()
{
return ((a*f-e*c)/(a*d-b*c));
}
public static void main(String[] args)
{
double a,b,c,d,e,f;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a: ");
a = scan.nextDouble();
System.out.print("Enter b: ");
b = scan.nextDouble();
System.out.print("Enter c: ");
c = scan.nextDouble();
System.out.print("Enter d: ");
d = scan.nextDouble();
System.out.print("Enter e: ");
e = scan.nextDouble();
System.out.print("Enter f: ");
f = scan.nextDouble();
LinearEquation eq = new LinearEquation(a,b,c,d,e,f);
if(eq.isSolvable() == true)
{
System.out.println("x: " + eq.getX() + " y: " + eq.getY());
}
else
{
System.out.println("The equation has no soluation");
}
}
}
/*
output:
Enter a: 4
Enter b: 5
Enter c: 5
Enter d: 3
Enter e: 2
Enter f: 5
x: 7.923076923076923
y: -0.7692307692307693
Enter a: 3
Enter b: 3
Enter c: 3
Enter d: 3
Enter e: 8
Enter f: 7
The equation has no soluation
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.