Java (College File)
Java (College File)
Java (College File)
1. What is java?
Ans. Java is a class-based, object-oriented programming language that is
designed to have as few implementation dependencies as possible.
2. What are the features in Java?
Ans. The main features are
Object Oriented
• • Platform Independent
• • Simple
• • Secure
• • Architecture-neutral
• • Portable
• • Robust
• • Multithreaded
• • Interpreted
• • High Performance
• • Distributed
• • Dynamic
System.out.print("\n");
}
}
Output:
Input a Decimal Number :
69
Binary number is: 1000101
12.Write a program to calculate electricity bill according to the following
charges (Units given by user): a. 0 to 100 units -------------2Rs/Unit
Also add fixed charges of 200Rs
import java.util.Scanner;
public class ElectricityBill
{
private static Scanner sc;
public static void main(String[] args)
{
int Units;
double X,Amount,Total_Amount;
sc = new Scanner(System.in);
System.out.print("Please Enter the Consumed Units: ");
Units = sc.nextInt();
if (Units <=100)
{
Amount = Units * 2;
}
else if (Units <= 150)
{
Amount = 300 + ((Units - 100 ) * 3);
}
else
{
Amount = 1050+ ((Units - 150 ) * 7);
}
X = Amount;
Total_Amount = Amount + 200;
System.out.println("\nElectricity Charges = " + X );
System.out.println("Fixed Charge = 200");
System.out.println("Total Electricity Bill = " + Total_Amount);
}
}
Output:
Please Enter the Consumed Units: 625
Electricity Charges = 4375.0
Fixed Charge = 200
Total Electricity Bill = 4575.0
Experiment 1
Aim: Write a program to implement Stack and Queue using Array.
Stack
import java.io.*;
class Stack
{
static int max=10,i,top,ch,item;
static int a[]=new int[10];
StackArray()
{
top=-1;
}
public static void main(String args[])throws IOException
{
while((boolean)true)
{
System.out.println("enter 1.Push 2.Pop 3.Display 4.Exit");
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception e) { }
if(ch==4)
break;
else
{
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
}
}
}
}
static void push()
{
if(top==max)
System.out.println("stack is full");
else
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the element:");
item=Integer.parseInt(br.readLine());
a[++top]=item;
}
catch(Exception e) { }
}
static void pop()
{
if(top==-1)
System.out.println("stack is empty");
else
top--;
System.out.println("poped item:"+a[top]);
}
static void display()
{
System.out.println("elements in stack are:");
for(i=top; i>0; i--)
System.out.println(a[i]);
}
}
Output:
enter 1.Push 2.Pop 3.Display 4.Exit
1
enter the element:
5
enter 1.Push 2.Pop 3.Display 4.Exit
1
enter the element:
6
enter 1.Push 2.Pop 3.Display 4.Exit
1
enter the element:
7
enter 1.Push 2.Pop 3.Display 4.Exit
2
poped item:7
enter 1.Push 2.Pop 3.Display 4.Exit
3
elements in stack are:
6
5
enter 1.Push 2.Pop 3.Display 4.Exit
4
Queue
import java.io.*;
class Queue
{
static int i,front,rear,item,max=5,ch;
static int a[]=new int[5];
QueueArr()
{
front=-1;
rear=-1;
}
public static void main(String args[])throws IOException
{
while((boolean)true)
{
try
{
System.out.println("Select Option 1.insert 2.delete 3.display 4.Exit");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
ch=Integer.parseInt(br.readLine());
}
catch(Exception e)
{}
if(ch==4)
break;
else
{
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
}
}
}
}
static void insert()
{
if(rear>=max)
{
System.out.println("Queue is Full");
}
else
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Element: ");
item=Integer.parseInt(br.readLine());
}
catch(Exception e)
{}
rear=rear+1;
a[rear]=item;
}
}
static void delete()
{
if(front==-1)
{
System.out.println("Queue is Empty");
}
else
{
front=front+1;
item=a[front];
System.out.println("Deleted Item: "+item);
}
}
static void display()
{
System.out.println("Elements in the Queue are:");
for(int i=front+1; i<=rear; i++)
{
System.out.println(a[i]);
}
}
}
Output:
Select Option 1.insert 2.delete 3.display 4.Exit
1
Enter the Element:
5
Select Option 1.insert 2.delete 3.display 4.Exit
1
Enter the Element:
6
Select Option 1.insert 2.delete 3.display 4.Exit
1
Enter the Element:
7
Select Option 1.insert 2.delete 3.display 4.Exit
2
Deleted Item: 5
Select Option 1.insert 2.delete 3.display 4.Exit
3
Elements in the Queue are:
6
7
Select Option 1.insert 2.delete 3.display 4.Exit
4
Experiment 2
Aim : Write a Java Program with default and paramaterized constructor.
public class Vehicle1 {
int passengers;
int fuelCapacity;
int mileage;
Vehicle1() {
System.out.println("This is Default constructor");
}
Vehicle1(int passengers, int fuelCapacity, int mileage) {
System.out.println(" This is Parameterized constructor");
this.passengers = passengers;
this.fuelCapacity = fuelCapacity;
this.mileage = mileage;
}
void fuelConsumption() {
int fc = (fuelCapacity * 100) / mileage;
System.out.println("Fuel consumption : " + fc + " litre ");
}
public static void main(String args[]) {
Vehicle1 v = new Vehicle1(8, 60, 14);
Vehicle1 v1 = new Vehicle1();
v.fuelConsumption();
}
}
Output:
This is Parameterized constructor
This is Default constructor
Fuel consumption : 428 litre
Experiment 3
Inheritance
Aim: Write a Java Program and create a class Shape which has width,
height, radius and create three classes Rectangle, Square and Circle and
shows Inheritance Properties among them.
import java.util.*;
import java.util.*;
class Shape{
int width,height,radius;
Shape()
{
}
Shape(int p)
{
}
Shape(int c,int b)
{
}
}
class Rectangle extends Shape{
Rectangle(int width, int height)
{
super(width,height);
this.width = width;
this.height = height;
}
void area()
{
int a = width*height;
System.out.println("Area of Rectangle is :"+a);
}
}
class Square extends Shape{
Square(int width)
{
super(width);
this.width = width;
}
void area()
{
int a = width*width;
System.out.println("Area of Square is :"+a);
}
}
class Circle extends Shape{
Circle(int radius)
{
super(radius);
this.radius = radius;
}
void area()
{
double a = (double) (Math.PI*radius*radius);
System.out.println("Area of Circle is :"+a);
}
}
public class exp3{
public static void main(String[] args){
Rectangle n = new Rectangle(4,5);
n.area();
Square p = new Square(6);
p.area();
Circle m = new Circle(3);
m.area();
Shape s = new Shape();
}
}
Output:
Area of Rectangle is :20
Area of Square is :36
Area of Circle is :28.274333882308138
Experiment 4
Aim : Program on String
import java.util.Scanner;
public class exp4
{
public static void main (String args[])
{
String S1 = new String ("This is string");
String S2 = new String ("\nHi");
System.out.println ("String Length :" +S1.length ());
System.out.println ("String Length :" +S2.length ());
String S3 = S1.concat(S2);
System.out.println (S3);
int index1=S1.indexOf("is");
System.out.println("Index:" +index1);
String a = new String ("123");
String b = new String ("123");
String c = new String ("124");
System.out.println (a.equals (b));
System.out.println (a.equals (c));
}
}
Output:
String Length :14
String Length :3
This is string
Hi
Index:2
true
false
Experiment 5
Aim: Program using StringBuffer Class.
public class exp5
{
public static void main(String[] args)
{
StringBuffer a = new StringBuffer("Hello");
a.append(50.55);
System.out.println("appending double = " + a);
a.append('N');
System.out.println("appending character = " + a);
a.append("java");
System.out.println("appending string = "+ a);
a.insert(11, "program5");
System.out.println("inserting string = "+ a);
a.insert(5, 60.66);
System.out.println("inserting double = "+ a);
a.reverse();
System.out.println("reverse = " + a);
}
}
Output :
appending double = Hello50.55
appending character = Hello50.55N
appending string = Hello50.55Njava
inserting string = Hello50.55Nprogram5java
inserting double = Hello60.6650.55Nprogram5java
reverse = avaj5margorpN55.0566.06olleH
Experiment 6
Aim: Program to convert int, float integer, gloat object into
string.
public class exp6
{
public static void main(String args[])
{
int a = 10;
String str_1 = Integer.toString(a);
System.out.println("\nint:"+a);
System.out.println("After int to stirng = " + str_1);
float x = 50.55f;
System.out.println("\nfloat:"+x);
String str_2 = Float.toString(x);
System.out.println("After float to stirng = " + str_2);
Float fobj = new Float(10.25);
System.out.println("\nfloat object:"+fobj);
String str_3 = fobj.toString();
System.out.println("Float converted to String as " + str_3);
}
}
Output:
int:10
After int to stirng = 10
float:50.55
After float to stirng = 50.55
float object:10.25
Float converted to String as 10.25
Experiment 7
Aim: Program to display values of the field by overriding
toString() method.
public class exp7_1
{
public static void main(String[] args)
{
student s = new student();
System.out.println(s);
}
}
class student{
int Enrollment_Number;
float cgpa;
String name;
student()
{
Enrollment_Number = 214;
name = "Naman Sharma";
cgpa = 8.0F;
}
public String toString() {
return String.format("Name:"+ name + " \n" +"Enrollment_Number:" +
Enrollment_Number + "\n"+ "CGPA:"+ cgpa );
}
};
Output:
Name:Naman Sharma
Enrollment_Number:214
CGPA:8.0
Experiment 8
Aim: Exception Handling program
import java.util.*;
import java.io.*;
public class exp8 {
public static void main(String[] args)
{
//Arithmetic Exception
try {
int num1 = 10;
int num2 = 0;
int output = num1 / num2;
System.out.println("Output" + output);
} catch (ArithmeticException e1) {
System.out.println("Arithmetic Exception :");
System.out.println("Can't divide num1 by 0");
}
//Array Index Out of Bounds
try {
int arr[] = new int[10];
arr[11] = 5;
} catch (ArrayIndexOutOfBoundsException e2) {
System.out.println("Array Index is Out of Bounds Exception");
System.out.println("\n");
}
//String Index Out of Bounds
try {
String s = "Naman";
char c = s.charAt(26);
System.out.println(c);
} catch (StringIndexOutOfBoundsException e3) {
System.out.println("String Index is Out of Bounds Exception");
System.out.println("\n");
}
//NULL Pointer Exception
try {
String s1 = null;
System.out.println(s1.length());
} catch (NullPointerException e4) {
System.out.println("This is NULL Pointer Exception");
System.out.println("\n");
}
//Number Format Exception
try {
int n = Integer.parseInt("NS");
System.out.println(n);
} catch (NumberFormatException e5) {
System.out.println(" This is NUMBER FORMAT EXCEPTION");
System.out.println("\n");
}
//File Not Found Exception
try {
File f = new File(("D://xy.txt"));
FileReader fr = new FileReader(f);
} catch (FileNotFoundException e6) {
System.out.println(" Given File (D://xy.txt) Does not exist ");
}
}
}
Output:
Arithmetic Exception :
Can't divide num1 by 0
Array Index is Out of Bounds Exception
String Index is Out of Bounds Exception
This is NULL Pointer Exception
This is NUMBER FORMAT EXCEPTION
Given File (D://xy.txt) Does not exist
Experiment 9
i)Nested Try Clause
class exp9_1
{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e)
{
System.out.println(e);
}
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("other statement");
}catch(Exception e)
{
System.out.println("handeled");
}
System.out.println("normal flow");
}
}
Output:
going to divide
java.lang.ArithmeticException: / by zero
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length
5
other statement
normal flow
ii) Multiple Catch Clause
public class exp9_2 {
public static void main(String[] args) {
try
{
int arr[] = new int[10];
arr[10] = 10/0;
}
catch (ArithmeticException e)
{
System.out.println("Arithmetic Exception Occurs First");
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index Out of Bounds Exception Occurs ");
}
}
}
Output:
Arithmetic Exception Occurs First
Experiment 10
i) Thread Class
class MyThread1 extends Thread{
public MyThread1(String name)
{
super(name);
}
public void run()
{
int i =0;
while (i<=40)
{
System.out.println(this.getName()+":"+i);
i++;
try { Thread.sleep(300);}
catch (Exception e){}
}
}
}
public class exp10_1 {
public static void main(String[] args) {
MyThread1 t1 = new MyThread1("Hello");
MyThread1 t2 = new MyThread1("Java");
t1.start();
t2.start();
}