Program Coding
Program Coding
task.
The process of writing a program is called program coding or programming.
All the programs running on a computer are written by using different types of
programming languages.
POP is the oldest form of programming language. The codes in the POP generally carry
sequences of procedural steps that will be performed during the program's running.
Examples of Procedural-Oriented programming are C, BASIC, PASCAL, COBOL, FORTRAN,
etc.
Assembly language
Objects in Java
If we consider the real-world, we can find many objects around us, cars, dogs, humans, etc.
All these objects have a state and a behavior.
If we consider a dog, then its state is - name, breed, color, and the behavior is - barking,
wagging the tail, running.
If you compare the software object with a real-world object, they have very similar
characteristics.
Software objects also have a state and a behavior. A software object's state is stored in
fields and behavior is shown via methods.
So in software development, methods operate on the internal state of an object and the
object-to-object communication is done via methods.
Classes in Java
A class is a blueprint from which individual objects are created.
Following is a sample of a class.
Class: Car – No of Wheel, Colour, Speed, SafetyFeatures (Property/data/state)
Class: behaviour – Running(), turning(),
Example
public class Dog {
String breed;
int age;
String color;
void barking() {
void hungry() {
void sleeping() {
}
}
TOKENS in JAVA
For example, consider the following code.
In the above code snippet, public, class, Demo, {, static, void, main, (, String, args, [, ], ),
System, ., out, println, javatpoint, etc. are the Java tokens.
Types of Tokens
Java token includes the following:
o Identifiers
o Keywords
o Literals
o Operators
o Separators
o Comments
Identifier: Identifiers are used to name a variable, constant, function/method, class, and
array. It usually defined by the user. It uses letters, underscores, or a dollar sign as the first
character.
Remember that the identifier name must be different from the reserved keywords.
o The first letter of an identifier must be a letter, underscore or a dollar sign. It cannot
start with digits but may contain digits.
o The whitespace cannot be included in the identifier.
o Identifiers are case sensitive.
o No special symbols like !, @, #, % etc can be used
1. PhoneNumber
2. PRICE
3. radius
4. a
5. a1
6. _phonenumber
7. $circumference
8. jagged_array
9. 12radius //invalid
Keywords: These are the pre-defined reserved words of any programming language.
Each keyword has a special meaning to the JAVA compiler. It is always written in lower
case. Java provides the following keywords:
Literals: In programming, literal is a notation that represents a fixed value (constant) in the
source code. It is defined by the programmer. Java provides five types of literals are as
follows:
o Integer
o Floating Point
o Character
o String
o Boolean
Literal Type
23 int
9.86 double
Operators: In programming, operators are the special symbol that tells the compiler to
perform a special operation. Java provides different types of operators that can be
classified according to the functionality they provide. There are five types of operators in
Java, are as follows:
o Arithmetic Operators
o Assignment Operators
o Relational Operators
o Unary Operators
o Logical Operators
Operator Symbols
Arithmetic +,-,/,*,%
Unary ++ , - - , !
Assignment = , += , -= , *= , /= , %=
Logical && , ||
Separators: The separators in Java is also known as punctuators. There are nine separators
in Java, are as follows:
separator ; , . ( ) { } [ ]
o Square Brackets []: It is used to define array elements. A pair of square brackets
represents the single-dimensional array, two pairs of square brackets represent the
two-dimensional array.
o Parentheses (): It is used to call the functions and parsing the parameters.
o Curly Braces {}: The curly braces denote the starting and ending of a code block.
o Comma (,): It is used to separate two values, statements, and parameters.
o Semicolon (;): It is the symbol that can be found at end of the statements. It
separates the two statements.
o Period (.): It separates the package name form the sub-packages and class. It also
separates a variable or method from a reference variable.
Comments: Comments allow us to specify additional information about the Java code for
understanding the code better. Java compiler do not execute comments. Java provides the
following two types of comments:
Variable in Java is a data container that stores the data values during Java program
execution. Variable is a memory location name of the data.
Every variable is assigned data type which designates the type and quantity of value it can
hold.
Variable Declaration:
To declare a variable, you must specify the data type & give the variable a unique name.
float pi;
double d;
char a;
String name;
Variable Initialization:
To initialize a variable, you must assign it a valid value.
do =20.22;
a=’v’;
name = “Sajal”;
Example :
int a=2,b=4,c=6;
float pi=3.14f;
double do=20.22;
char a=’v’;
1. Primitive Data Types :- which include integer, character, boolean, and float
2. Non-primitive Data Types :- which include classes, arrays and interfaces.
Data Type Default size Min Value Max Value
Byte 1 byte -128 127
Short 2 bytes -32768 32676
Int 4 bytes -2147483648 2147483647
Long 8 bytes
Float 4 bytes
Double 8 bytes
Boolean 1 bit 0 1
Char 2 bytes
e = 'K'; //65536 chan]racters; code against each character is called ASCII - Americal
Standard Code for Information Interchange
f = 1.2f;
g = 1.2;
h = false;
name = "Sajal";
System.out.println(-128+7.8); // -120.2
System.out.println(23.5+7.8); // 31.3
System.out.println('g'+7.8); // 103 + 7.8= 110.8
System.out.println("Hello World !"+ 7.8); //Hello World7.8
System.out.println(false);
In such cases, you have to explicitly specify the type cast operator. This process is known
as Type Casting.
In case, you do not specify a type cast operator; the compiler gives an error. Since this rule
is enforced by the compiler, it makes the programmer aware that the conversion he is
about to do may cause some loss in data and prevents accidental losses.
Exercise
A. Tick the correct option
1. Which of these is not a feature of the Java language?
a) it is a platform-dependent language
2. Which of the following symbols are used to write comments in Java?
d) all of these
3. Which of these is used to store values in memory?
a) variables
4. Which of the following is the developer of Java?
b) James Gosling
5. Which of the following variables is invalid?
c) Priya $Pay
B. True/False
1. In Java, keywords are reserved words. T
2. Java code is converted into bytecode by its compiler. T
3. The values on which an operator works are called operands. T
4. IDE stands for Integrated Document Environment. F
5. The = operator is used to check the equality between two values. F
C. Matching
1. == c. Equality operator
2. = a. Assignment operator
3. || d. Logical OR operator
4. ++ b. Unary operator
2. What are logical operators? Write the names of any two logical operators.
Logical operators are used to combine multiple conditions and evaluate them.
They return a Boolean true or false as result.
e.g,
&& (AND) -> (a>4 && b<5)
|| (OR) -> (a>4 || b<5)
! (NOT) -> !(a>4)
3. What is the use of unary operators? How many unary operators are there in Java?
Unary operators are special operators that require only one operand or value to
perform operations.
Java provides only two unary operators, which are increment and decrement.
++ -> b = a++;
‘-- -> b = a--;
b = a++ + ++a;
b = a++ + a++;
b = ++a + a++;
b = ++a + ++a;
1. WAP a program to accept 3 sides of a triangle as argument and find the area.
Class area {
void display(double a, double b, double c) {
double s, t, area;
s = (a+b+c)/2;
t = s*(s-a)*(s-b)*(s-c);
area = Math.sqrt(t);
System.out.println("The area is ="+area);
}}
Variable list
Name Type Description
a Double First side of the triangle
b Double Second side of the triangle
c Double Third side of the triangle
s Double Stores half of the perimeters
t Double Temporary storage for intermediate calculation
area Double Stores the area of the triangle
3. WAP to accept the price of 3 items, and find the total bill amount if 18% GST is
applied.
Class bill {
Void display(double a, double b, double c){
double total, final_bill;
total = a+b+c;
final_bill = total + total * 0.18;
System.out.println(“The final bill amount = ”+final_bill);
}
}
Variable list
Name Type Description
a Double Price of first item
b Double Price of Second item
c Double Price of third item
Total Double Stores the sum of price of 3 items
final_bill Double Stores final bill amount including GST