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]...
you are to write a program in Java, that reads in a set of descriptions of...
you are to write a program in Java, that reads in a set of descriptions of various geometric shapes, calculates the areas and circumferences of the shapes, and then prints out the list of shapes and their areas in sorted order from smallest to largest area. There are four possible shapes: Circle, Square, Rectangle, and Triangle. The last is always an equilateral triangle. The program should read from standard input and write to standard output. The program should read until...
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
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
(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 Java that reads in a set of positive integers and outputs how...
Write a program in Java that reads in a set of positive integers and outputs how many times a particular number appears in the list. You may assume that the data set has at most 100 numbers and -999 marks the end of the input data. The numbers must be output in increasing order. For example, for the data 15 40 28 62 95 15 28 13 62 65 48 95 65 62 65 95 95 -999 The output is...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from 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,...
JAVA PROGRAM Without changing changing main or PlayOneGame, you need to write the rest of the...
JAVA PROGRAM Without changing changing main or PlayOneGame, you need to write the rest of the methods. Please complete "Rock, Scissor, Paper" game. Main: public static void main(String[] args ) { int wins = 0; int losses = 0; PrintWelcomeMessage(); // Just says hello and explains the rules while( PlayerWantsToPlay() ) // Asks the player if they want to play, and returns true or false { boolean outcome = PlayOneGame(); // One full game. We do NOT come back here...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT