Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Unit 1-Java Basics-I.pptx

Uploaded by

jithentar.cs21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 1-Java Basics-I.pptx

Uploaded by

jithentar.cs21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Course Name: Object Oriented Java Programming

Course Code: 21CS3PCOOJ


Course Faculty: Syed Akram

Class on 29/10/2022 at 09:50am


Syllabus
import java.io.*;

class Demo
{
public static void main(String args[])
{
System.out.println(“WELCOME TO JAVA”);
}
}
• Compile: javac ClassName.java
– Javac Demo.java
• EXECUTE: java ClassName
– Java Demo
Java Keywords
• Difference between Post Increment (n++) and
Pre Increment (++n)
– Post Increment (n++): It increases the value of
variable by 1 after execution of the statement.
– Pre Increment (++n): It increases the value of
variable by 1 before execution of the statement.
Prime Number Logic: Prime Number are divisible by itself only.

Not divisible by any Divisible by 2 ...no need Divisible by 3 ...no need


Number to check further to check further
7%2=1 8%2=0 9%2=1
7%3=1 8%3= 9%3=0
7%4=3 8%4= 9%4
7%5=2 8%5= 9%5
7%6=1 8%6= 9%6
8%7= 9%7
9%8
Numbers are not divisible by more than half of the number
No need to check upto 6 No need to check upto 7 No need to check upto 8
check upto 3 only check upto 4 only check upto 4 only
• Fibonacci Series ( 1 1 2 3 5 8 13...)
• Logic: Sum of previous two numbers will give
us next number.

prev next sum


shifted to prev shifted to next
1 1 2
1 2 3
2 3 5
3 5 8
5 8 13
8 13 ...
13 ... ...
prev will give you fibonacci series
• Sum of 1st 10 Natural Numbers

sum n sum
sum+n
0 1 1
1 2 3
3 3 6
6 4 10
10 5 15
15 6 21
21 7 28
28 8 36
36 9 45
45 10 55
Factorial
• Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1
prod n prod

prod*n

1 5 5

5 4 20

20 3 60

60 2 120

120 1 120
Common Mistakes
• Some common errors in the initial phase of learning
programming:
- Parentheses Mismatch Eg: (()
- If there is a missing semicolon(;)
- The way method/class is used- case sensitivity

• The best way to learn programming is writing a lot of


programs on your own.
Write Java program
1. Write a program which prints the following information about at least 5
persons:

NAME MAIL-ID EMPLOYEE-CODE PHONE

Eg. Raju raju@bms b1299 25764728

Ramesh ramesh@bms b1211 25764728

Each entry should be on a separate line.

2. Write a program that prints the following line on the screen along with
quotes.

“Can we print ‘\’ with System.out.println() statement?”


DataType
• Integer
– Byte and short values are used in an expression they are
promoted to int when the expression is evaluated.
– Hexadecimal value is written as zero-x 0x1
Program to demonstrate in how many days light will travel how many miles.

class Light {
public static void main(String args[]) {
OUTPUT:
int lightspeed; In 1000 days light will travel about
long days; 16070400000000 miles.
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
• Floating-Point Types
TO Compute Area of Circle

class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
Characters
• Java char is 16 bits type
– Range from 0-65536
– No negative chars
– The standard set of characters known as ASCII still
ranges from 0 to 127.
– The extended 8-bit character set, ISO-Latin-1,
ranges from 0 to 255
Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
OUTPUT:
ch1 and ch2: X Y
char variables behave like integers
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
OUTPUT:
} ch1 contains X
ch1 is now Y
Booleans
• It can have only two values
– true/false
OUTPUT:
// Demonstrate boolean values.
b is false
class BoolTest {
b is true
public static void main(String args[]) {
This is executed.
boolean b;
10 > 9 is true
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
if(b) System.out.println("This is executed.");
b = false;
if(b){
System.out.println("This is not executed.");
}
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) {
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1;
System.out.println("y is: " + y);
y = 100;
System.out.println("y is now: " + y);
}
OUTPUT:
} y is: -1
} y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
Predict the output
class ScopeErr {
public static void main(String args[]) {
int bar = 1;
{
int bar = 2;
}
}
}

Compile time error


Type Conversion and Casting
• If the two types are compatible, then Java will perform the conversion
automatically
• Java’s Automatic Conversions
– When one type of data is assigned to another type of variable, an automatic
type conversion will take place if the following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.
– Called as widening conversion
– Eg: byte to int
• Casting Incompatible Types
• if you want to assign an int value to a byte variable

• To create a conversion between two incompatible types,


- you must use a cast.
- A cast is simply an explicit type conversion.
- It has this general form:
(target-type) value
Eg:
int a;
byte b;
b = (byte) a;
Demonstrate casts
class Conversion {
public static void main(String args[]) {
byte b;
int i = 257;
double d = 323.142;
System.out.println("\nConversion of int to byte.");
b = (byte) i;
System.out.println("i and b " + i + " " + b);
System.out.println("\nConversion of double to int.");
i = (int) d;
System.out.println("d and i " + d + " " + i);
System.out.println("\nConversion of double to byte.");
b = (byte) d;
System.out.println("d and b " + d + " " + b);
}
}
OUTPUT:
Conversion of int to byte.
i and b 257 1
Conversion of double to int.
d and i 323.142 323
Conversion of double to byte.
d and b 323.142 67
Automatic Type Promotion in Expressions

byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;

WRONG WAY
CORRECT
byte b = 50;
byte b = 50;
b = b * 2; //ERROR
b = (byte)(b * 2);
The Type Promotion Rules
• type promotion rules that apply to expressions
– First, all byte, short, and char values are promoted to
int
– if one operand is a long, the whole expression is
promoted to long
– If one operand is a float, the entire expression is
promoted to float.
– If any of the operands is double, the result is double.
class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
238.14 + 515 - 126.3616
} result = 626.7784146484375
Arrays
• An array is a group of like-typed variables that are referred to by a common
name
• One-Dimensional Arrays
– Array declaration:
• type var-name[ ];
– Eg:
• int month_days[];
– array-var = new type[size];
– Eg:
• month_days = new int[12];
• month_days[1] = 28;
• System.out.println(month_days[3]);
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
April has 30 days
• An array initializer is a list of comma-separated
expressions surrounded by curly braces

class AutoArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}
class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}

Average is 12.299999999999999
Multidimensional Arrays
• In Java, multidimensional arrays are actually arrays
of arrays
– int twoD[][] = new int[4][5];
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++){
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++; OUTPUT:
} 01234
} 56789
for(i=0; i<4; i++) { 10 11 12 13 14
for(j=0; j<5; j++) 15 16 17 18 19
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++) {
OUTPUT:
for(j=0; j<i+1; j++) {
0
twoD[i][j] = k;
12
k++;
345
}
6789
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};

Alternative Array Declaration Syntax


type[ ] var-name;
08/11/2022
Arithmetic Operators
// Demonstrate the basic arithmetic operators.
class BasicMath {
public static void main(String args[]) {
// arithmetic using integers
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a = " + a); Integer Arithmetic
a=2
System.out.println("b = " + b); b=6
System.out.println("c = " + c); c=1
d = -1
System.out.println("d = " + d); e=1
System.out.println("e = " + e);
}
}
// arithmetic using doubles
OUTPUT:
System.out.println("\nFloating Point Integer Arithmetic
Arithmetic"); a=2
b=6
double da = 1 + 1;
c=1
double db = da * 3; d = -1
double dc = db / 4; e=1
Floating Point Arithmetic
double dd = dc - a;
da = 2.0
double de = -dd; db = 6.0
System.out.println("da = " + da); dc = 1.5
dd = -0.5
System.out.println("db = " + db);
de = 0.5
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
The Bitwise Operators
Java defines several bitwise operators that can be applied to
the integer types, long, int, short, char, and byte.
The Bitwise Logical Operators
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void main(String args[]) {
String binary[] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
int a = 3; // 0 + 2 + 1 or 0011 in binary
int b = 6; // 4 + 2 + 0 or 0110 in binary
int c = a | b;
int d = a & b; ~a is ANDed with 0x0f(0000 1111 in binary)
in order to reduce its value to less than 16,
int e = a ^ b; so that it can be printed by use of the binary
int f = (~a & b) | (a & ~b); array.
int g = ~a & 0x0f;
System.out.println(" a = " + binary[a]);
System.out.println(" b = " + binary[b]);
System.out.println(" a|b = " + binary[c]);
System.out.println(" a&b = " + binary[d]);
System.out.println(" a^b = " + binary[e]);
System.out.println("~a&b|a&~b = " + binary[f]);
System.out.println(" ~a = " + binary[g]);
}
}
OUTPUT:
a = 0011
b = 0110
a|b = 0111
a&b = 0010
a^b = 0101
~a&b|a&~b = 0101
~a = 1100
The Left Shift
• The left shift operator, <<, shifts all of the bits in a value to the
left a specified number of times.
• general form:
– value << num
– byte & short value are automatically promoted to int.
– Bits shifted left will not be lost until they shift past bit
position 31.
– a negative byte or short value will be sign-extended when
it is promoted to int.(high order bits will be filled with 1’s)
• FF which is 1111 1111 will be sign extended to
11111111111111111111111111111111
– For these reasons, to perform a left shift on a byte or short implies
that you must discard the high-order bytes of the int result
• discard the top three bytes of the result if what you want is the
result of a shifted byte
// Left shifting a byte value.
class ByteShift {
public static void main(String args[]) {
byte a = 64, b;
int i;
i = a << 2;
b = (byte) (a << 2);
System.out.println("Original value of a: " + a);
System.out.println("i and b: " + i + " " + b);
}
}
OUTPUT:
Original value of a: 64
i and b: 256 0
// Demonstrate the bitwise logical operators.
class BitLogic {
public static void main(String args[]) {
int i;
int num = 0xFFFFFFE;
for(i = 0; i<4;i++) {
num = num << 1;

System.out.println(num);
}
} 5368770908
1073741816
} 2147483632
-32
The Right Shift
• The right shift operator, >>, shifts all of the bits
in a value to the right a specified number of
times.
– general form
• value >> num
• int a = 32;
• a = a >> 2; // a now contains 8
• int a = 35;

class HexByte {
Public static void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}

OUTPUT:
b = 0xf1
The Unsigned Right Shift
• if you are shifting something that does not represent a numeric
value,
– you may not want sign extension to take place
• If you want to shift a zero into the high-order bit no matter what
its initial value was.
– This is known as an unsigned shift(>>>).
• int a = -1;
• a = a >>> 24;
11111111 11111111 11111111 11111111 –1 in binary as an int
>>>24
00000000 00000000 00000000 11111111 255 in binary as an int
// Unsigned shifting a byte value.
class ByteUShift {
static public void main(String args[]) {
char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7‘, '8', '9', 'a', 'b', 'c', 'd', 'e', 'f’ };
byte b = (byte) 0xf1;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);

System.out.println(" b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);


System.out.println(" b >> 4 = 0x” + hex[(c >> 4) & 0x0f] + hex[c & 0x0f]);
System.out.println(" b >>> 4 = 0x” + hex[(d >> 4) & 0x0f] + hex[d & 0x0f]);
System.out.println("(b & 0xff) >> 4 = 0x” + hex[(e >> 4) & 0x0f] + hex[e & 0x0f]);
}
}
Thank You

You might also like