Bifalgorithm post · Computer science one · Computing Quadratic Roots

What is 26?

What is 26? (Computer science one · Computing Quadratic Roots) — "26.6. Examples 1 a = Double.parseDouble(args[0]); 2 b = Double.parseDouble(args[1]); 3 c = Do…

Ideas in this flowchart

  • Programa Java para calcular las raíces de una ecuación cuadrática y considerar entradas problemáticas
  • Implementación básica del programa
  • El programa lee los valores a, b y c mediante Scanner
  • También puede convertir tres argumentos de la línea de comandos con Double.parseDouble
  • Calcula las raíces usando la fórmula cuadrática
  • Muestra los valores de las raíces y de la ecuación
  • Posibles entradas problemáticas
  • El usuario puede introducir cero para a
  • El discriminante puede ser negativo cuando b² < 4ac
  • El usuario puede introducir valores no numéricos
  • La versión con argumentos puede recibir menos o más de tres argumentos
  • Los condicionales permiten prevenir consecuencias, manejar entradas incorrectas y comunicar errores al usuario
Feed/

What is 26?

No cover

Channel

Computer science one

1 page

"26.6. Examples 1 a = Double.parseDouble(args[0]); 2 b = Double.parseDouble(args[1]); 3 c = Double.parseDouble(args[2]); Finally, think about the possible inputs a user could provide that may cause problems for this program. For example: • What if the user entered zero for a ? • What if the user entered some combination such that b 2 < 4 ac ? • What if the user entered non-numeric values? • For the command line argument version, what if the user provided less than three arguments? Or more? How might we prevent the consequences of such bad input? How might we handle the event that a user enters bad input and how do we communicate these errors to the user? To start to resolve these issues, we’ll need conditionals. 401\n\n26. Basics 1 package unl.cse; 2 3 import java.util.Scanner; 4 5 /** 6 * This program computes the roots to a quadratic equation 7 * using the quadratic formula. 8 */ 9 public class QuadraticRoots { 10 11 public static void main(String args[]) { 12 13 double a, b, c, root1, root2; 14 Scanner s = new Scanner(System.in); 15 16 System.out.println(\"Please enter a: \"); 17 a = s.nextDouble(); 18 System.out.println(\"Please enter b: \"); 19 b = s.nextDouble(); 20 System.out.println(\"Please enter c: \"); 21 c = s.nextDouble(); 22 23 root1 = (-b + Math.sqrt(b*b - 4*a*c) ) / (2*a); 24 root2 = (-b - Math.sqrt(b*b - 4*a*c) ) / (2*a); 25 26 System.out.printf(\"The roots of %fx^2 + %fx + %f are:\n\n\", 27 a, b, c); 28 System.out.printf(\" root1 = %f\n\n\", root1); 29 System.out.printf(\" root2 = %f\n\n\", root2); 30 31 } 32 33 } Code Sample 26.4.: Quadratic Roots Program in Java 402"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.