0% found this document useful (0 votes)
41 views

Computer Programming 2

College Subject Notes

Uploaded by

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

Computer Programming 2

College Subject Notes

Uploaded by

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

COMPUTER PROGRAMMING 2

AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

OBJECT ORIENTED PROGRAMMING - public – method is accessible by other classes


- private – method can be accessible within the
OBJECT-ORIENTED PROGRAMMING – a programming class. It is the default access modifier.
methodology that defines object whose behaviors - protected – method can be accessible within
and interactions accomplish a given task the class and other related class (inheritance)
OBJECT – represents either a real-world object or an RETURN TYPE – it specifies whether a method returns
abstraction. It has characteristics or attributes (what a value or not.
objects possess or have), and it has states and
behavior (a piece of Java code called method). POSSIBLE RETURN TYPE:

CLASS – defines a kind of object. It is a blueprint that - Data type – method that returns a value. It is
defines the variables and methods common to all required to use the return keyword in method
objects of a certain kind. body.
- void – method that does not return any value.
INHERITANCE – enables new objects to take on the
properties of existing objects. It is used to avoid METHOD NAME – an identifier and is case sensitive.
repetition of programming instructions for each Recommended to use PascalCase.
class. It makes use of the extends keyword.
PARAMETER – is used to pass and receive data from
- BASE / SUPERCLASS / PARENT CLASS / a method.
ANCESTOR CLASS – the existing class that the
ABOUT PARAMETERS:
derived class the built upon. The class whose
properties are inherited by another class. - a method may contain no parameter
- DERIVED / SUBCLASS / CHILD CLASS / - a method may contain list of parameters
DESCENDANT CLASS – defined by adding - data type of parameters depends on
instance variables and method to an existing method’s return type.
class. The class that inherits all features from the
base class and have its own additional METHOD BODY – a set of statements that performs
features. the specific task of the method.

INTERFACE – contains the headings for a number of ENCAPSULATION – process of combining data and
public methods. It can also define public names actions into a single item. To implement
constants. It uses the keyword implements. encapsulation, declare the modifier of all fields as
private.
PACKAGE – a collection of related classes and
interfaces that have been grouped together into a CONSTRUCTOR – special method that is used to
folder. It is used with the import keyword. create and initialize an object.

FACTS ABOUT CONSTRUCTORS:


CLASSES AND METHODS
- using the new keyword calls the constructor
METHOD – collection of statements that are grouped
- it must have the same name with the class to
together to perform an operation
which it belongs to
DECLARATION OF METHOD: - it does not have a return type

access_modifier return_type method_name STATIC VARIABLES – a variable that is shared by all


(param) { objects of its class.

method body } FACTS ABOUT STATIC VARIABLES:

ACCESS MODIFIER – it determines the access level of - Static variables that are not constants should
the method from another class. be private
- These should be accessed and changed only
TYPES OF ACCESS MODIFIER: via accessor(get) and mutator (set) methods.
- Can be invoked without using any object
COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

- Invoked by using the class name instead of the class must implement, and uses the keyword
object name implements.

MATH CLASS – provides a number of standard EXCEPTION HANDLING


mathematical methods.
EXCEPTION – an event that occurs during the
execution of a program that disrupts the normal flow
of instructions.

TYPES OF EXCEPTIONS:

- CHECKED EXCEPTIONS – exceptions that occur


during compilation

- UNCHECKED EXCEPTIONS – exceptions that


occur during execution. Also known as runtime
exceptions.
FACTS ABOUT MATH CLASS:

- It does not require an import statement


- All methods in this class are static

METHOD OVERLOADING – declaring a method with


same name but different set of parameters which
determined by number, type, and order. It requires
a program to have a unique method signature.
EXCEPTION HANDLING – process used to change the
METHOD OVERRIDING – if a derived class defines a normal flow of code execution if a specified
method with the same name, the same number exception occurs.
and types of parameters, and the same return type
TRY, CATCH, AND FINALLY
as a method on the base class, the definition in the
derived class is said to override the definition in the - TRY BLOCK – block of code that might throw an
base class. exception that can be handled by a matching
catch block.
POLYMORPHISM – the ability of an object to take on
many forms

ABSTRACT CLASS – a class that cannot be


instantiated but can be subclassed.

FACTS ABOUT ABSTRACT CLASS:

- An abstract class may contain abstract - CATCH BLOCK – a segment of code that can
handle an exception that might be thrown by
methods and accessor
the try block that precedes it.
- A derived class must include actual
implementation of all inherited abstract
methods

ABSTRACT METHOD – a method that has ‘no body’


and is declared inside the abstract class only

INTERFACE – contains the headings for a number of - getMessage() – method that can be used to
public methods. It is used to specify methods that a determine Java’s message about the
exception
COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

- FINALLY BLOCK – contains statements which are - Permanent files are commonly stored in the
executed whether or not an exception is main directory or the root directory. To organize
thrown. stored files, folders or directories are used.

PATH – the complete list of the disk drive plus the


hierarchy of directories in which the file is located.

BACKSLASH (\) OR PATH DELIMITER – the special


character used to separate path components.

COMMON OPERATIONS DONE WITH A FILE:


- THROW KEYWORD – it is used to raise any type of
Exception (derived from Exception class) - Determining whether and where a path or file
manually. It sends an exception out of a block exists
or a method so it can be handled elsewhere.
- Opening a file

- Reading from a file

- Writing to a file

- Closing a file
USER-DEFINED EXCEPTIONS – created by extending
the Exception class. - Deleting a file

PATH AND FILES CLASSES

PATH CLASS – creates object that contain


information about files and directories, such as sizes,
locations, creation dates, and is used to check
whether a file or directory exists.
FILE INPUT AND OUTPUT
FILES CLASS – performs operations on files and
COMPUTER FILE – a collection of data stored on a directories, such as determining their attributes,
non-volatile device. creating input and output streams, and deleting
them.
TWO TYPES OF STORAGE DEVICES
TO USE BOTH PATH AND FILE CLASS:
- VOLATILE – temporary, values are lost when the
computer loses its power. The RANDOM ACCESS import java.nio.file.*;
MEMORY (RAM) is being used when a Java
TO CREATE A PATH:
program stores a value in a variable.
Path filePath = Paths.get(“C:\\Java\\Chapter8”);
- NON-VOLATILE – permanent, program that is
saved on a disk uses a non-volatile storage. TWO TYPES OF PATHS:
TWO CATEGORIES OF FILE: - ABSOLUTE PATH - is a complete path; it does not
require any other information to locate a file on
- TEXT FILE – consists of data that can be read in
a system.
a text editor and uses a scheme like ASCII
(American Standard Code for Information - RELATIVE PATH – depends on other path
Interchange) and Unicode. information.
- BINARY FILE – consists of data that is not PATH METHODS:
encoded as text like photo, sound, and .class
extension files. - toString() – returns the String representation of
the path, eliminating double backslashes.
- Common characteristics of a text file and binary
file are size, name, and date and time of - getFileName() – returns the file or directory
creation. denoted by this path; this is the last item in the
sequence of name elements.
COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

- getNameCount() – returns the number of name Import java.nio.file.attribute.*;


elements in the path.

- getName() – returns the name in the position of


the path specified by the integer parameter. BASICFILEATTRIBUTES METHODS:
toAbsolutePath() – to convert a relative path to an - size() – returns the size of the file in bytes
absolute path.
- creationTime() – returns the date and time the
FILE ACCESSIBILITY file was created. Format: yyyy-mm-ddThh:mm:ss
checkAccess() – checks if a file exists and if a - lastModifiedTime() – returns the date and time
program can access it. the file was last edited
TO USE CHECKACCESS: - compareTo() – compares relationship between
values retrieved from creationTime() or
import static java.nio.file.AccessMode.*; lastModifiedTime()
TO USE THE METHOD: FILE ORGANIZATION, STREAMS, AND BUFFERS
filePath.getFileSystem().provider().checkAccess(); - CHARACTER – can be any letter, number, or
CHECKACCESS() ARGUMENTS: other special symbol (such as punctuation
mark) that makes up data.
- None – checks whether the file exists
- FIELD – a group of characters that has some
- READ – checks whether the file exists and meaning.
whether the program has permission to read the
file. - RECORDS – a collection of fields that contain
data about an entity.
- WRITE – checks whether the file exists and
whether the program has permission to write the - FILE – consists of related records.
file.

- EXECUTE – checks whether the file exists and


whether the program has permission to execute
the file.

DELETING A PATH

delete() – accepts a path parameter and removes


the last element(file or directory) in a path or throws
an exception if the selection is unsuccessful. SEQUENTIAL ACCESS FILE – a data file used when
each record in a file is accessed one after another
in the order in which it was stored.

COMMA-SEPERATED VALUES - values in a record that


are separated by commas.
deleteIfExists() – method can also be used to STREAM – flow of data
remove a file without encountering an exception if
the file does not exist. INPUT STREAM – data is taken from a source (file or
keyboard) and is delivered into your program.
RETRIEVE FILE ATTRIBUTES
OUTPUT STREAM – the data is delivered from your
readAttributes() – retrieves useful information about program to a destination (file or screen).
a file.
BUFFER – a memory location into which you can write
TWO ARGUMENTS OF READATTRIBUTES(): data.
Path and BasicFileAttributes.class

TO USE READATTRIBUTES():
COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

FLUSHING – clears any bytes that have been sent to - write(byte[] b, int off, int len) – writes bytes to the
a buffer for output but have not yet been displayed output stream from the specified byte array
to a hardware device. starting at offset position off for a length of len
characters.

newOutputStream() – method is used to create a


writeable file. A Path and a StandardOpenOption
argument are passed to this method.

STANDARDOPENOPTION ARGUMENT:

- WRITE – opens the file for writing.

- APPEND – appends new data to the end of the


file; use this option with WRITE or CREATE.

- TRUNCATE_EXISTING – truncates the existing file


IO CLASSES: to 0 byte so the file contents are replaced, use
- InputStream – abstract class that contains this option with the WRITE option.
methods for performing input. - CREATE_NEW – creates a new file only if it hasn’t
- FileInputStream – provides the capability to existed yet.
read disk from files. - CREATE – opens a file if it exists or creates a new
- BufferedInputStream – handles input from a file if it hasn’t existed yet.
system’s standard or default input device. - DELETE_ON_CLOSE – delete the file when the
- OutputStream – abstract class that contains stream is closed.
methods for performing output. newInputStream() – method of the Files class is used
- FileOutputStream – provides the capability to to open a file for reading. This method accepts a
write to disk files. Path parameter and returns a stream that can read
bytes from a file.
- BufferedOutputStream – handles input from a
system’s standard or default output device. BufferedReader – object is declared to read a line of
text from a character-input stream, buffering
- PrintStream – contains methods for performing characters so reading is more efficient.
output that never throws an exception.
BUFFEREDREADER METHODS:
- Reader – abstract class for reading character
streams. - close() – closes the stream and any resources
associated with it.
- BufferedReader – reads text from a character-
input stream. - read() – reads a single character.

- BufferedWriter – writes text to a character- - read(char[] buffer, int off, int len) – reads
output stream. character into a portion of an array from
position off for len characters.
OUTPUTSTREAMS METHODS: (void)
- readLine() – reads a line of text.
- close() – closes the output streams and releases
any system resources associated with the - skip(long n) – skips the specified number of
stream. characters.

- flush() – flushes the output stream; if any bytes SEQUENTIAL DATA FILES
are buffered, they will be written. BufferedWriter – class that writes text to an output
- write(byte[] b) – writes all bytes to the output stream, buffering the characters.
stream from the specified byte array. BUFFEREDWRITER METHODS:

- close() – closes the stream, flushing it first.


COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

- flush() – flushes the stream.

- newline() – writes a line separator

- write(String s, int off, int len) – writes a String from


position off for length len.

- write(char[] array, int off, int len) – writes a


character array from position off for length len.

- write(int c) – writes a single character.

PREFINALS: ENUMERATIONS AND NESTED CLASSES

ENUMERATION (enum) – is a data type that contains


a fixed set of constants. They can be declared on
ordinal() – returns the constant’s position in the list,
their own or within another class.
the first position is 0
ENUMERATION (enum) – data type that contains
fixed set of constants

TO DECLARE ENUM:

TO ASSIGN CONSTANTS:

equals() – true if the argument is equal to the calling


object value

METHODS AND THEIR OUTPUTS:

toString() – returns the name of the calling contant


object
compareTo() – befative integer id the calling
object’s value id less than the argument, 0 if the
same, positive if greater than
COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

1. STATIC MEMBER CLASS – has access to all


static methods

2. NON-STATIC MEMBER CLASS (inner class) –


requires an instance and has access to all
data and methods of the top-class

3. LOCAL CLASS – defined within a method


body

4. ANONYMOUS CLASS – a special case of a


local class that has not identifier

valueOf() – returns an enumeration constant

values() - returns an array of enumerated constants


REGULAR EXPRESSIONS

REGULAR EXPRESSION (regex) – a character or a


sequence of characters that represent a string or
multiple strings.

The equals() method compares one (1) string to


another by comparing a single argument.

The matches() method allows you to determine


what the string should match against and allows you
to specify multiple values. This method allows much
more flexibility in your code.

TYPE-SAFE – used to describe a data type that allows


only appropriate behaviors.

NESTED CLASSES

NESTED CLASS – a class within another class


COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

USING [ ] FOR RANGE:

REPETITION OPERATOR – is any symbol in regular


expressions that indicates the number of times a
specified character appears in a matching string.

Dot (.) is used for any single character in regular


expression
COMPUTER PROGRAMMING 2
AUTHOR:
Hello! Please do not
reproduce my notes,
thank you! Goodluck,
future IT!

You might also like