0% found this document useful (0 votes)
12 views

JAVA PROGRAMMING

java

Uploaded by

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

JAVA PROGRAMMING

java

Uploaded by

madhusimhadri713
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

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

CPU to JVM .Class File


LOW LVL
OUTPUT LANG. MID LEVEL LANGUAGE
JRE(JAVA RUN-TIME ENVIRONMENT)

1
JAVA SYNTAX :

class classname
{
: public static void main(String [] args)
: {
: :
: : MAIN METHOD (here)
: :
: }
}

 classname should be always start as Capital letter.


 classname should not have space.
 classname should not have any symbols.

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”);

}
}

OUTPUT : Statement to be Print

2
DATATYPES :
1. boolean =====> True / False

2. byte (1byte memory) =====> Integer type Values (1,10,20,100…)


3. short (2byte memory)
4. int (4byte memory)
5. long (8byte memory)

6. float (4byte memory) =====> Decimal Type Values (1.2,4.8,3.5….)


7. double (8byte memory)

8. char =====> Single character ( value always denotes in ‘ ‘ )

9. String =====> Combinations of single character ( value


always denotes in “ “ )

VARIABLE :
Variable is memory block which is used to store Data.

Syntax : Datatype VariableName = Data Value

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)

For float also after the value f should be mentioned


eg : f( f)

Naming Rules for a Variables :


1. Variable name can be a single character or a combinations of characters.
2. Variable name should always start with Alphabets (capital/small).
3. Variable name should not start with numbers.
4. Variable name should not contain any symbols except (_ and $).
5. Variable name should not have SPACE.
6. Variable name should not contain (‘ ‘ and “ “).

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

ASCII (American Standard Code for Information Interchange)

ASCII Values : In java all character have an integer values.

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) ).

2. Relational Operator (<, >, <=, >=, ==, !=) :


Syntax
System.out.println(3<4); // True.
System.out.println(3>4); // False.
System.out.println(3<=4); // True.
System.out.println(3>=4); // False.
System.out.println(3==4); // False.
System.out.println(3!=4); // True.

5
3. Logical Operator (AND(&&), OR(||), NOT(!)) :
Syntax

Eg : (AND ==> &&)


System.out.println(3<4 && 4>5); // True && False output = False.
System.out.println(3>4 && 3<=4); // False && True = False.
System.out.println(3>=4 && 3==4); // False && False = False.
System.out.println(3!=4 && 3==3); // True && True = True.

Eg : (OR ==> ||)


System.out.println(3<4 || 4>5); // True && False output = True.
System.out.println(3>4 || 3<=4); // False && True = True.
System.out.println(3>=4 ||3==4); // False && False = False.
System.out.println(3!=4 || 3==3); // True && True = True.

Eg : (NOT ==> !)
System.out.println(!(3<4)); // False.
System.out.println(!(3>4)); // True.

4. Assigning Operator (=) :


To Assign the Specific value or data.
Eg :
class Classname
{
public static void main(String [] args)
{
int a=5;
int b=6;
a=b; // a value will become value of b.
System.out.println(a);
System.out.println(b);
}
}
Output : 6
6

5. Conditional Operator (?, :) :


Eg :
class Classname
{
public static void main(String [] args)
{
int a=5;
int b=(a<=3)?10:15; // int b=(a<=3 (True))?n1:n2 ==> ans=n1
// int b=(a>=3 (False))?n1:n2==> ans=n2
System.out.println(b);
}
}
Output : 10

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

2. Write a java Programme on Person Eligible for Vote or not ?


Eg :1:-
class Classname
{
public static void main(String [] args)
{
int a=16;
String result=(a>=18)? “Eligible” : “Not Eligible”;
System.out.println(result);
}
}
Output :
Not Eligible

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

3. Write a Java Programme on Largest Number Among Two given Numbers ?


Eg :1:-
class Classname
{
public static void main(String [] args)
{
int a=5;
Int b=55;
String result=(a>b)?a:b;
System.out.println(“Largest number is : “+result);
}
}
Output :
Largest number is : 55
Eg :2:-
class Classname
{
public static void main(String [] args)
{
int a=66;
Int b=55;
String result=(a>b)?a:b;
System.out.println(“Largest number is : “+result);
}
}
Output :
Largest number is : 66

8
Eg For Larger number upon five numbers

Output : 222

Eg For Larger number upon five numbers_2 Method

Output : 222222

6. Increment / Decrement Operator (++, --) :

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.

7. Combinational Operator (+=, -=,*=,/=,%=) :


Eg 1 :
int a= 10;
a+=2;
System.out.println(a);
Output :
a = 12

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

Note :In java mathematical operation is done by left to right


1. Brackets.
2. *,/,% (First preference)
3. +, - (First preference) .

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….
}
}

2. Syntax for Variable Scanners :

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));
}
}

Write a Java programme on Product of Three Numbers

12
1. Control Statements :
Control Statement is used to control the flow of the java programme.

1. Decision making / Conditional Statement :


 Simple if
 if else
 else if
 switch

2. Looping Statement :
 for
 while
 do while

1. if else :

Syntax
If(condition)
{
Operation
}
else
{
Operation
}

Eg :

Write a Java programme on Person Eligible for voting or not

13
Write a Java programme on Couple are Eligible for Marriage

Write a Java programme on Person Eligible for IAS Exam

Write a Java programme on Number Divisible by 5

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

Eg : Write a program using Switch Condition ?

19
3. Looping Statements :
1. For :
Syntax :
For(variable deceleration and inclination;Condition;Increment /Decrement)
{
Operation
}

Eg : Write a program to print your name 10 times ?

Write a program to print Natural Numbers from 1 to 100 ?

Write a program to print Even Natural Numbers ?

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 prime or Not-prime ?

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 ?

Eg : Write a program to print Factors of given Number ?

Eg : Write a program to count No.of Digits in given Number ?

25
Eg : Write a program to find Sum of Digits in given Number ?

Eg : Write a program to find Sum of Even Digits in given Number ?

Eg : Write a program to find Sum of factorials 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).

Eg : Write a program to find Number is Spy Number or Not ?

27
Eg : Write a program to Reverse the given number Eg (123 = 321) ?

Eg : Write a program to find Number is Palindrome or Not ?

Eg : Write a program to print 1st Ten Fibonacci Series ?

28
Eg : Write a program to find given Number is Armstrong or Not ?

Eg : Write a program to find given Number is Perfect Number or Not ?

29
Eg : Write a program to find given Number is Magic Number or Not ?

Eg : Write a program to find HCF of given Numbers ?

Eg : Write a program to find LCM of given Numbers ?

30
Eg : Write a program to find Sum of N numbers ?

Eg : Write a program to find Average of N numbers ?

Eg : Write a program to find Sum of Given Numbers ?

31

You might also like