0% found this document useful (0 votes)
29 views27 pages

Class 2 - 27 February To 03 March 2023

Uploaded by

stefanvdm084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views27 pages

Class 2 - 27 February To 03 March 2023

Uploaded by

stefanvdm084
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Object-

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:

• Recap Objects and ClassesUnderstand the use of Java Keywords


• Use the Java I/O package
• Understand difference between primitive and non-primitive data types
• Use Data Types in BlueJ
Recap: Objects
• An object is an instance of a class.
• A class is a template or blueprint from which
objects are created.
• So, an object is the instance(result) of a
class.
• Object Definitions:
• An object is a real-world entity.
• An object is a runtime entity.
• The object is an entity which has state and
behavior.
• The object is an instance of a class.
Recap: Objects
• A class is a group of objects which have common
properties.
• It is a template or blueprint from which objects are
created.
• It is a logical entity.
• It can't be physical.
• A class in Java can contains: see picture
• Syntax for a class:
1. class <class_name>{
2. field;
3. method;
4. }
 Java
Keywords
 Packages
new Creates new objects
Java Keywords package Declares a package
private An access modifier used for attributes,
• Java has a set of keywords (i.e. methods and constructors, making them
reserved words that cannot be used only accessible within the declared class
as variables, methods, classes, or any protected An access modifier used for attributes,
other identifiers). methods and constructors, making them
• Note: true, false, and null are not accessible in the same package and
keywords, but they are literals and subclasses
reserved public An access modifier used for classes,
• See the list of keywords. Example attributes, methods and constructors,
• The new keyword is used to allocate
making them accessible by any other
class
memory at runtime.
• All objects get memory in Heap memory static A non-access modifier used for methods
area. and attributes. Static methods/attributes
can be accessed without creating an
object of a class
Java Packages
• A package in Java is used to group related
classes, interfaces and sub-packages
• Think of it as a folder in a file directory.
• A java package is a group of similar types of
classes, interfaces and sub-packages.
• Package in java can be categorized in two
form: • Advantage of Java Package
• Built-in Packages (packages from the Java API) 1) Java package is used to categorize
• User-defined Packages (create your own packages) the classes and interfaces so that
• We use packages to avoid name conflicts, they can be easily maintained.
and to write a better maintainable code. 2) Java package provides access
• There are many built-in packages such as: protection.
• java, lang, awt, javax, swing, net, io, util, sql etc. 3) Java package removes naming
collision.
Java Packages
• The package keyword is used
to create a package in java.
• Example:
package mypack;
public class Simple{
public static void main(String args[])
{
System.out.println("Welcome to pa
ckage");
}
}
• BlueJ users: how to make
packages; how to use javadoc
and jar
https://people.cs.ksu.edu/
~schmidt/300f04/
bluej.packages.html
Java Packages: Built-in Packages

• 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

• The ArrayList class belongs to


java.util package.
• To use it, we have to import
the package using the import
statement.
• The first line of the code
import java.util.ArrayList
• imports the java.util package
and uses ArrayList class which
is present in the sub package
util.
DIY: Built-in Packages

• In the example above, java.util is


a package, while 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:
User-defined packages

• User-defined packages are those which are developed by users in


order to group related classes, interfaces and sub packages.
• Creating a Package in Java
• Choose a name for the package and include a package command as the first
statement in the Java source file.
• The java source file can contain the classes, interfaces, enumerations, and
annotation types that you want to include in the package.
• For example, the following statement creates a package named
MyPackage.
• Syntax: package MyPackage;
• To create a class inside a package, you should declare the package
name as the first statement of your program. Then include the class as
part of the package.
DIY: User-defined 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:

• To create a package, use the package keyword:


DIY: User-
defined
Packages
DIY: User-defined Packages

• have first declared the package


Edureka, then imported the
class Compare from the
package MyPackage.
• So, the order when we are
creating a class inside a
package while importing
another package is,
• Package Declaration
• What errors do you see?
• Package Import
DIY: Static Import in Java

• Static import feature facilitates the Java programmer to access any


static member of a class directly without using the fully qualified
name.
The Java File
and IO
Package
Java.lang package

• Java System.out.println() is used to print an argument that is


passed to it. The statement can be broken into 3 parts which can
be understood separately as:
1.System: It is a final class defined in the java.lang package.
2.out: This is an instance of PrintStream type, which is a public and static
member field of the System class.
3.println(): As all instances of PrintStream class have a public method
println(), hence we can invoke the same on out as well. This is an
upgraded version of print(). It prints any argument passed to it and adds
a new line to the output. We can assume that System.out represents the
Standard Output Stream.

• 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

• In a nutshell, Java packages provide access control to the classes:


1. Anything declared public can be accessed from anywhere
2. Anything declared private can be seen only within that class
3. If access specifier is not mentioned, an element is visible to subclasses as well as to other
classes in the same package
4. Lastly, anything declared protected element can be seen outside your current package,
but only to classes that subclass your class directly
• Points to Remember
• Every class is part of some package.
• If you omit the package statement, the class names are put into the default package
• A class can have only one package statement but it can have more than one import
package statements
• The name of the package must be the same as the directory under which the file is saved
• When importing another package, package declaration must be the first statement,
followed by package import
DIY: java.util package
Practical Work: Thursday, 2 March
2023
• Practical practice:
• Use the previous slide code, to Replace
nextLine() method of the Scanner class
with methods highlighted in the table.

Check Announcements for the Practical


Exercise that need to be submitted to the
cpmg211@gmail on Thursday, 2 March
2023m 17:00

You might also like