Computer Programming Updated
Computer Programming Updated
Fractiona
Complete Characte
l Text
Numbers rs
Numbers
Numeric Data Types – Complete
Numbers
• Bit – A basic unit of storage 1.2 voltage = 0
1.5 voltage = 1
• Nibble – set of 4 bits, Byte-set of 8
bits
•Quantit
StorageNibbl
using 1’s
Bytes 2’s Comp +/- Rep. Characte
y e Comp r
0 0000 1111 1 0000 0 A
1 0001 1110 1111 -1 B
2 0010 1101 1110 -2 C
3 0011 1100 1101 -3 D
4 0100 1011 1100 -4 E
5 0101 1010 1011 -5 F
6 0110 1001 1010 -6 G
7 0111 1000 1001 -7 H
8 1000 0111 1000 -8 I
Primitive Data Types
• A data type is a set of values and operations defined on it
• There are number of data types in each programming
language
• For a set of numeric data types
• There is a maximum value (positive) and minimum value (negative)
• Maximum number of bits required to represent either min or max
value determines the size of data type
• Primitive
Data Data
Mintypes
value in Java Max value Size
type
Byte -128 127 8-bit
Char Codes like ASCII
Short -32768 32767 16-bit
Int -2,147,483,648 -2,147,483,647 32-bit
long - 9,223,372,036,854,775,8 64-bit
Extended Data Types
• The data types that are built upon the primitive data types
to provide additional functionality and features
Enumeratio
Strings Arrays
ns
decision {
int a=13, b=12;
<= 7<=8
8<=8
True
True
9<=8 false
• Examples if(a>b) {
System.out.println("A is greater than
B");
> 8>5
8>9
True
false
}
public static void main(String[] args)
}
>= 7>=8
7>=7
False
True
{ 7>=8 False
int a=13,b=11;
if(a<b) { == 5==5
6==5
True
False
System.out.println("A is greater than
B"); != 6!=9
6!=6
True
False
}
else {
System.out.println("A is not greater than
B");
}
Selection Statements
public static void main(String[] args)
• These are three types of {
int day = 4;
statements switch (day) {
• If statement case 1:
System.out.println("Monday");
• If-else statement break;
• Switch statement: case 2:
System.out.println("Tuesday");
• A switch statement is used to break;
execute different blocks of case 3:
code depending on the value System.out.println("Wednesday");
of a variable break;
case 4:
• Break: this statement is used System.out.println("Thursday");
to come out of switch block break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Invalid day");
}
}
Repetitions in Java
• Repetition statements are used to repeat a
statement or a group of statements
• There are three repetition statements int i = 1;
(loops) normally used in languages while (i <= 5) {
System.out.println(i);
• While loop
i++;
• Do-while loop }
• For loop
public static void main(String[] public static void main(String[]
args) { args) {
int[] numbers = {1, 2, 3, 4, 5}; int x = 10;
int sum = 0; do {
for (int i = 0; i < numbers.length; System.out.print("value of
i++) { x : " + x);
sum += numbers[i]; x++;
} System.out.print("\n");
System.out.println("The sum is: " } while (x < 20);
+ sum); }
Functions and Methods
• A function or a method is a block of code
that performs a specific task or set of
tasks
• They are a way to organize and modularize public static double calculateArea(double
code, making it easier to read, understand, radius) {
and maintain double area = Math.PI * radius *
• Function: is a standalone block of code radius;
that can be called from anywhere in a return area;
}
public void setName(String name) {
program.
• It takes some input, performs a set of this.name = name;
operations, and returns some output }
• Method: It is a function that is
associated with an object or a class.
• It performs a specific task or set of tasks on
the object or class, and can modify the
object's state or return some output
Linear Data Structures
Encapsulation
Inheritance
It means that data and behavior are
bundled together in a class, and It is a way to create a new class
access to the data is controlled by based on an existing one, inheriting
methods or functions defined in the the properties and behavior of the
class. parent class.
Polymorphism
It refers to the ability of
objects of different classes to
be used interchangeably.