Download presentation
Presentation is loading. Please wait.
1
Using java libraries CGS3416 spring 2019
2
Some built-in functions
System.out.println(); Math.ceil(); Math.pow(a,2);
3
Function Input output Parameters Return values Defined by return type
public void functionName(variableType a, variableType b) { //internal block of code } In order to return values, changing the void to int, String, Boolean etc.
4
Java API – Application Program Interface
Library of classes, Java 8 ~ 4240 classes Developed by experts, well-tested by users Used by millions of programmers Part of JDK
5
API Code Reuse A collection of Packages Classes
Interfaces with their respective methods Fields constructors Included with the JDK Code Reuse
6
Java.util.Date != java.sql.Date
Packages Meaningful organization Security : private, public Name scoping Java.util.Date != java.sql.Date
7
Java API: Important Packages
java.lang ~ fundamental classes (primitive types, string, math functions, thread and exceptions) java.util ~ Data structures (like Linked List, Dictionary and support ; for Date / Time operations). java.io ~ Reading (from a file)&writing(to a file) java.net ~ Networking java.sql ~ Databases
8
Where – Java documentation
Java 8 api
9
3rd Party APIs
10
Accessing classes in Packages
Same package ~ direct access Different package import Fully-qualified class name ~ rare!
11
Importing Single vs Multiple Classes
Import single class Explicit import (or Single-type-Import) import java.util.Scanner; Import multiple classes Separate explicit import *import (or Import-On-Demand): imports all classes in a package import java.util.*; Explicit import or * import? Better clarity with explicit import Explicit import seems to be preferred asterisk wildcard characters
12
Fully-qualified Class name
Alternative to import java.util.Scanner input = new java.util.Scanner(System.in); Required if using java.util.Date & java.sql.Date
13
java.lang is imported by default
14
Static fields and methods
class – level java.lang.Math has only static methods For a static variable or method, we use this format: className.fieldName // fields Math.PI className.methodName(arguments) // methods Math.ceil() Math
15
Example: java.lang.Math
All fields and methods in this class are static Class Math has two fields, which are common mathematical constants. Sample usage: double area = Math.PI * radius * radius; // compute area of a circle Sample calls to static methods from Math
16
java.lang.Math Math.random() Math.abs()
Double between 0.0 & 1.0 (exclusive) 0 <= (int) (Math.random()*5)<5 Random roll dice 1<=(int)(Math.random() * 6) + 1<=6 Math.abs() Absolute value Math.abs(-240) = 240
17
java.lang.Math Math.round() Math.ceil() Math.floor()
Nearest long or int based on argument Math.round(24.8) = 25, Math.round(24.25f) – 24 (int) Math.ceil() Smallest double >= argument & equal to integer Math.ceil(20.1) = 21 Math.floor() Largest double <= argument & equal to integer Math.floor(24.8)=24
18
java.lang.Math Math.min()/max() Math.sqrt()
Positive square of a double
19
Demo Using math function
20
Instance Field and Methods
Create one or more objects from a class A class is a blueprint for building objects For a instance variable or method, we use this format: objectName.fieldName // fields objectName.methodName(arguments) // methods
21
Example: java.util.Random library
Demo
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.