Java Programming File (Mahima)
Java Programming File (Mahima)
PRACTICAL FILE
OF
JAVA PROGRAMMING
LANGUAGE
SUBMITTED TO : SUBMITTED BY :
Dr. Rajeev Puri Sir Mahima
BCA-III
10722130160
2
INDEX
S.NO. TOPIC PAGE REMARKS
NO.
1. Introduction to Java 6
2. Features of Java 8
3. Structure 10
4. Advantages 11
5. Disadvantages 11
PROGRAMS
6. WAP to print a string 14
7. WAP to calculate area of circle using 14
scanner class
8. WAP to calculate arithmetic 16
expression through scanner
9. Program to show the concept of 18
conditional operator
10. Program to show the concept of 18
logical operator
11. Program to show the concept of 20
Relational operators
13. WAP to find whether a number is 20
even or odd.
14. WAP to find greatest of three 22
numbers.
15. WAP to calculate percentage and 22
division of a student
16. WAP to use Switch Statement 24
17. WAP to print Fibonacci Series. 26
18. WAP to find Factorial of a number. 26
19. WAP to calculate Sum of first n 28
numbers .
20. WAP to print a table. 28
531
7531
97531
36. Program to use of Exception 50
handling.
37. Program with Multiple catch 50
exceptions.
38. Program to use of Stack Trace 52
39. Program to illustrate the use of final 52
keyword
40. Program to add two numbers to 54
illustrate the use of packages.
41. Program to multiply two numbers 54
to illustrate the use of packages.
42. Program to divide two numbers to 56
illustrate the use of packages.
44. Program to illustrate the use of 56
thread.
45. Program to illustrate the use of 58
constructors and methods of thread
class.
47. Program to illustrate the use of 60
IsAlive method.
49. Program to illustrate the use of 62
reading and writing files.
50. Program to illustrate the use of 64
PushBackInputStream class.
5
6
INTRODUCTION TO JAVA
Java is a class-based, object-oriented programming language
that is designed to have as few implementation dependencies
as possible. It is intended to let application developers write
once, and run anywhere (WORA), meaning that compiled Java
code can run on all platforms that support Java without the
need for recompilation. Java was first released in 1995 and is
widely used for developing applications for desktop, web, and
mobile devices. Java is known for its simplicity, robustness, and
security features, making it a popular choice for enterprise-level
applications.
FEATURES OF JAVA
o Simple : Java is very easy to learn, and its syntax is simple, clean
and easy to understand. Java has removed many complicated
and rarely-used features, for example, explicit pointers,
operator overloading, etc.
STRUCTURE
ADVANTAGES
o Java gives Automatic Garbage Collection.
o Java is Multithreaded language.
o Memory distribution.
o Java is Secure language.
o Java is a disseminated language.
o Java code platform independent.
o Java is purely Object-Oriented programming language.
DISADVANTAGES
o Memory consumption is more.
o Less machine interactive.
o Java requires huge memory space.
o Verbose and Complex codes.
o Execution of Java language is slow.
12
13
OUTPUT - 6
OUTPUT - 7
14
import java.util.*;
public class printString {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string : ");
String str = sc.nextLine();
System.out.println("Entered string is :
"+str);
}
}
import java.util.*;
public class circleArea {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the radius of the
circle : ");
float r = sc.nextFloat();
System.out.println("Area of a circle is :
"+Math.PI*r*r);
}
}
15
OUTPUT – 8
16
import java.util.*;
public class arithmeticExp
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 1st number : ");
float n1 = sc.nextFloat();
System.out.print("Enter the 2nd number : ");
float n2 = sc.nextFloat();
System.out.print("Enter the operator(+,-,*,/,%) : ");
String op = sc.next();
switch(op){
case "+":
System.out.println("Result is : "+(n1+n2));
break;
case "-":
System.out.println("Result is : "+(n1-n2));
break;
case "*":
System.out.println("Result is : "+(n1*n2));
break;
case "/":
System.out.println("Result is : "+(n1/n2));
break;
case "%":
System.out.println("Result is : "+(n1%n2));
break;
default:
System.out.println("Invalid Operator");
}
}
}
17
OUTPUT – 9
Case:1
Case:2
OUTPUT - 10
18
import java.util.*;
public class condOperator {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the your age : ");
int age = sc.nextInt();
String result = (age>=18)?"You can drive":"You
cannot drive";
System.out.println(result);
}
}
OUTPUT – 11
OUTPUT - 13
CASE-1
CASE-2
20
import java.util.*;
public class evenOdd {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = sc.nextInt();
String res = (num%2==0)?"Even":"Odd";
System.out.println("Number is "+res);
}
}
21
OUTPUT – 14
OUTPUT – 15
22
import java.util.*;
public class greatestThree {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter your marks in three
subjects : ");
float s1 = sc.nextFloat();
float s2 = sc.nextFloat();
float s3 = sc.nextFloat();
float percent = ((s1+s2+s3)/300)*100;
System.out.println("Percentage is
"+percent);
}
}
23
OUTPUT - 16
24
import java.util.*;
public class arithmeticExp
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 1st number : ");
float n1 = sc.nextFloat();
System.out.print("Enter the 2nd number : ");
float n2 = sc.nextFloat();
System.out.print("Enter the operator(+,-,*,/,%) :
");
String op = sc.next();
switch(op){
case "+":
System.out.println("Result is : "+(n1+n2));
break;
case "-":
System.out.println("Result is : "+(n1-n2));
break;
case "*":
System.out.println("Result is : "+(n1*n2));
break;
case "/":
System.out.println("Result is : "+(n1/n2));
break;
case "%":
System.out.println("Result is : "+(n1%n2));
break;
default:
System.out.println("Invalid Operator");
}
}
}
25
OUTPUT - 17
OUTPUT - 18
26
OUTPUT -19
OUTPUT – 20
28
import java.util.*;
public class sum{
public static void main(String args[]){
int sum=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the nth number : ");
int num = sc.nextInt();
for(int i=1 ; i<=num ; i++){
sum+=i;
}
System.out.println("Sum is : "+sum);
}
}
import java.util.*;
public class table {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number : ");
int num = sc.nextInt();
System.out.println("Table of "+num+" : ");
for(int i=1 ; i<=10 ; i++){
System.out.println(num+" X "+i+" = "+(num*i));
}
}
}
29
OUTPUT - 21
30
import java.util.*;
class rectangle{
int l,b;
rectangle(int n1,int n2){
l=n1;
b=n2;
}
int calcArea(){
return l*b;
}
}
OUTPUT - 22
OUTPUT - 23
32
OUTPUT - 24
34
class rectangle{
int l,b;
rectangle(int n1,int n2){
this.l=n1;
this.b=n2;
}
rectangle(rectangle rect1){
l=rect1.l;
b=rect1.b;
}
}
public class constructor {
public static void main(String args[]){
rectangle rect1 = new rectangle(10,7);
rectangle rect2 = new
rectangle(rect1);
System.out.println("Length and breadth of
rectangle-1 : ");
System.out.println("Length =
"+rect1.l+"\nBreadth = "+rect1.b);
System.out.println("Length and breadth of
rectangle-2 (Copy Constructor) : ");
System.out.println("Length =
"+rect2.l+"\nBreadth = "+rect2.b);
}
}
35
OUTPUT – 25
36
class box{
float l,b,h;
//for cube
box(float side){
l=b=h=side;
}
//when no side is given
box(){
l=b=h=0;
}
//all three sides are diffferent
box(float length,float breadth,float height){
l=length;
b=breadth;
h=height;
}
//display vol
float volume(){
return l*b*h;
}
}
public class constructor {
public static void main(String args[]){
box b1 = new box();
box b2 = new box(5);
box b3 = new box(2,3,4);
System.out.println("Volume of b1 : "+b1.volume());
System.out.println("Volume of b2 : "+b2.volume());
System.out.println("Volume of b3 : "+b3.volume());
}
}
37
OUTPUT - 26
OUTPUT - 27
38
OUTPUT - 28
40
class Employee{
int salary=80000;
}
class Engineer extends Employee{
int bonus=20000;
int totalSal(){
return salary+bonus;
}
}
class Manager extends Employee{
int bonus=10000;
int totalSal(){
return salary+bonus;
}
}
class Data_Analyst extends Employee{
int bonus=15000;
int totalSal(){
return salary+bonus;
}
}
public class inheritance{
public static void main(String args[]){
Engineer e1 = new Engineer();
Manager m1 = new Manager();
Data_Analyst da1 = new Data_Analyst();
System.out.println("Engineer's total salary :
"+e1.totalSal());
System.out.println("Manager's total salary :
"+m1.totalSal());
System.out.println("Data_Analyst's total salary :
"+da1.totalSal());
}
}
41
OUTPUT - 29
OUTPUT - 30
42
OUTPUT - 31
OUTPUT - 32
44
OUTPUT - 33
OUTPUT - 34
46
OUTPUT - 35
48
for(int j=1;j<=i;j++){
System.out.print(j);
}
for(int k=i-1;k>0;k--){
System.out.print(k);
}
System.out.println();
}
}
}
OUTPUT - 36
OUTPUT - 37
50
OUTPUT - 38
OUTPUT - 39
52
import java.io.*;
class main {
public static void main(String[] args){
int a[] = { 1, 2, 3 };
try {
System.out.println(a[5]);
}catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
}
OUTPUT - 40
OUTPUT - 41
54
import java.util.*;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers : ");
float a=sc.nextFloat();
float b=sc.nextFloat();
float result=a+b;
System.out.println("Sum : "+result);
}
}
import java.util.*;
public class main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers : ");
float a=sc.nextFloat();
float b=sc.nextFloat();
float result=a*b;
System.out.println("Product : "+result);
}
}
55
OUTPUT - 42
OUTPUT - 44
56
OUTPUT - 46
58
import java.lang.Thread;
public class main {
public static void main(String[] args) {
Thread t1 = new Thread();
Thread t2 = new Thread("My Thread");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("I am a runnable
thread!");
}
});
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("All the threads are
finished!");
}
}
59
OUTPUT - 47
60
OUTPUT - 49
62
import java.io.*;
public class main{
public static void main(String[] args) throws
IOException{
String str = "File Handling in Java using "+"
FileWriter and FileReader";
FileWriter fw=new FileWriter("output.txt");
for (int i = 0; i < str.length(); i++)
fw.write(str.charAt(i));
System.out.println("Written successfully");
fw.close();
}
}
63
OUTPUT – 50
64
import java.io.*;
public class main {
public static void main(String[] args)throws
Exception{
String srg = "1##2#34###12";
byte ary[] = srg.getBytes();
ByteArrayInputStream array = new
ByteArrayInputStream(ary);
PushbackInputStream push = new
PushbackInputStream(array);
int i;
while( (i = push.read())!= -1) {
if(i == '#') {
int j;
if( (j = push.read()) == '#'){
System.out.print("**");
}
else{
push.unread(j);
System.out.print((char)i);
}
}
else{
System.out.print((char)i);
}
}
}
}