Interactive Applications (CLI) and Math



Similar documents
1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Introduction to Java. CS 3: Computer Programming in Java

AP Computer Science Static Methods, Strings, User Input

Zeros of Polynomial Functions

JUST THE MATHS UNIT NUMBER 1.8. ALGEBRA 8 (Polynomials) A.J.Hobson

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Zero: If P is a polynomial and if c is a number such that P (c) = 0 then c is a zero of P.

1.3 Algebraic Expressions

SOLVING QUADRATIC EQUATIONS BY THE NEW TRANSFORMING METHOD (By Nghi H Nguyen Updated Oct 28, 2014))

Mathematics. ( : Focus on free Education) (Chapter 5) (Complex Numbers and Quadratic Equations) (Class XI)

Sample CSE8A midterm Multiple Choice (circle one)

SECTION 0.6: POLYNOMIAL, RATIONAL, AND ALGEBRAIC EXPRESSIONS

4.1. COMPLEX NUMBERS

Chapter 2 Introduction to Java programming

Comp 248 Introduction to Programming

FX 115 MS Training guide. FX 115 MS Calculator. Applicable activities. Quick Reference Guide (inside the calculator cover)

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

1.1 Your First Program

Factoring Polynomials and Solving Quadratic Equations

This unit has primarily been about quadratics, and parabolas. Answer the following questions to aid yourselves in creating your own study guide.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Partial Fractions. Combining fractions over a common denominator is a familiar operation from algebra:

Basics of Java Programming Input and the Scanner class

Building Java Programs

NSM100 Introduction to Algebra Chapter 5 Notes Factoring

FACTORING QUADRATICS and 8.1.2

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Linear Equations in One Variable

Display Format To change the exponential display format, press the [MODE] key 3 times.

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

The Method of Partial Fractions Math 121 Calculus II Spring 2015

Introduction to Java

AP Computer Science Java Mr. Clausen Program 9A, 9B

Building Java Programs

Chapter 2: Elements of Java

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Program to solve first and second degree equations

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Part I. Multiple Choice Questions (2 points each):

ALGEBRA 2 CRA 2 REVIEW - Chapters 1-6 Answer Section

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

SOLVING QUADRATIC EQUATIONS - COMPARE THE FACTORING ac METHOD AND THE NEW DIAGONAL SUM METHOD By Nghi H. Nguyen

Math Common Core Sampler Test

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

MATH 143 Pre-calculus Algebra and Analytic Geometry

Using Files as Input/Output in Java 5.0 Applications

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

AP Computer Science Java Subset

Clovis Community College Core Competencies Assessment Area II: Mathematics Algebra

Factoring Quadratic Expressions

Unit 3: Day 2: Factoring Polynomial Expressions

Java Programming Fundamentals

FINAL EXAM SECTIONS AND OBJECTIVES FOR COLLEGE ALGEBRA

What are the place values to the left of the decimal point and their associated powers of ten?

1 Lecture: Integration of rational functions by decomposition

Building Java Programs

Introduction to Programming

0.8 Rational Expressions and Equations

ALGEBRA 2: 4.1 Graph Quadratic Functions in Standard Form

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Notes on Determinant

Topic 11 Scanner object, conditional execution

JAVA ARRAY EXAMPLE PDF

Math Placement Test Study Guide. 2. The test consists entirely of multiple choice questions, each with five choices.

0.4 FACTORING POLYNOMIALS

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

QUADRATIC, EXPONENTIAL AND LOGARITHMIC FUNCTIONS

Algebra 2/Trig Unit 2 Notes Packet Period: Quadratic Equations

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

(Eng. Hayam Reda Seireg) Sheet Java

Cover Page. MTT 1, MTT 2, MTT 3, MTT 4 Developmental Mathematics I, II, III, IV MTE 1, MTE 2, MTE 3, MTE 4, MTE 5, MTE 6, MTE 7, MTE 8, MTE 9

Solving Cubic Polynomials

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

07/15/10 Math E-10 Precalculus Fall Course Requirements

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Solving Quadratic Equations

Algebra I Credit Recovery

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Prerequisites: TSI Math Complete and high school Algebra II and geometry or MATH 0303.

Programming Fundamentals I CS 110, Central Washington University. November 2015

ASEN Structures. MDOF Dynamic Systems. ASEN 3112 Lecture 1 Slide 1

Zeros of Polynomial Functions

Chapter 3. Input and output. 3.1 The System class

Polynomial Operations and Factoring

CPLEX Tutorial Handout

COLLEGE ALGEBRA 10 TH EDITION LIAL HORNSBY SCHNEIDER 1.1-1

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

LINKED DATA STRUCTURES

Building Java Programs

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

MATH 21. College Algebra 1 Lecture Notes

Chapter 8 Selection 8-1

Introduction to programming

Transcription:

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 1

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.nexttype(); 2

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 3

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 4

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 5

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 130) 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 128 in Java. We need to calculate each root separately 6

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 correct but are not used correctly in the program Since the user can enter any combination of three integer values for the coefficients, we need to analyze the possible special cases where just computing the formula based on the input values of a, b, and c is not correct This is the introduction to your Project #1 7

Solving Quadratic Equations User can enter any values for a, b, and c If the user enters values that cannot be computed properly using the formulas, the program will fail to operate correctly Let s try a = 1, b = 0, and c = 1 The program generates two answers NaN stands for Not a Number What happened? 8

Solving Quadratic Equations With those coefficient values, the formula for calculating the discriminant results in a negative number discriminant = b * b 4 * a * c discriminant = 0 * 0 4 * 1 * 1 discriminant = -4 Later in calculating the roots, the formula takes the square root of the discriminant Mathematically, a negative number does not have a real square root 9

Solving Quadratic Equations The Math.sqrt() method can t provide any real number that is the square root of -4 In this case, it returns the result NaN However, in algebra we learned to fake the square root of a negative number by using the imaginary number i (the square root of -1) Math.sqrt(-4) can be shown as: Math.sqrt(-1 * 4) which equals: Math.sqrt(-1) * Math.sqrt(4) which equals: i * 2.0 where i is the imaginary square root of -1 How can we get our program to print this answer? 10

Solving Quadratic Equations We need to write the program so that our code checks the value of the discriminant before trying to take the square root of it If the value of the discriminant is negative, we need to construct the correct answer = i * + Math.sqrt(Math.abs(-4)); That code will provide the resulting String = i * 2.0 11

Solving Quadratic Equations There are other possible values of a, b, and c that can result in NaN or no valid result Suppose the user enters a value of 0 for a? The formula for the roots divides by (2.0 * a) If the value of a is 0, the division is impossible The expression evaluations will provide the results NaN or Infinity Again, we need to write the program so that our code checks the value of a before trying to do the division 12

Solving Quadratic Equations If the value of a is 0, is there a solution? Yes, let s look at the equation with a = 0 0 * Math.pow(x, 2) + b * x + c = 0 is the same as: b * x + c = 0 which can be solved as a linear equation: x = - c / b as long as b is not equal to 0! Note: There is now only one root - not two Based on the above, we can see another special case, if b is equal to 0 (but only if a is also equal to 0) It is OK for b to be equal to 0, if a is not 0 13

Solving Quadratic Equations Suppose both a = 0 and b = 0? The remaining equation looks like this: 0 + 0 + c = 0 If the user entered a value of 0 for c, then any value of x is a solution, i.e. 0 + 0 + 0 always = 0 But, suppose the user had entered a value for c that was not equal to 0? Now, there is no possible solution for x No value of x can make a non-zero value of c be equal to 0 14

Solving Quadratic Equations Now that we have covered all these cases, what does it mean for our programming of a program for solving quadratic equations based on values for a, b, and c? We need to write the program so that our code makes decisions about each of these possible special cases before just trying to calculate a result based on the formulas 15

Control Flow Up until now, all of our programs just ran sequentially through a sequence of steps Each statement did something and then continued to the next statement in sequence To make decisions while solving a quadratic equation, we need to control the flow of the execution of statements in our program We will see how to do that in the next lecture 16

Factoring our Program But before we do that, let s see how to divide our program into smaller parts This is called factoring the program If we think about it, we can envision two different things that our program has to do Gather input from the user and display results Calculate the results based on the formulas At a top level, we can create one class for each of those two parts of the problem 17

Factoring our Program Why would we want to break our program down into two parts or classes? There are many possible reasons, two are: We may assign two programmers to the job Each programmer can write one of the classes We may be able to reuse one part separately from the other part, e.g. use the calculation class with a CLI class initially and re-use it later with a GUI class to make it more user friendly 18

Factoring our Program Proposed class diagram for our program: Remember that one of our classes must have a main method QuadraticCLI + main(string [ ]): void QuadraticSolver A dotted arrow means that the QuadraticCLI class depends on the QuadraticSolver class + getequation (a : int, b : int, c int) : String + getsolution (a : int, b : int, c int) : String 19

Factoring our Program Note that the method names in both classes are underlined in the class diagram That means that they are specified to be static just like the Math class methods We call the QuadraticSolver methods with the class name and pass the specified parameters to the method by listing them inside the ( ) Each method returns a String to be printed 20

The QuadraticCLI Class Passing the inputs to the QuadraticSolver class 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 display and solve the equation // pass a, b, and c as parameters in the method calls System.out.println(QuadraticSolver.getEquation(a, b, c)); System.out.println(QuadraticSolver.getSolution(a, b, c)); 21

The QuadraticSolver Class A method to display a quadratic equation with coefficients a, b, and c as a String public static String getequation(int a, int b, int c) { String result = null; // code to concatenate string for returning equation text result = "Solving: " + a + "x\u00b2 " + ( (b >= 0)? "+ " : "") + b + "x " + ( (c >= 0)? "+ " : "") + c + " = 0"; return result; } The above method uses string concatenation There is an embedded Unicode value \u00b2 to get the superscripted 2 to display the x 2 term 22

The QuadraticSolver Class A method to provide the solution as a String public String getsolution(int a, int b, int c) { String solution = null; } // Use if-else to choose the correct formulas // using the provided parameters a, b, and c. // Use string concatenation to create a // solution String that can be returned // to the QuadraticCLI class.... Your Java statements go here return solution; 23