  Topics: Selection (conditional) statement, keyboard input using Scanner class, while loop, methods
Reading: Sec 2.2, Sec 3.1 (exclude pp104-109), Sec 3.2 (exclude pp112-114), pp 130-135 of Sec 3.3 (exclude the dowhile
loop)

Write a program to find the minimum value of the quadratic function q(x)=x2+bx+c on the interval [L, R].

/* Min value of q(x) = x^2 + bx + c on interval [L,R]
*/
public static void main (String[ ] args) {

final double b=2, c=-1.5;
double L=-3, R=5;
double qMin, qL, qR; // Min value of q, q(L), q(R)

double xc= -b/2;
if (L<=xc && xc<=R)
// qMin is q(xc)
qMin= xc*xc + b*xc + c;
else {
// qMin is q(L) or q(R)
qL= L*L + b*L + c;
qR= R*R + b*R + c;
if (qL < qR)
qMin= qL;
else
qMin= qR;
}

System .out.println("Min value is " + qMin);
}
}

User Input

We’ll use the class Scanner to read in user input from the keyboard. First, you need to import the class using the import
statement outside of the class body:

import java.util.Scanner;

Inside a method (e.g., main method), you create an object of the Scanner class. Below, we create such an object and
refer to it with the variable keyboard:

Scanner keyboard= new Scanner ( System .in);

Now we can use keyboard to read user input. Below are some example method calls. Read Sec 2.2 (Savitch) for more
information on the Scanner class.

Examples:

int var1= keyboard.nextInt ();
double var2= keyboard.nextDouble();
char var3= keyboard.nextChar();
boolean var4= keyboard.nextBoolean();

The Math class

A collection of basic mathematical functions .

double tmp = Math.exp(1);
tmp = 3*Math.sin(2);
tmp = Math.floor(3*Math.sin(2));
tmp = Math.random(); // in [0,1)

Shortcut expressions

Increment: i++;
Decrement: i--;
Assignment operators :

s += val;
s -= val;
s *= val;
s /= val;

 Conditional Statement The while loop if ( condition1 ) statement1; while ( condition) statement; if ( condition1 ) statement1; else statement2; Pattern for doing something n times if ( condition1 ) statement1; else if ( condition2 ) statement2; else statement3; int i= 1; while ( i<=n ) { // do something // increment counter i= i + 1; } Pattern for doing something an indefinite number of times % initialization while ( not stopping signal ) { // do something // update status (variables) }

Example: Factorial

Write a program fragment to calculate k ! (the factorial of k ). Assume k is given and k>=0. Use a while loop.

Method

/* = a random integer in [lo..hi]
*/
public static int randInt (int lo, int hi) {
return (int) Math.floor(
Math.random()*(hi-lo+1) + lo);
}
Method

A method is a named , parameterized group of statements
 modifiers return-type method-name ( parameter-list ) { statement-list }

return-type void means nothing is returned from the method

There must be a return statement, unless return-type is
void

Method

A method is a named, parameterized group of statements
 modifiers return-type method-name ( parameter-list ) { statement-list }

parameter-list : type-name pairs separated by commas

int randInt(int lo, int hi)

A parameter is a variable that is declared in the method

Calling a static method …

… that is in a different class :
classname.methodname(…)

Examples:

Math.random()
Math.pow(2.5,2)

… that is in the same class:
methodname(…)

Our class MyRandom has a static method
randInt, so an example method call within the
class can be
randInt(3,8)
 Prev Next

Start solving your Algebra Problems in next 5 minutes!      2Checkout.com is an authorized reseller
of goods provided by Sofmath

Attention: We are currently running a special promotional offer for Algebra-Answer.com visitors -- if you order Algebra Helper by midnight of July 6th you will pay only \$39.99 instead of our regular price of \$74.99 -- this is \$35 in savings ! In order to take advantage of this offer, you need to order by clicking on one of the buttons on the left, not through our regular order page.

If you order now you will also receive 30 minute live session from tutor.com for a 1\$!

You Will Learn Algebra Better - Guaranteed!

Just take a look how incredibly simple Algebra Helper is:

Step 1 : Enter your homework problem in an easy WYSIWYG (What you see is what you get) algebra editor: Step 2 : Let Algebra Helper solve it: Step 3 : Ask for an explanation for the steps you don't understand: Algebra Helper can solve problems in all the following areas:

• simplification of algebraic expressions (operations with polynomials (simplifying, degree, synthetic division...), exponential expressions, fractions and roots (radicals), absolute values)
• factoring and expanding expressions
• finding LCM and GCF
• (simplifying, rationalizing complex denominators...)
• solving linear, quadratic and many other equations and inequalities (including basic logarithmic and exponential equations)
• solving a system of two and three linear equations (including Cramer's rule)
• graphing curves (lines, parabolas, hyperbolas, circles, ellipses, equation and inequality solutions)
• graphing general functions
• operations with functions (composition, inverse, range, domain...)
• simplifying logarithms
• basic geometry and trigonometry (similarity, calculating trig functions, right triangle...)
• arithmetic and other pre-algebra topics (ratios, proportions, measurements...)

ORDER NOW!         