{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture05 - Interactive Applications(CLI and Math...

Info icon This 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
Image of page 1

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

View Full Document Right 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 ();
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
Image of page 3

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

View Full Document Right 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, Expression Evaluation, and Assignment
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
Image of page 5

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

View Full Document Right 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
Image of page 6
7 Solving Quadratic Equations However, the textbook’s program to solve for the roots of a quadratic equation is deficient!
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern