J 6,7,8,9
J 6,7,8,9
By
Dr. Bharati Mishra
Topics
• Common Mathematical Functions
• Character Data Type and Operation
• The String Type
• Case Study, Formatting Console Output
Math Class in Java
3
Java's Math class
Method name Description
Math.abs(value) absolute value
Math.ceil(value) moves up to ceiling
Math.floor(value) moves down to floor
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.rint(value) Round int, nearest whole number
Math.sqrt(value) square root
Math.sin(value) sine/cosine/tangent of
Math.cos(value) an angle in radians
Math.tan(value) Constant Description
Math.toDegrees(value) convert degrees to Math.E 2.7182818...
Math.toRadians(value) radians and back
Math.PI 3.1415926...
4
No output?
• Simply calling these methods produces no visible result.
Math.pow(3, 4); // no output
5
Calling Math methods
Math.methodName(parameters)
• Examples:
double squareRoot = Math.sqrt(121.0);
System.out.println(squareRoot); // 11.0
int absoluteValue = Math.abs(-50);
System.out.println(absoluteValue); // 50
System.out.println(Math.min(3, 7) + 2); // 5
6
Return
• return: To send out a value as the result of a method.
• The opposite of a parameter:
• Parameters send information in from the caller to the method.
• Return values send information out from a method to its caller.
• A call to the method can be used as part of an expression.
-42 Math.abs(-42)
42
main
2.71
3
Math.round(2.71)
7
Why return and not print?
8
• What is output by the following code?
double a = -1.9;
double b = 2.25;
System.out.print( Math.floor(a) +
" " + Math.ceil(b) + " " + a);
A. 3.0
B. -2.0 3.0 -2.0
C. -1.0 3.0 -1.0
D. -1 3 -1.9
E. -2.0 3.0 -1.9
9
Math questions
• Evaluate the following expressions:
Math.abs(-1.23)
Math.pow(3, 2)
Math.pow(10, -2)
Math.sqrt(121.0) - Math.sqrt(256.0)
Math.round(Math.PI) + Math.round(Math.E)
Math.ceil(6.022) + Math.floor(15.9994)
Math.abs(Math.min(-3, -5))
• Math.max and Math.min can be used to bound numbers.
Consider an int variable named age.
What statement would replace negative ages with 0?
What statement would cap the maximum age to 40?
10
Quirks of real numbers
• Some Math methods return double or other non-int
types.
int x = Math.pow(10, 3); // ERROR: incompat. types
12
Case Study-01
• In physics, the displacement of a moving body represents
its change in position over time while accelerating.
• Given initial velocity v0 in m/s, acceleration a in m/s2, and
elapsed time t in s, the displacement of the body is:
• Displacement = v0 t + ½ a t 2
• Write a method displacement that accepts v0, a, and
t and computes and returns the change in position.
• example: displacement(3.0, 4.0, 5.0) returns
65.0
13
Case Study-02
• Find the angles of a triangle, given the vertices of the triangle.
Character
• Character class
Character
• Character class
Character charObject = new Character('b');
//String is a class
String message = new String("Welcome to Java");
String s = "Java";
s = "HTML";
Trace Code
String s = "Java";
s = "HTML";
Interned String
• To improve efficiency and save memory:
• JVM uses a unique instance for string literals with
the same character sequence.
• Such an instance is called interned.
Interned String
If you use the string initializer, no new object is
created, if the interned object is already created.
String Comparison
if (s1.equals(s2))
{
// s1 and s2 have the same contents
}
if (s1 == s2)
{
// s1 and s2 have the same reference
}
String Comparison
if (s1.compareTo(s2) > 0)
{
// s1 is greater than s2
// lexicographically, i.e. unicode order
}
else if (s1.compareTo(s2) == 0)
{
// s1 and s2 have the same contents
}
else
// s1 is less than s2
String
• Length
• Retrieving individual characters
• Concatenating string
String
1. String concatenation
String s3 = s1.concat(s2);
2. String concatenation
String s3 = s1 + s2;
• String extraction
Extraction
String s1 = "Welcome".toLowerCase();
//s1 is a new String: “welcome”
String s2 = "Welcome".toUpperCase();
//s2 is a new string: “WELCOME”
String s3 = " Welcome ".trim();
//s3 is a new string: “Welcome”
String s4 = "Welcome".replace('e', 'A');
//s4 is a new string: “WAlcomA”
String s5 = "Welcome".replaceFirst("e", "AB");
//returns a new string, WABlcome.
String s6 = "Welcome".replaceAll("e", "AB");
//s6 is a new string: “WABlcomAB”
String s7 = "Welcome".replaceAll("el", "AB");
//s7 is a new string: “WABlcome”
More
• Regular expression:
"Java".matches("Java");
"Java".equals("Java");
"Java is fun".matches("Java.*");
"Java is cool".matches("Java.*");
Regular Expression
CheckPalindrome Run
StringBuilder & StringBuffer
StringBuilder
PalindromeIgnoreNonAlphanumeric Run
Command Line Arguments
More on main
class TestMain
{
public static void main(String[] args)
{
...
}
}
Command Line args
java Calculator 2 + 3
java Calculator 2 / 3
Run
java Calculator 2 “*” 3 61
Formatting Output
Formatting Output
Maximum number
A flag (such as of digits after
Tells the decimal point
compiler to - to left justify)
expect a
specifier … Minimum
number of Data type (e.g. %f)
characters to
show
Width
• Output: “ 99.3456”