# CSCI 2170 Lecture 11: Math Returns & Scanners

18 views1 pages
11 Oct 2019
School
Course
Professor
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
Type Casting (converting from one type to another):
(type) expression
Examples:
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);
Example:
// creates Scanner named console that reads from the input (keyboard)
Scanner console = new Scanner(System.in);
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
console.next() // 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.