Question

In: Computer Science

You must implement a simple program that asks the user for three values, a, b, and...

You must implement a simple program that asks the user for three values, a, b, and c, which you should store as double values. Using these numbers, you compute the value of the two solutions to the quadratic equation, assuming that the equation is of the form: ax^2 + bx + c = 0

Complete the description of the program given above so that your output looks as close to the following sample output as possible. In this sample, the user entered 2, -5, and 2, respectively. User input is shown in green.

Output:

Welcome to the quadratic equation solver!

For an equation of the form ax^2 + bx + c,

Enter a: 2

Enter b: -5

Enter c: 2

The answers are x = 2.0 and x = 0.5

Need to use the quadratic formula. Have to compute the + and - parts separately, since Java does not contain a ± operator. You will need to use the Math.sqrt() method.

Solutions

Expert Solution

import java.util.Scanner;

public class QuadraticEquation {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Welcome to the quadratic equation solver!");
        System.out.println("For an equation of the form ax^2 + bx + c,");
        System.out.print("Enter a: ");
        double a = in.nextDouble();
        System.out.print("Enter b: ");
        double b = in.nextDouble();
        System.out.print("Enter c: ");
        double c = in.nextDouble();
        double d = b * b - 4 * a * c;
        if (a == 0 || d < 0) {
            System.out.println("This equation has no real roots.");
        } else {
            double r1 = (-b + Math.sqrt(d)) / (2 * a);
            double r2 = (-b - Math.sqrt(d)) / (2 * a);
            System.out.println("The answers are x = " + r1 + " and x = " + r2);
        }
    }
}


Related Solutions

Must implement a simple program that asks the user for a width and a height, storing...
Must implement a simple program that asks the user for a width and a height, storing them as double values. Afterwards, you compute area and perimeter as if the shape were a rectangle. Output the results. Then, you compute the area and perimeter as if the shape were a right triangle and you have been given the base and height. Area and Perimeter Calculator Enter width: 3 Enter height: 5 If your shape is a rectangle, its area is 15.0...
Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Selection Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using SELECTION SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_SelectionSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user...
Bubble Sort Programmatically Implement in C++ the necessary program that does the following: Asks the user and gets at least 5 whole numbers as user input from the keyboard and stores them in an array Displays the numbers from the array on the screen Sorts the numbers in the array using BUBBLE SORT Algorithm Displays the sorted numbers on the screen from the array Save your code file as "yourLastname_Firstname_BubbleSort.cpp" and submit your .cpp file. NOTE: This assignment needs only...
Write a program that asks the user to type in two integer values. Test these two...
Write a program that asks the user to type in two integer values. Test these two numbers to determine whether the first is evenly divisible by the second and then display the appropriate message to the terminal. Objective C
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a program that: a. Asks the user for their first name using a JOptionPane. b....
Write a program that: a. Asks the user for their first name using a JOptionPane. b. Asks the user for their age using a JOptionPane. C. Asks the user for their last name using a JOptionPane. d. Pops up a dialog with the following information: i. Number of characters in the first & last name combined ii. The full name all-in upper-case letters iii. The full name all-in lower-case letters iv. The first letter of the name v. The age...
Write a java simple document retrieval program that first asks the user to enter a single...
Write a java simple document retrieval program that first asks the user to enter a single term query, then goes through two docuements named doc1.txt and doc2.txt (provided with assignment7) to find which document is more relevant by calculating the frequency of the query term in each document. Output the conclusion to a file named asmnt7output.txt in the following format (as an example for query “java” and “problem”). The percentages in parenthese are respective supporting frequencies. java: doc1(6.37%) is more...
The program asks user to enter three integers, and the displays a message indicating whether the...
The program asks user to enter three integers, and the displays a message indicating whether the numbers are in sequential order, in reverse order or in neither order. Find the errors and correct the program codes in the if comparison expressions. (10 points) 1.     using System; 2.     using static System.Console; 3. 4.     class Sorting 5.     { 6.              static void Main() 7.              { 8.                       int num1, num2, num3; 9.                       string snum1, snum2, snum3; 10.                    Write ("Enter first number "); 11.                    snum1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT