Lecture05 - Interactive Applications (CLI) and Math...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example: Factoring the Solution Reading for this class: L&L, 3.5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Interactive Applications (CLI) An interactive program with a command line interface contains a sequence of steps to: Prompt the user to enter input data Read and save the user’s responses Process the data after all input(s) are received We can prompt the user: System.out.println(“prompt text”); We can read and format user responses: type variable = scan.next Type ();
Background image of page 2
3 Interactive Applications (CLI) Similar to Quadratic.java (Page 129) int a, b, c; // integer coefficients Scanner scan = new Scanner(System.in); System.out.println(“Enter coefficient A”); a = scan.nextInt(); System.out.println(“Enter coefficient B”); b = scan.nextInt(); System.out.println(“Enter coefficient C”); c = scan.nextInt(); // we have the data to solve the equation // ax-squared + bx + c = 0 for it’s roots
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 We have the input values, now what? To solve the quadratic equation, we need to program in Java the formulas learned in high school algebra: discriminant = b squared – 4ac root1 = (-b + squareroot of discriminant)/2a root2 = (-b - squareroot of discriminant)/2a How do we program those equations? We need to use the Math Class Library,
Background image of page 4
5 The Math Class The Math class is part of the java.lang package The Math class contains methods that perform various mathematical functions These include: absolute value square root exponentiation trigonometric functions
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 The Math Class The methods of the Math class are static methods (also called class methods ) Static methods can be invoked through the class name – no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); Similar to Quadratic.java (page 129) discriminant = Math.pow(b, 2) – 4.0 * a * c; root1 = (-1.0 * b + Math.sqrt(discriminant))/(2.0 * a); root2 = (-1.0 * b – Math.sqrt(discriminant))/(2.0 * a); Note: We can’t program the + in the formula on page 130 in Java. We need to calculate each root separately
Background image of page 6
7 Solving Quadratic Equations However, the textbook’s program to solve for the roots of a quadratic equation is deficient! The equations for calculating the roots are
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CS 110 taught by Professor Wilson during the Spring '10 term at University of Massachusetts Boston.

Page1 / 24

Lecture05 - Interactive Applications (CLI) and Math...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online