Q:

(Algebra: 2 * 2 linear equations) Design a class named LinearEquation for a 2 * 2 system of linear equations:

0

(Algebra: × 2 linear equations) Design a class named LinearEquation for a 2 × 2 system of linear equations:

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 getter methods for a, b, c, d, e, and f.
A method named isSolvable() that returns true if ad - bc is not 0.
Methods getX() and getY() that return the solution for the equation.

Draw the UML diagram for the class and then implement the class. 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.” See Programming Exercise 3.3 for sample runs.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (Algebra: 2 * 2 linear equations) Design a class named LinearEquation for a    *
* 2 * 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 getter methods for a, b, c, d, e, and f.                                 *
* ■ A method named isSolvable() that returns true if ad - bc is not 0.           *
* ■ Methods getX() and getY() that return the solution for the equation.         *
*                                                                                *
* Draw the UML diagram for the class and then implement the class. 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.” See       *
* Programming Exercise 3.3 for sample runs.                                      *
*********************************************************************************/
import java.util.Scanner;

public class Exercise_09_11 {
	/** Main method */
	public static void main(String[] args) {
		// Create a Scanner object
		Scanner input = new Scanner(System.in);

		
		// Prompt the user to enter a, b, c, d, e, and f
		System.out.print("Enter a, b, c, d, e, f: ");
		double a = input.nextDouble();
		double b = input.nextDouble();
		double c = input.nextDouble();
		double d = input.nextDouble();
		double e = input.nextDouble();
		double f = input.nextDouble();

		// Create a LinearEquation object
		LinearEquation linearEquation = new LinearEquation(a, b, c, d, e, f);
		
		// Display results
		if (linearEquation.isSolvable()) {
			System.out.println("x is " + linearEquation.getX() +
				" and y is " + linearEquation.getY());
		}
		else
			System.out.println("The equation has no solution.");
	}
}

LinearEquation.java

// UML diagram
/*****************************************************
*                    LinearEquation                  *
*----------------------------------------------------*
* -a: double                                         *
* -b: double                                         *
* -c: double                                         *
* -d: double                                         *
* -e: double                                         *
* -f: double                                         *
* +LinearEquation(a: double, b: double, c: double,   * 
*                 d: double, e: double, f: double)   *
* +getA(): double                                    *
* +getB(): double                                    *
* +getC(): double                                    *
* +getD(): double                                    *
* +getE(): double                                    *
* +getF(): double                                    *
* +isSolvable(): boolean                             *
* getX(): double                                     *
* getY(): double                                     *
*****************************************************/

// Implement LinearEquation class
public class LinearEquation {
	// Data fields
	private double a;
	private double b;
	private double c;
	private double d;
	private double e;
	private double f;

	/** Constructor with arguments for a, b, c, d, e, and 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;
	}

	/** Methods */
	/** Return a */
	public double getA() {
		return a;
	}

	/** Return b */
	public double getB() {
		return b;
	}

	/** Return c */
	public double getC() {
		return c;
	}

	/** Return d */
	public double getD() {
		return d;
	}

	/** Return e */
	public double getE() {
		return e;
	}

	/** Return f */
	public double getf() {
		return f;
	}

	/** Returns true if ad - bc is not 0 */
	public boolean isSolvable() {
		return a * d - b * c != 0;
	}

	/** Returns x */
	public double getX() {
		return (e * d - b * f) / (a * d - b * c);
	}

	/** Returns y */
	public double getY() {
		return (a * f - e * c) / (a * d - b * c);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now