Class 2 - 27 February To 03 March 2023
Class 2 - 27 February To 03 March 2023
Oriented
Orogramming
(OOP): JAVA
CMPG 211
Week 2
27 February 2023 to 03 March 2023
Lecturer: Dr V Malele
Office: G15
Expectations
• After engaging with the materials and activities in this study unit you
should be able to:
• The Java API is a library of prewritten classes, that are free to use, included in the Java
Development Kit or Environment (JDK) or (JDE).
• The library contains components for managing input, database programming, etc.
• The complete list can be found at Oracles website: https://docs.oracle.com/javase/8/docs/api/.
• 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
• Example: import java.util.*;
• Contains the ff classes:
• Scanner, date and time, random-number generator and other utility classes.
DIY: Built-in Packages
• To create your own package, you need to understand that Java uses a file system
directory to store them. Just like folders on your computer:
• Syntax:
• System.out.println(parameter)
• Parameters: The parameter might be anything that the user
wishes to print on the output screen.
Standard output on Screen
• In Java,
System.out.println(); you can simply use:
or
System.out.print();
System.out.printf(); to send output or
to
standard output (screen).
2. System
1. out
data.is a ispublic
a class static field: it accepts output
•1.Difference
print() between
- It println(),
prints string print()
inside the and printf()
quotes.
2. println()
similar like - It printsmethod.
print() string inside
Then the
the quotes
cursor
3. moves
printf() to the beginning
- It provides of the next line.
string formatting (similar
to printf in C/C++ programming).
DIY:
System.out.println("Number = " +
number);
• Here, we two
have the +
used "I operator to concatenate
(join)
• Here, the strings:
firstThen,
the thevalueam " and "awesome.".
variable number
evaluated.
string: "Number = ". value of
is concatenated is
to the
Java.io package
• The java.io package contains nearly every class you might ever need to
perform input and output (I/O) in Java.
• All these streams represent an input source and an output destination.
• The stream in the java.io package supports many data such as primitives
(tomorrow lesson) object, localized characters, etc.
• Sequence
Java.io package: Stream
• A stream can be defined as a
sequence of data.
• There are two kinds of Streams
• InPutStream − The InputStream is
used to read data from a source.
• Syntax:
• InputStream f = new
FileInputStream("C:/java/hello");
File f = new File("C:/java/hello");
InputStream f = new
FileInputStream(f);
• OutPutStream − The • Syntax:
• OutputStream f = new FileOutputStream("C:/java/hello")
OutputStream is used for writing
File f = new File("C:/java/hello"); OutputStream f = new
data to a destination. FileOutputStream(f);
To be discussed later
• Support for I/O
• Java provides strong but flexible support for I/O related to files and networks.
• Byte Streams
• Character Streams
• Standard Streams
• File Navigation and I/O.
• There are several other classes that we would be going through to get to know the
basics of File Navigation and I/O.
• File Class
• FileReader Class
• FileWriter Class
The Java File
and IO
Package
java.util package
• Java. util package contains the collections framework, legacy collection classes, event model, date and time facilities,
internationalization, and miscellaneous utility classes.
• Java ArrayDeque
• The Java ArrayDeque class provides resizable-array and implements the Deque interface. Following are the important
points about Array Deques −
• Array deques have no capacity restrictions so they grow as necessary to support usage.
• They are not thread-safe; in the absence of external synchronization.
• They do not support concurrent access by multiple threads.
• Null elements are prohibited in the array deques.
• They are faster than Stack and LinkedList.
• Scanner
• Scanner is a class of the java.util package.
• 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: See https://www.programiz.com/java-
programming/scanner
DIY: java.util package
• The System.in parameter is used to
take input from the standard input. It
works just like taking inputs from the
keyboard.
• We have then used the nextLine()
method of the Scanner class to read a
line of text from the user.
• Understand the different between
nextLine () and next() by inserting
your name and surname.
• Note next() reads until the next
whitespace.
Summary of Packages