CHAPTER IV:
JAVA LIBRARY, PACKAGES ,USER INPUTS AND
OPERATORS
IV.A LIBRARY AND PACKAGES
Note:
Java API has many pre written classes to help the programmer mange inputs, database etc
- known as Built-in packages)
What is Library and API?
What is library: refers to a collection of files, programs, routines, scripts, or functions
that can be referenced in the programming code.
A library is a package of code that has been written to do a certain task such as
finding a square root, record audio from a microphone or translate speech to text.
In real life, whenever you go to a library, you see a huge number of books and they
are divided into different sections: history, mathematics, chemistry, sociology, etc.
These books can answer any questions you may have. So there's no need to discover
the continents again
– you can simply take a book and find what you need.
And all the books are strictly organized:
on this shelf, you can find algebra books
on this shelf, geometry books
on this shelf, books dedicated to mathematical analysis
etc.
In Java, there’s a similar (virtual) library of tested code, which includes ready-made
frameworks for many problems which programmers face in their daily work. That
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
means that you can easily take a code from the library and use it. And this can save a
programmer a lot of time, because the code doesn't have to be written from scratch.
WHAT IS API?(APPLICATION PROGRAMMING INTERFACE)
Library is collection of code (functions/ methods) which programmers can use in their
own application code.
API is the Application programming Interface which acts as interface between
programmer and the library code.
API is just an interface through which methods and functions of library are exposed,
thus it is right to say API is part of library or we can say it is interface to the library.
Library as a whole is very vast collection of code, it is API that makes its
implementation easier.
The information in the Java library is divided into “packages” – kind of like shelves
in a library. And in each package you can a find tested code.
For example, there are such packages:
java.applet
java.lang – this is the main package of the Java language
java.util
java.io
java.net
etc.
Schematic illustration of the Java Library.
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
PACKAGES
What is packages? They each contain classes. You can see this concept illustrated
above in the example of the "java.lang" package. One of the class is java.lang is the
"Math" class. This class is ready-made frameworks which we can use when we write
programs.
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined
package.
Packages are divided into two categories:
Built-in Packages (packages from the Java API)
User-defined Packages (create your own packages)
BUILT-IN PACKAGES
The Java API is a library of prewritten classes, that are free to use, included in the Java
Development Environment.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Each class has its own methods (actions) which can be performed by means of class.
For example, the Math class has the following methods:
sqrt () – the derivation of a square root
sin () – the calculation of a sine of an angle
etc.
That means that if we need to calculate a square root, we don’t need to write a code
that will, step by step, calculate the square root. Such a code would take many lines
of code…so, instead, we just take the Math class from the java.lang package (which
contains the sqrt () method that calculates the square root) and we receive the
necessary solution from just one line of code.
sqrt() is a method of Math class. It returns square root of a number.
Practical working example:
import java.lang.Math;
public class Numbers {
public static void main(String[] args) {
System.out.print("Square root of 4 is: " + Math.sqrt(4));
}
}
When you run the program, the output will be:
Square root of 4 is: 2.0
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
IMPORTING PACKAGES
The library is divided into packages and classes.
Meaning you can either import a single class (along with its methods and attributes),
or a whole package that contain all the classes that belong to the specified package.
To use a class or a package from the library, you need to use the import keyword:
Syntax
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
IV.b USER INPUT (Scanner Class)
the Scanner class, Is used to get user input
is a class in the java.util package that helps programmers handle inputs from the user.
Example
import java.util.Scanner;
In the example above, java.util is a package, while Scanner is a class of
the java.util package.
GENERAL FORMAT FOR USER INPUT (BASIC)
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Practical Example
Output
To use the Scanner class, create an object of the class and use any of the available
methods found in the Scanner class documentation.
In our example, we will use the nextLine() method, which is used to read a complete
line:
The line 11 which says Scanner scan = new Scanner (System.in); known as
instantiation.
Thus System.in is calling the "in" object of InputStream class defined in System class
when Scanner is called during object creation time.
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Scanner(InputStream) is pre-defined constructor in Scanner class which when calls
requires the object of InputStream class as the parameter and that object is System.in.
USER INPUT (NON-STRING )
What cause an error?
The nextLine() is only used for String
data type.
Determine the data to be inputted and used the following based on the data type.
Practical Example
(in this example the data type is double (x), that’s why it uses nextDouble (line 14)
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
IV. C OPERATORS
JAVA OPERATORS
Operators are used to perform operations on variables and values.
Java divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
ARITHMETIC OPERATORS
Arithmetic operators are used to perform common mathematical operations.
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Practical Example
Output
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Output
Note: Integer data type display only whole number
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Modulus(remainder)
Output
Note: Modulus display only the remainder
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Increment and Decrement
Output
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
Note: Variable X increment by 1
Decrement
Output
Note: Variable x decrement by 1
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
JAVA ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables.
Practical Example
Code Output
The addition assignment operator (+=) adds a value to a variable:
Other Examples
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
JAVA COMPARISON OPERATORS
Comparison operators are used to compare two values:
Practical Example
The Equal to
Code Output
The output is false since the value of x is 5 and the value of y is 3, hence 5 is not equal
to 3
The Not Equal
The output is true since the value of x is 5 and the value of y is 3, hence 5 is not equal
to 3
Summary of Comparison Operators
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.
JAVA LOGICAL OPERATORS
Logical operators are used to determine the logic between variables or
values:
Practical Example
Code Output
Capiz State University Dayao Satellite College Computer Programming II
Computer Studies Department Charles B. Almonguera Jr.