Question

In: Computer Science

Write a Java program that solves a set of quadratic equations as per Requirement #1 (without...

Write a Java program that solves a set of quadratic equations as per Requirement #1 (without using global variables), and reads an integer number entered in the command line, as follows: > java program_name input_number and depending on the value of this number does the following:

1) If the number entered is positive, the program shall solve (running in a loop) as many quadratic equations of the following form, as the input_number tells:

a * x2 + b * x + c = 0

where coefficients a, b and c are double values, entered from the keyboard as per Requirement #3.

2) If the input number entered in the command line is not positive, the program shall print the following message to the screen and exit:

A positive value shall be entered in the command line.

3) The values of coefficients a, b and c, as listed in Requirement #1, shall be read from the keyboard by the program with the following message on the screen, each time a new coefficient is needed:

Please enter the next coefficient of a quadratic equation:

4) If the coefficient a is a zero, the program shall display the following message on the screen (first line) repeating the request for another value of a (second line):

Coefficient a cannot be a zero. Enter the correct value. Please enter the next coefficient of a quadratic equation:

5) When the program solves the quadratic equation (calculates the roots), as per Requirement #1, the following output shall be displayed on the screen:

<one empty line>

Quadratic equation with the following coefficients

a: <value>; b: <value>; c: has

the following roots: <root1>and <root2>

6) In case the quadratic equation for given coefficients does not have roots, the program shall print the following message, and continue operation for the next set of coefficients:

<one empty line>

Quadratic equation with the following coefficients:

a: <value>; b: <value>; c: <value> does not have roots

Solutions

Expert Solution

Code is Given Below:

============================

import java.util.Scanner;

public class Quadratic {
   public static void main(String[] args) {
       //creating scanner object to get input from user
       Scanner scn=new Scanner(System.in);
       //asking user to input
       System.out.println("Quadratic Equation solver");
       System.out.print("Enter Positive number: ");
       int n=scn.nextInt();
       //cheking if n is negative
       if(n<=0) {
           System.out.println("A positive value shall be entered in the command line.");
           return;
       }
       int i=1;
       //loop will run n times
       while(n>=i) {
           //asking user for input
           System.out.print("Please enter the next coefficient of a quadratic equation: ");
           double a=scn.nextDouble();
           if(a==0) {
               System.out.println("Coefficient a cannot be a zero. Enter the correct value. ");
               continue;
           }
           System.out.print("Please enter the next coefficient of a quadratic equation: ");
           double b=scn.nextDouble();
           System.out.print("Please enter the next coefficient of a quadratic equation: ");
           double c=scn.nextDouble();
           //finding discreminant
           double d=b*b - 4*a*c;
           //checking if equation has root or not
           if(d<0) {
               System.out.println("a: <"+a+">; b: <"+b+">; c: <"+c+"> does not have roots");
               i++;
               System.out.println();
               continue;
           }
           //finding roots
           double root1 = (-b + Math.sqrt(d))/2*a;
           double root2 = (-b - Math.sqrt(d))/2*a;
           //displaying result
           System.out.println("a: <"+a+">; b: <"+b+">; c: <"+c+"> has the following root");
           System.out.println("<"+root1+"> and <"+root2+">");
           i++;
          
           System.out.println();
       }
   }

}

Output:

===========

Code Snapshot:

==================


Related Solutions

IN PYTHON Write a program to do the following: Solve the a set of equations as...
IN PYTHON Write a program to do the following: Solve the a set of equations as mentioned in "Zybook 5.20 LAB: Brute force equation solver". Instead of the arithmetic operators use your own function defined in a module named calc. You must provide two files (calc.py, brute_force_solver.py) :- 1) calc.py:- Add function named 'add' instead of using operator '+' [10pts] Add function named 'difference' instead of using operator '-' [10pts] Add function named 'product' instead of using operator '*' [10pts]...
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
Write a program in C++ that solves this problem Calculate the area and volume of a...
Write a program in C++ that solves this problem Calculate the area and volume of a sphere problem. Inside a for loop, vary the radius from 10 to 40  with a step or increment of 5 and calculate the area and volume Your radius will be equal to your loop counter. All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma. Print the radius, area,...
QUESTION Write the main while loop for a Java program that processes a set of data...
QUESTION Write the main while loop for a Java program that processes a set of data as follows: Each line of the input consists of 2 numbers representing a quantity and price. Your loop should: 1. Read in the input 2. Calculate the tax. Tax will be 8.875% of price and will only be applicable if price is more than 110. Calculate the new price which is the price plus tax. 3. Calculate the final price by multiplying quantity by...
A Java program that solves a real-world problem The remaining amount (balance) for a prepaid cellular...
A Java program that solves a real-world problem The remaining amount (balance) for a prepaid cellular phone, is computed by subtracting the cost of the phone usage from the original phone load. The phone usage is based only on the total cost of text messages and phone calls. The cost of text messages is determined from the number of text messages sent and the cost of phone calls is determined from the number of minutes of calls made. ● Based...
Write a full program that solves the following equation and displays the value for x and...
Write a full program that solves the following equation and displays the value for x and y: 3.4x+50.2y=44.5 2.1x+.55y=5.9
Write a program for hotel booking system using C++ Program Requirement 1. You can write any...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any program based on the title assigned. 2. The program must fulfill ALL the requirements below. The requirements listed below are the MINIMUM requirement. Your program may extend beyond the requirements if needed. a) Create at least one (1) base class. b) Create at least two (2) derived classes that inherit from the base class created in 2(a). c) Create at least one (1) object...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program,...
MATLAB: Write as a script in the editor window of matlab. Quadratic roots. Write a program, quadroots.m, for finding the roots of the second- order polynomial ax2 + bx + c. Use the quadratic equation. The inputs are the coefficients a,b, and c and the outputs are z1 and z2. The program should produce (exactly) the following output in the Command window when run with (a, b, c) = (1, 2, −3):
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT