JAVA PROGRAMMING
JAVA PROGRAMMING
1. What is JAVA ?
JAVA is a High level programming language.which is used to co-related with real world
scenario.
JAVA is used to create Applications which is used to resolve real-world problems.
JAVA is Syntactical Based Programming language.
JAVA is one of the Highly Secure Language.
JAVA supports OOP’s (Object Oriented Programming Language) concept.
2. Operations In JAVA ?
1. CODING
2. COMPILATION
3. EXECUTION.
1. CODING :
Writing a set of java instructions in a file is called coding.
Coding is done by the Developer.
2. COMPLILATION :
High Level Language (JAVA CODE) is converting into to Mid Level Language (Byte CODE).
Compiler is responsible for Compilation.
3. EXECUTION :
In Execution all operations are performed.
JVM (JAVA VIRTUAL MACHINE) is responsible for execution.
CTE
JAVA C
JAVA CODE(.JAVA FILE) (JAVA COMPILER)
Checks
1. JAVA INSTRUCTIONS
2. SYNTAX
CTS
1
JAVA SYNTAX :
class classname
{
: public static void main(String [] args)
: {
: :
: : MAIN METHOD (here)
: :
: }
}
Print Statements :
class classname
{
: public static void main(String [] args)
: {
: :
: System.out.println(“Statement to be Print”);
: :
: }
}
Example :
class Classname
{
public static void main(String [] args)
{
System.out.println(“Statement to be Print”);
}
}
2
DATATYPES :
1. boolean =====> True / False
VARIABLE :
Variable is memory block which is used to store Data.
Out of nine data types five are most imp there are
1. boolean
2. int
3. double
4. Char
5. String
Note :
For Datatype long, l/L should be used after the value of data, if no compiler shows error.
eg : l( l)
3
1. Write a Programme on java to save data and print it ?
class My_Data
{
public static void main (String [] args)
{
String name = “Venkat”;
String clg_name = “GEC”;
char Grade = ‘A’;
double percentage = 67.8;
long ph_number = 8328016507l;
long adhar_number = 214283804103l;
String Pan_number = “BYOPV2238G”;
System.out.println(name);
System.out.println(clg_name);
System.out.println(Grade);
System.out.println(percentage);
System.out.println(ph_number);
System.out.println(adhar_number);
System.out.println(Pan_number);
}
}
Output :
Venkat
GEC
A
67.8
832016507
214283804103
BYOPV2238G
Operators :
An Operators is a symbol which is used to perform specific operation.
Operand :
Operand is a Data/Value which which is used to perform operation.
Eg : 5 + 3 = 8 ; 8 - 3 = 5; 2 3 = 6
(In this Data (5,3,8,3,2,3 are the Operands and are the Operators )
Types of Operators :
1. Unary Operator (Operator requires one Operand to perform operation (++,--)).
2. Binary Operator (Operator requires Two Operands to perform operation (+,-,*,/,%)).
3. Ternary Operator (Operator requires more than Two Operands to perform operation (conditional
operators)).
4
Major Types of Operators :
1. Arithmetic Operator. (+ , - , * , / , %)
2. Logical Operator. (And, OR, NOT)
3. Relational Operator. (< , <= , >, >=, == , !=)
4. Assignment Operator. (=)
5. Conditional Operator. (? , : )
6. Incremental / Decrement Operator. (++ , --)
7. Combination Operator. (+=, -= , *=, /=)
1. Arithmetic Operator (+ , - , * , / , %) :
Syntax
System.out.println(10+20); //30
System.out.println(20-10); //10
System.out.println(10*2); //20
System.out.println(47/5); //9
System.out.println(47.0/5); //9.4 (To print decimal values, Operand should also be a
decimal ).
System.out.println(47%5); //2
A - Z = 65 - 90
A - z = 97 - 122
0 - 9 = 48 - 57
For Eg :
System.out.println(‘A’+20); //85 (Because A has an ASCII value of 65).
System.out.println(‘3’+20); //59 (Because ‘3’ (‘ ‘ this indicates the character) has an ASCII
value of 31).
System.out.println(‘A’+’B’); //131 (Because A and B has an ASCII value of 65 , 66).
Note :
char + char = Number
char + number = Number
Number + char = Number
char + num + String =numberString //Eg : System.out.println(‘A’+10+Venkat); Output : 75Venkat
(String has no integer values).
String+num1+num2=Stringnum1num2//Eg : System.out.println(Venkat+10+20); Output :Venkat1020
(JVM will read left to right only (+ can be add / Concatenation(merge) ).
5
3. Logical Operator (AND(&&), OR(||), NOT(!)) :
Syntax
Eg : (NOT ==> !)
System.out.println(!(3<4)); // False.
System.out.println(!(3>4)); // True.
6
1. Write a java Programme on Even or Odd ?
Eg :1:-
class Classname
{
public static void main(String [] args)
{
int a=5;
String result=(a%2==0)? “Even” : “Odd”;
System.out.println(result);
}
}
Output :
Odd
Eg :2:-
class Classname
{
public static void main(String [] args)
{
int a=2;
String result=(a%2==0)? “Even” : “Odd”;
System.out.println(result);
}
}
Output :
Even
7
Eg :2:-
class Classname
{
public static void main(String [] args)
{
int a=20;
String result=(a>=18)? “Eligible” : “Not Eligible”;
System.out.println(result);
}
}
Output :
Eligible
8
Eg For Larger number upon five numbers
Output : 222
Output : 222222
1. Pre-Increment Operator :
int a = 10;
int b = ++a // operation first next value will assign (++a => 10+1 = 11 ; a=11 and 11 will assign for b.
System.out.println(a);
System.out.println(b);
Output : a = 11 ; b=11.
2. Pre-Decrement Operator :
int a = 10;
int b = a-- // operation first next value will assign (++a => 10-1 = 9 ; a=9 and 9 will assign for b.
System.out.println(a);
System.out.println(b);
Output : a = 9 ; b=9.
9
3. Post-Increment Operator :
int a = 10;
int b = a++; // operation first next value will assign (a++ => first value of a will assign for b and then
operation will done b=10; a++ => 10+1 =a=>11
System.out.println(a);
System.out.println(b);
Output : a = 11 ; b=10.
4. Post-Decrement Operator :
int a = 10;
int b = a--; // operation first next value will assign (a-- => first value of a will assign for b and then
operation will done b=10; a-- => 10-1 =a=>9
System.out.println(a);
System.out.println(b);
Output : a = 9 ; b=10.
Eg 1 :
int a= 10;
int b = ((++a)-(--a));
System.out.println(a);
System.out.println(b);
Output : a = 10 ; b=1.
Eg 2 :
int a= 10;
a-=2;
System.out.println(a);
Output :
a=8
Eg 3 :
int a= 10;
a*=2;
System.out.println(a);
Output :
a = 20
10
Eg :
System.out.println(5+4-2*6/3%7); 5+4-12/3%7==>5+4-4%7==>5+4-1==>9-1==>8
Output:
8
1. Scanner Programming :
Scanner programme is used to take inputs from the user.
Syntax :
Import java.util Scanner;
class Classname
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
Operations done here….
}
}
11
1. int variablename = scan.nextInt();
2. double variablename = scan.nextDouble();
3. boolean variablename = scan.nextboolean();
4. char variablename = scan.nextcharAt();
5. String variablename = scan.next();
Eg 1 :
Import java.util Scanner;
class Sum_of_twoNum
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
System.out.println(“Enter the First Number :”);
int n1 =scan.next.Int();
System.out.println(“Enter the Second Number :”);
int n1 =scan.next.Int();
System.out.println(“Sum of Two Numbers is :”+ (a+b));
}
}
12
1. Control Statements :
Control Statement is used to control the flow of the java programme.
2. Looping Statement :
for
while
do while
1. if else :
Syntax
If(condition)
{
Operation
}
else
{
Operation
}
Eg :
13
Write a Java programme on Couple are Eligible for Marriage
14
Write a Java programme on Letter is Vowel
2. else if :
Syntax
If(condition)
{
Operation
}
else if (condition)
{
Operation
}
else if (condition)
{
Operation
}
else
{
Operation
}
15
Eg : Write a Java programme on Grading for the Marks Obtained ?
16
Write a Java programme on Ranking for the Grade Obtained ?
17
Write a Java programme on Fee to paid in park in given Range ?
18
3. switch :
Syntax
int variable
switch (variable)
{
case 1 :
Operation
case 2 :
Operation
Case 3 :
Operation
19
3. Looping Statements :
1. For :
Syntax :
For(variable deceleration and inclination;Condition;Increment /Decrement)
{
Operation
}
20
2. while :
Syntax :
variable deceleration and initialization;
While(condition)
{
Operation ;
Variable++;
}
Eg :
int a=1;
while(a<=10)
{
System.out.println(a);
a++;
}
Output :
1
2
3 upto 10
3. do while :
Syntax :
variable deceleration and initialization;
do
{
Operation ;
Variable++;
}While(condition)
Eg :
int a=1;
do
{
System.out.println(a);
a++;
}while(a<=10)
Eg : Write a program on printing even numbers from (10-38) using while Condition ?
21
Eg : Write a program to even even numbers in given range by user using while Condition ?
Eg : Write a program to print odd numbers in given range by user using while Condition ?
4. while :
Syntax :
variable deceleration and initialization;
do
{
Operation ;
Variable++;
}while(condition);
22
Eg : Write a program on printing even numbers from (10-38) using do_while Condition ?
Eg : Write a program on printing odd numbers in given range using do_while Condition ?
Eg : Write a program on printing odd numbers in given range using do_while Condition ?
23
Eg : Write a program to print Multiplication table by user defined Value and Limit ?
Eg : Write a program to print Exponential value of Given base value and power value ?
24
Eg : Write a program to print Factorial of given Number ?
25
Eg : Write a program to find Sum of Digits in given Number ?
26
Eg : Write a program to find Number is Strong Number or Not ?
(Sum of factorials of digits in a number is same as given number is called STRONG
NUMBER).
27
Eg : Write a program to Reverse the given number Eg (123 = 321) ?
28
Eg : Write a program to find given Number is Armstrong or Not ?
29
Eg : Write a program to find given Number is Magic Number or Not ?
30
Eg : Write a program to find Sum of N numbers ?
31