CSCI 2170 Lecture 11: Math Returns & Scanners

18 views1 pages
11 Oct 2019
AP CS Name: ________________________
Notes: Math Returns & Scanners
Key Math Methods:
Math.abs(value) // absolute value
Math.ceil(value) // rounds up
Math.floor(value) // rounds down
Math.log10(value) // logarithm, base 10
Math.max(value1, value2) // larger of two values
Math.min(value1, value2) // smaller of two values
Math.pow(base, exp) // base to the exp power
Math.random() // random double between 0 and 1
Math.round(value) // nearest whole number
Math.sqrt(value) // square root
Math.sin(value) // sine of an angle in radians
Math.cos(value) // cosine of an angle in radians
Math.tan(value) // tangent of an angle in radians
Math.toDegrees(value) // convert radians to degrees
Math.toRadians(value) // convert degrees to radians
Type Casting (converting from one type to another):
(type) expression
double result = (double) 19 / 5; // 3.8
int result2 = (int) result; // 3
int x = (int) Math.pow(10, 3); // 1000
Scanner Objects (subset for console reading only, more later):
Required Library for these (placed above the Class creation):
import java.util.*; // required for Scanner Object
Scanner Creations:
Scanner name = new Scanner(source);
// creates Scanner named console that reads from the input (keyboard)
Scanner console = new Scanner(;
Scanner Methods (console is the Scanner object):
console.nextInt() // reads an int from the user and returns it
console.nextDouble() // reads a double from the user // reads a one-word String from the user
console.nextLine() // reads a one-line String from the user
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Grade+

Unlimited access to all notes and study guides.

Grade+All Inclusive
$10 USD/m
You will be charged $120 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.