Question
IN JAVA, Calculate a two variable systems of equations, as defined by a user; The input is taken as one string per equation. Let the
IN JAVA, Calculate a two variable systems of equations, as defined by a user;
The input is taken as one string per equation. Let the user enter the variables of each equation in any order (ie Ax+By=C, Ax=C-BY, By = C, and more)
If the system has no solutions or infinitely many the output should say so. Use the following getCoefficient() and getConstant methods to do so. Complete this using either the substitution or elimination method - AND DO NOT USE ARRAYS!
/** * This method takes an equation and a variable, and returns that variable's coefficient. * @param equation as a string * @param target variable as a char * @return target variable's coefficient as an int **/ public static int getCoefficient(String equation, char variable) { // Initialize a variable to track the coefficient String coefficient = "0"; int coefficientSign = 1; // remove all spaces, to make things easier equation = equation.replace(" ", "");
// Special case where user inputs single variable. if (equation.length() == 1 && equation.charAt(0) == variable) { return 1; } // Locate specified variable using indexOf // Reverse iterate through the equation starting at variable // Keep going until you reach an operator or a negative for (int i = equation.indexOf(variable)-1; i >= 0; i--) { char c = equation.charAt(i); if (i == equation.indexOf(variable) - 1 && !Character.isDigit(c)){ coefficient = "1"; } if (Character.isDigit(c)){ // if current char is a num coefficient += String.valueOf(c); // add to coeff coefficientSign = 1; } else if(c == '-'){ // if current char is a -, make coeff negative coefficientSign = -1; } else if (c == '+' || c == '='){ break; } } // for special cases like "30=-x", where the variable is after the equals sign. if (equation.indexOf(variable) > equation.indexOf('=')){ coefficientSign = 1; } int coefficientResult = Integer.parseInt(coefficient) * coefficientSign; return coefficientResult; }
/** * This method takes the equation, and returns the constant term. * @param equation as a String * @return constant as an integer (multiplied by constantSign) */ public static int getConstant(String equation) { // initialize a variable to track the constant String constant = ""; String constantSign = ""; // remove all spaces, to make things easier equation = equation.replace(" ", ""); // Check if the equation starts with a negative sign if (equation.charAt(0) == '-') { constantSign = "-"; } // Iterate through the equation, character by character for (int i = 0; i < equation.length(); i++) { char c = equation.charAt(i); if (Character.isDigit(c) || (c == '-' && i == 0)){ // if it's a number or a negative sign at the start constant += String.valueOf(c); } else if (c == '=' ) { // check if the constant is empty and if the char after the equal sign is a number if (constant.isEmpty() && (i + 1 < equation.length() && Character.isDigit(equation.charAt(i + 1)))) { constantSign = ""; continue; } break; } else if (Character.isLetter(c)){ // if it's a letter, reset constant constant = ""; } } int constantResult = Integer.parseInt(constantSign + constant); // return the constant once the for loop is finished return constantResult; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started