Decode
Java+DSA
Chapter 1 :
Basics
1) Output
2) Variables & Operators
3) Variable naming rules
4) Comments
5) Input
6) Modulus Operator
7) Typecasting
8) Hierarchy
9) Char and ASCII
IntelliJ Idea and JDK
Basic program in Java
public class FirstProgram{
public static void main(String args[])
{ System.out.print(“Hello World”);
}
}
How to move in next line?
Example :
System.out.print(“Hello PW”);
System.out.print(“Hello CW”);
Output will be :
Hello PWHello CW
How to move in next line?
Example :
System.out.println(“Hello PW”);
System.out.print(“Hello CW”);
Output will
be : Hello
PW Hello
CW
Variables and their Declaration
Let us focus on int data type as of
now.
1) Variables as containers :
Printing Variables & Updation of Variables
int x = 5;
System.out.println(x);
x = 7;
System.out.println(x);
x = x + 6;
System.out.println(x);
x = x - 20;
System.out.println(x);
Arithmetic operations on int data type
int x = 5;
int y = 2;
System.out.println(x+y);
System.out.println(x-y);
System.out.println(x*y);
System.out.println(x/y); /
issue
Increment - Decrement operators
int x = 5;
x +;
System.out.println(x);
x -;
System.out.println(x);
+x;
System.out.println(x);
–-x;
System.out.println(x);
double & float data type
double x = 3.1;
float f = 2.87f;
Arithmetic operations on float data type
float x = 5;
float y = 2;
cout <x+y <endl;
cout <x-y <endl;
cout <x*y <endl;
cout <x/y <endl;
Example : Calculating Area of a Circle
Homework : Calculate Volume of a Sphere
Example : Calculating Simple Interest
Variable Naming rules
1) Variables can start from an alphabet or underscore _ or $ .
2) Special characters except _ and $ are not allowed.
3) Some particular keywords are not allowed.
4) Commas or blanks are not allowed.
auto double int break extern enum unsigned while
case sizeof for const static long continue float
else signed do short switch char volatile default
goto struct if union return void register typedef
Variable Naming rules - Examples
q. Which of the following are invalid variable names and why?
BASICSALARY _basic basic-hra
#MEAN group. 422
population in over time mindovermatter
2006
FLOAT hELLO queue.
team’svictory Plot#3 2015_DDay
Taking input // Square of a
Number
Take 2 numbers input from user and print their Sum
Modulus Operator
Modulus Operator
Typecasting
ques : Take integer ‘x’ as input and print half of
the number.
Hierarchy of operators
int i = 2 * 3 / 4 ;
cout <i;
int/int , int/double, double/int, double/double
char data type
char ch = ‘a’;
ASCII values
char ch = ‘a’;
MCQ Time
!
Homework
In b = 6.6 / a + 2 * n ; which operation will be performed
first?
(1) 6.6 / a
(2) a + 2
(3) 2 * n
(4) Depends upon compiler
MCQ
Which of the following statements is false
(1) Each new Java instruction has to be written on a separate line
(2) Usually all Java statements are entered in small case letters
(3) Blank spaces may be inserted between two words in a Java
statement
(4) Blank spaces cannot be inserted within a variable name
Homework
The expression, double a = 7 / 22 * ( 3.14 + 2 ) * 3 / 5 ;
evaluates to
(1) 8.28
(2) 6.28
(3) 3.14
(4) 0
MCQ
The expression int x = 4 + 2 % - 8 evaluates
to
(1) -6
(2) 6
(3) 4
(4) None of the above