Question

In: Computer Science

Java - In this lab, you will be writing your own method using the brute force...

Java - In this lab, you will be writing your own method using the brute force algorithm, to solve a second degree polynomial.
This method will take in four parameters, three coefficients, and one constant, and it will have the following signature:

public static String bruteForceEquationSolver(int one, int two, int three, int constant){}

The equation you are trying to solve is of the following format, given the parameters stated above:

(constant) = (one) * x + (two) * y + (three) * z

X, Y, and Z are variables that you are finding solutions for.

For example, if you were to call the method with parameters (2, 3, 4, 42), you would be finding the solution for the equation 2x + 3y + 4z = 42.

Some specifications for the method:

  • Your method should try every possibility within the range (1-10) for each variable. Check possibilities using nested for loops, starting with x, then y, then z.
  • Your method should return if a solution is found, and return a string with the solutions in the following format:

    x: xSolution y: ySolution z: zSolution

    There should be a space between the solution and the next variable, and each variable letter should be followed by a colon and a space.

  • If no solution is found, return the string “Solution not found.”.

Call the testBFES() method from main after completing this method to ensure it is working as expected.

Here's what I have so far:

public static String bruteForceEquationSolver(int one, int two, int three, int constant) {
    // student code here
        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= 10; j++) {
                for (int k = 1; k <= 10; k++) {
                }
            }
        }
        return String.format("x:%d y:%d z:%d", one, two, three); 
}
public static void testBFES() {
System.out.println("Testing Brute Force Equation Solver");
String expected = "x: 2 y: 3 z: 4";
System.out.println("Expecting: " + expected);

String actual = bruteForceEquationSolver(3, 4, 6, 42);
System.out.println("Actual: " + actual);

boolean correct = expected.equals(actual);
System.out.println("Outputs equal? " + correct);
}

public static void testMT() {
System.out.println("Testing Multiplication Table");

String expected = "1\t2\t3\t4\t\n2\t4\t6\t8\t\n3\t6\t9\t12\t\n";
System.out.print("Expecting:\n" + expected);

String actual = multiplicationTable(3, 4);
System.out.print("Actual:\n" + actual);

boolean correct = expected.equals(actual);
System.out.println("Outputs equal? " + correct);
}

Solutions

Expert Solution

CODE/OUTPUT image






CODE:

Kindly upvote if this helped you. Comment for more help.

public class EquationSolver {
public static void main(String[] args) {
        String answer = bruteForceEquationSolver(2,3,4,42);
        System.out.println(answer);
}

public static String bruteForceEquationSolver(int one, int two, int three, int constant) {
       
        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= 10; j++) {
                for (int k = 1; k <= 10; k++) {
                if(  ((i*one) + (j*two) + (k*three)) == constant) {     
                          return String.format("x:%d y:%d z:%d", i, j, k);
                }
                }
            }
        }
        return "Solution not found";
}
}

Related Solutions

IN JAVA, For this exercise, you will create your own example of using a generic method....
IN JAVA, For this exercise, you will create your own example of using a generic method. First write a program that calls a method to multiply two integers and return the integer result. Then modify that program to make the method generic, calling it with two generic data types (T1 and T2) and then returning the result in the same type (also needs to be generic). Demonstrate your program by calling the method two times, once with an integer and...
For this lab, you will be writing method definitions. These method definitions will be based on...
For this lab, you will be writing method definitions. These method definitions will be based on method calls I have already written for you. Do all of the following steps in order. DO NOT move onto the next step until the previous one is complete. 1. Download the starting file called Lab10.java. 2. Within this file, you will find a completely written main method. Please DO NOT modify this code. Do read the code and familiarize yourself with what it...
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces....
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces. Please make sure you have a partner for this lab. No code is provided with this lab. Write code to experimentally resolve the following questions about Java. You will need to show your code to the TA or lab assistant. Part I: Assignments and Casting (1 point) ------------------------------------------ Let Y be a subclass of X, and let y and x be variables of classes...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And...
Can all problems in Karp 21 be solved using an Exhaustive search or Brute force? And why
3.       How many successful and unsuccessful comparisons will by made using brute force string matching in a...
3.       How many successful and unsuccessful comparisons will by made using brute force string matching in a binary string of 1000 zeros while trying to match the following patters: a.       1000 b.       0001 c.       00010
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings,...
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings, and improve your skills with for loops. The methods you will write will be part of a short trivia game, feel free to try it out after you finish.   import java.util.Scanner; public class Trivia { //TODO: isLeapYear //TODO: isPrime //TODO: aWord //TODO: reverse public static void main(String[] args){ Scanner answers = new Scanner(System.in); int score = 0; System.out.println("What year is a Leap Year?"); //...
***USING JAVA Scenario: You will be writing a program that will allow a user to find...
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word. Requirements: Do not use Arrays for this assignment. Do not use any String class methods (.phrase(), replace methods, regex methods)...
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
Sometimes, brute force is the best we can do. Consider the following prob- lem: You live...
Sometimes, brute force is the best we can do. Consider the following prob- lem: You live in a building of n floors. Someone keeps throwing eggs out of a balcony on some unknown floor f . At each time slot, this person will throw an egg. At each time slot, you can go to a balcony on any floor and attempt to save the egg. If the balcony you chose is below the thrower’s, you save the egg. If, however,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT