120 likes | 135 Views
Interactive Applications (CLI) and Math. Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The if Statement Reading for this class: L&L, 3.5, 5.1, 5.2. Interactive Applications (CLI).
E N D
Interactive Applications (CLI) and Math • Interactive Applications • Command Line Interfaces • The Math Class • Flow of Control / Conditional Statements • The if Statement • Reading for this class: L&L, 3.5, 5.1, 5.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.nextType();
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
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
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
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
Flow of Control • Unless specified otherwise, the order of statement execution through a method is linear: • one statement after another in sequence • Some programming statements allow us to: • decide whether or not to execute a particular statement • execute a statement over and over, repetitively • These decisions are based on boolean expressions (or conditions) that evaluate to true or false • The order of statement execution is called the flow of control
Conditional Statements • A conditional statement lets us choose which statement will be executed next • Therefore they are sometimes called selection statements • Conditional statements give us the power to make basic decisions • The Java conditional statements are the: • if statement • if-else statement • switch statement
The condition must be a boolean expression. It must evaluate to either true or false. if is a Java reserved word If the condition is true, the statement is executed. If it is false, the statement is skipped. The if Statement • The if statement has the following syntax: if ( condition ) statement;
Conditions/Boolean Expressions • A condition is often obtained using an equality operator and/or relational operator which create boolean expressions that return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to • Note the difference between the equality operator (==) and the assignment operator (=)
The if Statement • An example of an if statement: • First the condition is evaluated -- the value of sum is either greater than the value of MAX, or it is not • If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped. • Either way, the call to println is executed next • See Age.java (page 208) if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum);
Indentation • The statement controlled by the if statement is indented to indicate that relationship • The use of a consistent indentation style makes a program easier to read and understand • Although it makes no difference to the compiler, proper indentation is crucial to human readers "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding