Java Classes: Math, Integer A C S - 1903 L E C T U R E 8
Math class Math class is a utility class You cannot create an instance of Math All references to constants and methods will use the prefix Math. Contains constants, π (PI) and e (E) Java convention is to name constants using capital letters.
Math class Some methods: pow( ) Raise the first argument to the power specified in second argument e.g. Math.pow(x,3) abs( ) Returns absolute value of its argument max( ) Returns the larger of two int or double arguments min( ) Returns the smaller of two int or double arguments many more random( ) Returns a double value greater than or equal to 0.0 and less than 1.0. round( ) floor( ) ceil( ) Returns the closest int to the argument Returns the largest double value that is less than or equal to the argument and is equal to an int Returns the smallest double value that is greater than or equal to the argument and is equal to an int
Example public class FindMax The larger of 3 integers is determined. The max method is used twice. Scanner methods used: Math.max() returns the larger of two values passed in as arguments Math is a utility class with static methods. Consider the statement: Math.max(j,k) The method max The Math class Two arguments, j and k, passed in to max
Example public class FindMax { public static void main(string[] args){ Scanner kb = new Scanner(System.in); System.out.println( "Please enter 3 int values"); int i = kb.nextint(); int j = kb.nextint(); int k = kb.nextint(); int mx = Math.max(i, Math.max(j,k)); System.out.println("largest is " +mx); } } FindMax.java
Casting Casting is an operation that allows us to change the type of a value. We can take a value of one type and cast it into an equivalent value of another type. There are two forms of casting in Java: explicit casting and implicit casting Recall mixed mode expressions: widening conversions
Explicit casting Java supports an explicit casting syntax with the following form: (type) exp E.g. double d1 = 3.2; double d2 = 3.99; int i1 = (int) d1; i1 value is 3 int i2 = (int) d2; i2 value is 3 double d3 = (double) i2; d3 value is 3.0
Integer class Integer class is a utility class Many methods are static: you do not need an object of type Integer. The prefix Integer. is used for these. Contains constants, MAX_VALUE and MIN_VALUE Re: Java convention is to name constants using capital letters.
Integer class Some methods max( ) min( ) parseint( ) Returns the larger of two int arguments Returns smaller Parses the string argument expecting that argument to be a valid decimal integer. Correction to notes: E.g. parseint(" 23 ") Should be: E.g. parseint("23")
Example 1 Read lines of text from System.in Each line is parsed according to the expected format: <name of an item><comma><quantity as integer> Examples of such lines: monitor,45 laptop,55
Example public class TotalQuantity Integer methods used: parseint( ) returns the integer represented by a character string Integer is a utility class with static methods. Consider the statement: int qty = Integer.parseInt(qtyAsString); int variable The Integer class String containing an integer The method parseint
Example public class TotalQuantity { public static void main(string[] args){ Scanner kb = new Scanner(System.in); int totalqty = 0; for (int i = 0; i < 4; i++){ System.out.print("Enter next line: "); String line = kb.nextline(); int commaat = line.indexof(","); String qtyasstring = line.substring(commaat+1); int qty = Integer.parseInt(qtyAsString); totalqty += qty; } System.out.println("total = " +totalqty); } } TotalQuantity.java
Wrapper classes With similarity to the Integer class, there are classes for other types these types of classes are called wrapper classes. These are called wrapper classes because you instantiate an object and wrap a primitive value inside Double Boolean Byte Character Float Long Short