KEMBAR78
LM 01 Introduction | PDF | Object Oriented Programming | Integer (Computer Science)
0% found this document useful (0 votes)
12 views9 pages

LM 01 Introduction

Uploaded by

ss
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)
12 views9 pages

LM 01 Introduction

Uploaded by

ss
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

CS 211

OBJECT-ORIENTED
Programming
LEARNING MODULE

Prepared by:
Ms. FATIMA MARIE P. AGDON
Mr. JANCYRILL L. MENDOZA
Introduction to OOP
MODULE ONE
& Java
In this module, students will be able to be introduced to Object-Oriented Programming,
the fundamentals of Java Programming such as the Java Main, recognize the different
primitive data types, know the purpose of operators, and learn how to take inputs.

Object-Oriented Programming (OOP)


Object-Oriented Programming, abbreviated as OOP, deals about creating objects
that contain both data and functions. It has several advantages over procedural
programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes
the code easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code
and shorter development time

What is Java?
Java is a popular programming language (previously named as Oak), created in 1995
owned by Oracle, and that is run in 3 billion devices. It is used for mobile applica-
tions (especially Android applications), desktop applications, web applications, web
servers and application servers, games, database connection, and much, much more.

We will write Java code in a text editor. However, it is possible to write Java in an Inte-
grated Development Environment, such as IntelliJ IDEA, Netbeans or Eclipse, which
are particularly useful when managing larger collections of Java files.

To install Java, you can check the steps here: https://www.w3schools.com/java/ ja-
va_getstarted.asp.

The Java Main


Like in C/C++, Java requires a main function. But unlike in C/C++, it has to be enclosed
in a class. For example, let us take a look in this code:
This means the keywords class, public, static, and void may seem to be unfamiliar.

And there it is, your first Java Program! The main function or in Java parlance, the
main method, accepts an array of arguments.

How to Print
To be able to print on the console, we use System.out.println to print a line on the
console. If you know C, then this is your printf or in C++, your cout. We enclose in
double quotes the literal strings we want printed on the console.

Using System.out.print will do the same thing except it will print the enclosed literal
string without the new line character. Check and run the codes below:

See the difference?

Primitive Data Types


In the code below, the first print, prints the string 49, and the second print prints the
number 49.

Like in C/C++, expressions are punctuated with semicolons (;). Since you are aware
on how to print on the console, it would be great to have an idea on how to get data
or input from the console. But before this, let us learn the different types of data in
Java.
Data Types
Java is a strongly-typed programming language, which means that you cannot put
or assign or store different types of data together. Truly, you cannot compare apples
and oranges.

Data Type Description Size

Characters, including non-


char 16 bits (0 to 65535)
printable.

boolean True or False 1 bit

byte Network packets 8 bits (-128 to 127)

Whole numbers 32 bits


int
(-2,147,483,648 to 2,147,483,647)

Smaller than int 16 bits


short
(-32,768 to 32,767)

Larger than int 64 bits


long (-9,223,372,036,854,775,808 to
9,223,372,036,854,775,807)

Numbers with floating points. 32 bits


float
(Sufficient for storing 6 to 7 decimal digits)

Larger numbers with floating 64 bits


double (Sufficient for storing 15 decimal digits)
points.

Variables
Data should be stored in some identifier or variable. It is a way for us to be able to
refer to the data whenever we need to retrieve them or manipulate them. How do we
name variables or identifiers?

Identifiers in Java are used not just for variables but for naming methods, classes,
packages, and interfaces as well. They should be composed of:
• alphanumeric characters (A-Z, a-z, 0-9)
• the dollar sign ($), and,
• the underscore (_).

They cannot begin with any of the digits but if you really want to, then you can
prepend it with the underscore (_9ball). They can be as long as you want them to
be. But I am sure you wouldn’t be naming your identifiers as long as supercalifragilis-
ticexpialidocious now would you?
Always remember that identifiers are case-sensitive and to name them as descriptive
of what they represent as possible.

Main, main, and args are identifiers you met in our previous code samples includ-
ing String, a predefined class name.

Before you can start using variables, you have to declare them. Think of it like you
are telling the Java compiler, “Hey, I want to have storage for data this big, and I am
naming it <variable_name>, so do not be a stranger, okay?”

The format in naming variables in Java is: <data_type> <variable_name>.

Suppose you want to declare more than one variable of the same type, you do
not have to separate them in different lines. All you need to do is to comma sepa-
rate the variable names like letter and digit in the example below.

Operators
Now that we are able to store data in variables, what can we do with them? How can
we manipulate them? Check the table below:

Arithmetic Operators. Arithmetic operators are being used for numeric data. The
following are the arithmetic operators in Java:

Symbol Operator Name Syntax

+ Addition a+b

- Subtraction a-b

* Multiplication a*b

/ Division a/b

% Modulo (remainder) a%b


Arithmetic Operators, cont’d.

Symbol Operator Name Syntax

Increment a++
++ (a = a + 1) or ++a

Decrement a--
-- (a = a - 1) or --a

Assignment Operators. The following are the assignment operators in Java:

Symbol Description Syntax

= Assigns 49 to a a = 49

Assigns the sum of a and 10 to a


+= (a = a + 10)
a += 10

Assigns the difference of a and 10 to a


-= (a = a - 10)
a -= 10

Assigns the product of a and 10 to a


*= (a = a * 10)
a *= 10

Assigns the quotient of a and 10 to a


/= (a = a / 10)
a /= 10

%= Assigns the remainder of a and 10 to a a %= 10

When printing variables, there is no need to enclose them in double quotes. If we


do this: System.out.println("num"); then what will be printed out would be the
string literal "num" and not the value stored in the variable num.

When we do not initialize the variables in Java, the compiler will complain
with an error telling us that the variable might not have been initialized. In this
scenario, Java forces the variables to be initialized first before they can be
used.
The sample code in the previous page also demonstrates that Java allows for declara-
tion at the same time definition. This means that a variable can be assigned an ini-
tial value while it is being declared as seen above (see int num = 0;).

Character

Characters are enclosed in single quotes. To separate string literals from values of
identifiers, use the + operator, which is also known as a concatenation operator.

Notice that the sample code below will produce an error. By adding 'Z' to the variable
letter.

Also, notice that assigning numbers to variables of type char is allowed (see letter
= 65; and digit = 53;). The reason for that is when you add a character and an integer,
the result might be an integer that has a greater value than the maximum al-
lowable value in a char data type and you might lose some information.
When you compile this code below, you should see a compilation error. Specifically
on num2.

Now, this code should be able to compile:

There should be something new in the println arguments: the \n (a newline escape
character, remember in Computer Programming?), as seen on Line 7.

Taking Inputs
Whether you use the scanf or cin from C/C++, one thing is for sure before being able
to use them. They are predefined functions from a library readily available for you. For
Java, the magic word will be import, and the packages are imported. In this case,
the java.util package is the one imported, and this is where the Scanner is found.

Packages in Java are simply a way to put together a group of related classes
and Java has quite a number of packages for your use. Check the table in the next
page for an array of all the things we can get with Scanner.
Scanner Method Description

nextInt() Reads an int value

nextFloat() Reads a float value

nextDouble() Reads a double value

nextShort() Reads a short value

nextLong() Reads a long value

nextByte() Reads a byte value

nextBoolean() Reads a boolean value

nextLine() Reads a string value

next() Reads a word (a string without spaces)

Here is an actual program that does an actual labor… well, almost.

In this code, it will ask the user to input an integer. Said integer will be the value of
the variable km. Then, the value obtained will be multiplied to 1000, and the product
will be the value of the variable m.

References
CodeChum. https://codechum.com
Programiz. https://programiz.com
W3Schools. https://www.w3schools.com/java/java_intro.asp

You might also like