Uploaded by S MK

335-1592494362219-HND DSA W1 Introduction To Java

advertisement
Unit 1 –Data Structures & Algorithm
1.1 Introduction to java
1
Topics To be covered
 Class
 Objects
 Access Modifier
 Comments
 Data Types
 Operators
2
Class
 It is a basic concept of oops which revolve around the real time entities.
 Classes are Blueprint or set of instructions to build a specific type of
object.
 Syntax
Class <classname>{
<fields>
<methods>
}
3
Object
 Object is an instance of a class.
 It is a self contained component which consists of methods and
properties .
 Syntax
Classname refvariable = new Classname();
4
Access Modifiers.
 It is used to control the access level for classes, methods, attributes and
constructors.
 3 Important Access Modifiers
1.public
: It can be accessible by any classes.
2.private
: It can be accessible within the declared classes.
3.protected : It can be accessible in the same package and
subclasses.
5
Comments in Java
 Comments are statements that are not
executed by the compiler or interpreter.
 Mainly 2 types
I. Single Line comment ( //)
II. Multi Line Comment (/* */)
6
Data Types
 It specify the sizes and values that can be stored in a variable.
7
Class Structure
public class Dog {
//attributes
String name;
String color;
int age;
float weight;
//methods
public void barking(){};
public void eating(){};
p.s.v.m(){};
}
8
Program to display users name
on screen.
Public class A {
public static void main(String[] args){
//this is a comment.
/* this is a
multi line comment */
System.out.println(“Praveen”);
}
}
9
Operators
 Operators are special symbols that carry out operations on operands.
 Important Operators
1. Arithmetic Operator (+,-,*,/,%)
2. Relational Operator (==,!=,>,<,>=,<=)
3. Logical Operator ( || ,&&)
4. Assignment Operator ( = )
10
Program to demonstrate
Arithmetic Operators
class Aoperator{
public static void main(String args[]){
int x=20;
int y=5;
System.out.println(x+y);
//25
System.out.println(x-y);
//15
System.out.println(x*y);
//100
System.out.println(x/y);
//4
System.out.println(x%y);
//0
}
}
11
Program to demonstrate
Relational Operator
class Roperator{
public static void main(String args[]){
int x=20;
int y=5;
int z=20;
System.out.println(x > y); //True
System.out.println(y > x); //False
System.out.println(x== z); //True
System.out.println(x!= z); //False
}
}
12
Program to demonstrate Logical
Operator
class Loperator{
public static void main(String args[]){
int x=20;
int y=5;
int z=20;
System.out.println(x > y && x > z); //false
System.out.println(y > x && x == z); //False
System.out.println(x== z || x < y); //True
System.out.println(x!= z || y < z); //True
}
}
13
Program to demonstrate
Assignment Operator
class Aoperator{
public static void main(String args[]){
int x, int y;
x=20;
y=x;
System.out.println(“x = “ + x); //x=20
System.out.println(“y = “ + y); //y=20
}
}
14
Download