The program below is the solution to Liang’s Introduction to Java (9th Edition) Chapter 3 Exercise 3.3.
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> */<br />import java.util.Scanner;<br /><br />public class Ex03_03 {<br /> public static void main(String[] args) {<br /> // declare variables<br /> double a, b, c, d, e, f, num1, num2, denom, x, y;<br /> <br /> // create Scanner to read user input<br /> Scanner input = new Scanner(System.in);<br /><br /> System.out.print("This program solves 2x3 system of linear equations. "<br /> + "nnFor example in the following equation 9x + 4y = -6 "<br /> + "n 3x - 5y = -21"<br /> + "na, b, c, d, e, f = 9, 4, 3, -5, -6, -21 respectively. "<br /> + "nEnter each number separated by pressing space or enter.");<br /> <br /> // prompt user to enter details<br /> System.out.print("nnEnter a, b, c, d, e, f: ");<br /> a = input.nextDouble();<br /> b = input.nextDouble();<br /> c = input.nextDouble();<br /> d = input.nextDouble();<br /> e = input.nextDouble();<br /> f = input.nextDouble();<br /> <br /> //calculate x and y<br /> num1 = ((e * d) - (b * f));<br /> num2 = ((a * f) - (e * c));<br /> denom = ((a * d) - (b * c));<br /> <br /> x = num1 / denom;<br /> y = num2 / denom;<br /> <br /> if (denom == 0) {<br /> System.out.println("nThe equation has no solution.");<br /> } else {<br /> System.out.println("x is " + x + " and y is " + y);<br /> }<br /> }<br />}<br /> |
Program sample runClick here…