Library Classes in Java
Library Classes in Java
Oriented
TUTORIAL Programming
6.0
MAIN
CONCEPTS
TO BE
COVERED
Using library classes
Reading
documentation
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
Thousands of classes.
Tens of thousands of
THE methods.
JAVA Many useful classes that
CLASS make life much easier.
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
PACKAGES
Packages:
Organized into packages like java.lang, java.util, java.io.
Key Packages:
java.lang: Basics (data types, exceptions).
java.util: Utilities (ArrayList, HashMap, Scanner).
java.io: Input/output (File, FileInputStream).
Common Functionality:
java.net: Networking.
java.awt / javax.swing: GUI.
java.time: Date/time.
Java.math: arbitrary-precision arithmetic.
java.util.concurrent: Concurrent programming.
Benefits:
Saves time, promotes code reusability
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BL
UEJ, © DAVID J. BARNES, MICHAEL KÖLLING
JAVA UTIL PACKAGE
Remember:
• we only need to know the interface, not
the implementation.
Returns:
true if the …; false otherwise
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
METHODS FROM STRING
•contains
•endsWith
•indexOf
•substring
•toUpperCase
•trim
•Beware: strings are immutable!
import java.util.Random;
...
Random rand = new Random();
...
int num = rand.nextInt();
int value = 1 + rand.nextInt(100);
int index = rand.nextInt(list.size());
FURTHER LIBRARY
CLASSES
MAIN CONCEPTS TO BE
COVERED
Further library classes:
• Set – avoiding duplicates
• Map – creating associations
Writing documentation:
• javadoc
•.
•Ordering:
•Does not maintain the order of
elements. one
•Iteration order is not two
guaranteed. three
•Use Cases:
•Efficient for membership
testing and eliminating
duplicates. OBJECTS FIRST WITH JAVA - A PRACTICAL
•Not suitable when order or
INTRODUCTION USING BLUEJ, © DAVID J. BARNES,
MICHAEL KÖLLING
USING SETS : THE
TREESET (SORTED SET)
•Definition:
•A component of the Java Collections
Framework.
•Implements the Set interface.
•Stores unique elements without a specific
order.
•Uniqueness: Set<String> sortedNames = new TreeSet<>();
sortedNames.add("Alice");
•Prohibits duplicate elements. sortedNames.add("Bob");
•Utilizes a natural ordering or a custom sortedNames.add("Charlie");
comparator.
•Ordering:
•Does not maintain the order of elements.
•Elements are stored in sorted order based on
natural order or one detailed in Comparable
Use Cases:
• Efficient for membership testing and eliminating
duplicates.
• Ideal when elements need to be sorted.
TOKENISIN
G STRINGS public HashSet<String> getInput()
{
System.out.print("> ");
String inputLine =
The code on the
reader.nextLine().trim().toLowerCase(
RHS breaks the );
user input into an
array of words and String[] wordArray =
inputLine.split(" ");
then uses the HashSet<String> words = new
HashSet to store HashSet<String>();
only unique words
for(String word : wordArray) {
words.add(word);
}
return words;
}
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING
Maps are collections that contain
pairs of objects.
Parameterized with two types.
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
USING MAPS
A map with strings as keys and values
:HashMap
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
LETTER COUNTER
CLASS DEMO
Fields:
private Map<String, Integer> letterCountMap;: This field holds a map where
keys are words (Strings) and values are the counts of unique letters in those words
(Integers).
Constructor:
public LetterCounter() { letterCountMap = new HashMap<String, Integer>(); }:
The constructor initializes the letterCountMap as a new HashMap when an instance of
LetterCounter is created.
getLetterCount Method:
public int getLetterCount(String word) { ... }:
This method takes a word as input and returns the count of unique letters in that
word.It checks if the word is already in the map (letterCountMap). If yes, it retrieves
the count from the map. If not, it calculates the count by creating a set of characters
and adding each character to the set. The size of the set represents the count of
unique letters.The calculated count is then stored in the map, and the count is
returned.
printAll Method:
public void printAll() { ... }: This method prints all entries in the map,
displaying each word along with its corresponding count of unique letters.
In summary, the LetterCounter class is designed to count and store the number of
unique letters in words. It uses a map to keep track of word counts and provides
methods to get the count for a specific word (getLetterCount) and print all word
counts (printAll).
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BL
UEJ, © DAVID J. BARNES, MICHAEL KÖLLING
COLLECTIONS AND
PRIMITIVE TYPES
The generic collection classes can be used with all
class types ...
… but what about the primitive types: int, boolean,
etc.?
Suppose we want an ArrayList of int?
unboxin
int firstMark = markList.remove(0); g
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
Documentation for a class should
include:
the class name
a comment describing the overall
purpose and characteristics of the class
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
The documentation for each constructor
and method should include:
the name of the method
the return type
the parameter names and types
ELEMENTS a description of the purpose and
OF function of the method
DOCUMENT a description of each parameter
ATION a description of the value returned
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING
JAVADOC
Class comment:
/**
* The Responder class represents a response
* generator object. It is used to generate an
* automatic response.
*
* @author Michael Kölling and David J. Barnes
* @version 1.0 (2016.02.29)
*/
OBJECTS FIRST WITH JAVA - A PRACTICAL
INTRODUCTION USING BLUEJ, © DAVID J. BARNES,
MICHAEL KÖLLING
JAVADOC
Method comment:
/**
* Read a line of text from standard input (the text
* terminal), and return it as a set of words.
*
* @param prompt A prompt to print to screen.
* @return A set of strings, where each String is
* one of the words typed by the user
*/
public HashSet<String> getInput(String prompt)
{
...
}
OBJECTS FIRST WITH JAVA - A PRACTICAL
INTRODUCTION USING BLUEJ, © DAVID J. BARNES,
MICHAEL KÖLLING
PUBLIC VS PRIVATE
Public elements are accessible to objects of other
classes:
• Fields, constructors and methods
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
CLASS VARIABLES
CONST
final int SIZE = 10;
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
CLASS CONSTANTS
static: class variable
final: constant
private static final int gravity = 3;
Public visibility is less of an issue with final fields.
Upper-case names often used for class constants:
public static final int BOILING_POINT = 100;
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL KÖLLING
CLASS METHODS
A static method belongs to its class rather than the
instances:
DS
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING
Class variables belong to their
class rather than its instances.
Class methods belong to their class
rather than its instances.
REVIEW Class variables are used to share
data among instances.
Class methods are prohibited from
accessing instance variables and
methods.
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING
The values of final variables are
fixed.
They must be assigned at
declaration or in the constructor
REVIEW (for fields).
final and static are unrelated
concepts, but they are often used
together.
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING
FURTHER ADVANCED
MATERIAL
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING
Polymorphism allows us to ignore
the more specific type in most
cases.
POLYMO We create objects of the specific
type, but ...
RPHIC … declare variables of the more
COLLEC general type:
OBJECTS FIRST WITH JAVA - A PRACTICAL INTRODUCTION USING BLUEJ, © DAVID J. BARNES, MICHAEL
KÖLLING