Rana Keval Y.
(1087) 4CE2
Practical -1 Date : / /
Basic Program
1) Study of class path and java runtime environment
By Ordinary Method
1. Open Environment Variable dialog box by search box
2. Click on new button enter path as variable name and copy paste to
variable value and click on OK .
2. Write a program to
● Implement command line calculator
Page | 1
Rana Keval Y.(1087) 4CE2
package Lab1;
import [Link];
public class CommandLineCalculator
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
double num1;
double num2;
char operator;
double result = 0;
[Link]("Enter first number: ");
num1 = [Link]();
[Link]("Enter second number: ");
num2 = [Link]();
[Link]("Enter operator (+, -, *, /): ");
operator = [Link]().charAt(0);
switch(operator)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
Page | 2
Rana Keval Y.(1087) 4CE2
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
[Link]("Invalid operator");
return;
}
[Link]("Result = " + result);
}
}
Output :
● Write To prints Fibonacci series.
Page | 3
Rana Keval Y.(1087) 4CE2
Output :
Mr. Sunit Parmar
(Teacher’s Signature)
Page | 4
Rana Keval Y.(1087) 4CE2
Practical -2
Date: / /
Basic Program using Class
1. Create a class BankAccount that has Depositor name , Acc_no, Acc_type,
Balance as Data Members and void createAcc() . void Deposit(), void
withdraw() and void BalanceInquiry as Member Function. When a new
Account is created assign next serial no as account number. Account number
starts from 1
Page | 5
Rana Keval Y.(1087) 4CE2
Output :
2. Create a class time that has hour, minute and second as data
members. Create a parameterized constructor to initialize Time
Objects. Create a member Function Time Sum (Time, Time) to sum
two time objects.
Page | 6
Rana Keval Y.(1087) 4CE2
Output :
3. Define a class with the Name, Basic salary and dearness allowance
as data [Link] and print the salary + dearness allowance
and TDS rate is as per following table. Gross Salary TDS Rs. 100000
and below NIL Above Rs. 100000 10% on excess over 100000 DA is
74% of Basic Salary for all. Use appropriate member [Link],
Page | 7
Rana Keval Y.(1087) 4CE2
Basic salary(yearly), dearness allowance and tax deduced at
source(TDS) and net salary, where TDS is charged on gross salary
which is basic .
Output :
Mr. Sunit Parmar
(Teacher’s Signature)
Page | 8
Rana Keval Y.(1087) 4CE2
Practical -3
Date :
1. Create a class BankAccount that has Depositor name , Acc_no, Acc_type,
Balance as Data Members and void createAcc() . void Deposit(), void
withdraw() and void BalanceInquiry as Member Function. When a new Account
is created assign next serial no as account number. Account number starts from
Program :
package Lab3;
import [Link];
class BankAccount
{
static int nextAccNo = 1;
String depositorName;
int acc_no;
String acc_type;
double balance;
void createAcc()
{
Scanner sc = new Scanner([Link]);
[Link]("Enter Depositor Name: ");
depositorName = [Link]();
[Link]("Enter Account Type: ");
acc_type = [Link]();
[Link]("Enter Initial Balance: ");
Page | 9
Rana Keval Y.(1087) 4CE2
balance = [Link]();
acc_no = nextAccNo;
nextAccNo++;
}
void Deposit(double amount)
{
balance = balance + amount;
}
void withdraw(double amount)
{
if(amount <= balance)
balance = balance - amount;
else
[Link]("Insufficient Balance");
}
void BalanceInquiry()
{
[Link]("Account Number: " + acc_no);
[Link]("Depositor Name: " + depositorName);
[Link]("Account Type: " + acc_type);
[Link]("Balance: " + balance);
}
Page | 10
Rana Keval Y.(1087) 4CE2
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
BankAccount acc = new BankAccount();
[Link]();
[Link]("Enter amount to deposit: ");
double d = [Link]();
[Link](d);
[Link]("Enter amount to withdraw: ");
double w = [Link]();
[Link](w);
[Link]();
}
}
OUTPUT :
2. Create a class time that has hour, minute and second as data
members. Create a parameterized constructor to initialize Time
Objects. Create a member Function Time Sum (Time, Time) to sum
two time objects.
Program :
package Lab3;
class Time
Page | 11
Rana Keval Y.(1087) 4CE2
{
int hour;
int minute;
int second;
Time(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
Time TimeSum(Time t1, Time t2)
{
Time temp = new Time(0,0,0);
[Link] = [Link] + [Link];
[Link] = [Link] + [Link] + [Link] / 60;
[Link] = [Link] % 60;
[Link] = [Link] + [Link] + [Link] / 60;
[Link] = [Link] % 60;
return temp;
}
void display()
{
[Link](hour + ":" + minute + ":" + second);
}
Page | 12
Rana Keval Y.(1087) 4CE2
public static void main(String[] args)
{
Time t1 = new Time(2, 45, 50);
Time t2 = new Time(3, 20, 30);
Time t3 = new Time(0,0,0);
t3 = [Link](t1, t2);
[Link]("Sum of Time = ");
[Link]();
}
}
OUTPUT :
3. Define a class with the Name, Basic salary and dearness
allowance as data [Link] and print the Name, Basic
salary(yearly), dearness allowance and tax deduced at source(TDS)
and net salary, where TDS is charged on gross salary which is basic
salary + dearness allowance and TDS rate is as per following table.
Gross Salary TDS Rs. 100000 and below NIL Above Rs. 100000 10%
on excess over 100000 DA is 74% of Basic Salary for all. Use
appropriate member function.
Program :
Page | 13
Rana Keval Y.(1087) 4CE2
OUTPUT :
Mr. Sunit Parmar
(Teacher’s Signature)
Page | 14
Rana Keval Y.(1087) 4CE2
PRACTICAL – 4
Date: / /
1. class Cricket having data members name, age and member
methods display() and setdata(). class Match inherits Cricket and
has data members no_of_odi, no_of_test. Create an array of 5
objects of class Match. Provide all the required data through
command line and display the information.
Program :
Page | 15
Rana Keval Y.(1087) 4CE2
OUTPUT :
2. Define a class Cripher with following data Field: String plainText; int key
Functions: Cipher(String plaintext,int key) abstract String Encryption( ) abstract
Page | 16
Rana Keval Y.(1087) 4CE2
String Decryption( ) Derived two classes Substitution_Cipher and
Caesar_Cipher override Encyption() and Decyption() Method. in substitute
cipher every character of string is replace with another character. For example.
In this method you will replace the letters using the following scheme. Plain
Text: a b c d e f g h i j k l m n o p q r s t u v w x y z Cipher Text: q a z w s x e d c r f
v t g b y h n u j m i k o l p So if string consist of letter “gcet” then encrypted
string will be ”ezsj” and decrypt it to get original string In ceaser cipher encrypt
the string same as program 5 of LAB 5.
Program :
Page | 17
Rana Keval Y.(1087) 4CE2
Page | 18
Rana Keval Y.(1087) 4CE2
OUTPUT :
3. Declare an interface called Property containing a method computePrice to
compute and return the price. The interface is to be implemented by following
two classes i) Bungalow and ii) Flat. Both the classes have following data
members - name - constructionArea The class Bungalow has an additional data
member called landArea. Define computePrice for both classes for computing
total price. Use following rules for computing total price by summing up sub-
costs: Construction cost(for both classes):Rs.500/- per [Link] Additional cost (
for Flat) : Rs. 200000/- ( for Bungalow ): Rs. 200/- per sq. feet for landArea Land
cost ( only for Bungalow ): Rs. 400/- per sq. feet Define method main to show
usage of method computePrice.
Page | 19
Rana Keval Y.(1087) 4CE2
OUTPUT :
4. Define following classes and interfaces. public interface GeometricShape {
public void describe(); } public interface TwoDShape extends GeometricShape {
public double area(); } public interface ThreeDShape extends GeometricShape {
public double volume(); } public class Cone implements ThreeDShape { private
double radius; private double height; public Cone (double radius, double
height) public double volume() public void describe() } public class Rectangle
implements TwoDShape { private double width, height; public Rectangle
(double width, double height) public double area() public double perimeter()
public void describe() } public class Sphere implements ThreeDShape { private
double radius; public Sphere (double radius) public double volume() public void
describe() } Define test class to call various methods of Geometric Shape.
Program :
package Lab4;
interface GeometricShape
Page | 20
Rana Keval Y.(1087) 4CE2
void describe();
interface TwoDShape extends GeometricShape
double area();
interface ThreeDShape extends GeometricShape
double volume();
class Cone implements ThreeDShape
private double radius;
private double height;
public Cone(double radius, double height)
[Link] = radius;
[Link] = height;
public double volume()
return ([Link] * radius * radius * height) / 3;
public void describe()
Page | 21
Rana Keval Y.(1087) 4CE2
[Link]("Cone: radius=" + radius + " height=" + height);
[Link]("Volume = " + volume());
class Rectangle implements TwoDShape
private double width;
private double height;
public Rectangle(double width, double height)
[Link] = width;
[Link] = height;
public double area()
return width * height;
public double perimeter()
return 2 * (width + height);
public void describe()
Page | 22
Rana Keval Y.(1087) 4CE2
[Link]("Rectangle: width=" + width + " height=" + height);
[Link]("Area = " + area());
[Link]("Perimeter = " + perimeter());
class Sphere implements ThreeDShape
private double radius;
public Sphere(double radius)
[Link] = radius;
public double volume()
return (4.0/3.0) * [Link] * radius * radius * radius;
public void describe()
[Link]("Sphere: radius=" + radius);
[Link]("Volume = " + volume());
public class TestShapes
Page | 23
Rana Keval Y.(1087) 4CE2
public static void main(String[] args)
Rectangle r = new Rectangle(10,5);
Cone c = new Cone(3,7);
Sphere s = new Sphere(4);
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
Output:
Mr. Sunit Parmar
(Teacher’s Signature)
Page | 24
Rana Keval Y.(1087) 4CE2
PRACTICAL – 5
Date: / /
Inner Class:
Define two nested classes: Processor and RAM inside the outer class: CPU with
following
data members
class CPU {
double price;
class Processor{ // nested class
double cores;
double catch()
String manufacturer;
double getCache()
void displayProcesorDetail()
protected class RAM{ // nested protected class
// members of protected nested class
double memory;
String manufacturer;
Double clockSpeed;
double getClockSpeed()
void displayRAMDetail()
}
}
1. Write appropriate Constructor and create instance of Outer and inner class and call
the
methods in main function
Program :
package Lab5;
Page | 25
Rana Keval Y.(1087) 4CE2
class CPU
{
double price;
CPU(double price)
[Link] = price;
class Processor
double cores;
double cache;
String manufacturer;
Processor(double cores, double cache, String manufacturer)
[Link] = cores;
[Link] = cache;
[Link] = manufacturer;
}
double getCache()
return cache;
void displayProcesorDetail()
{
[Link]("Processor Manufacturer: " + manufacturer);
[Link]("Cores: " + cores);
[Link]("Cache: " + getCache());
Page | 26
Rana Keval Y.(1087) 4CE2
}
}
protected class RAM
double memory;
String manufacturer;
double clockSpeed;
RAM(double memory, String manufacturer, double clockSpeed)
[Link] = memory;
[Link] = manufacturer;
[Link] = clockSpeed;
}
double getClockSpeed()
{
return clockSpeed;
void displayRAMDetail()
[Link]("RAM Manufacturer: " + manufacturer);
[Link]("Memory: " + memory + " GB");
[Link]("Clock Speed: " + getClockSpeed() + " GHz");
public static void main(String[] args)
CPU cpu = new CPU(50000);
[Link] p = [Link] Processor(8, 12, "Intel");
[Link] r = [Link] RAM(16, "Corsair", 3.2);
Page | 27
Rana Keval Y.(1087) 4CE2
[Link]("CPU Price: " + [Link]);
[Link]();
[Link]();
[Link]();
OUTPUT :
2. Write a program to demonstrate usage of static inner class, local inner class
and anonymous inner class
Program :
package Lab5;
class Outer
{
static class StaticInner
{
void display()
{
[Link]("Static Inner Class");
}
}
Page | 28
Rana Keval Y.(1087) 4CE2
void showLocalClass()
{
class LocalInner
void display()
[Link]("Local Inner Class");
LocalInner l = new LocalInner();
[Link]();
}
}
interface Demo
void show();
public class InnerClassDemo
{
public static void main(String[] args)
{
[Link] s = new [Link]();
[Link]();
Outer o = new Outer();
[Link]();
Demo d = new Demo()
{
public void show()
Page | 29
Rana Keval Y.(1087) 4CE2
{
[Link]("Anonymous Inner Class");
};
[Link]();
OUTPUT:
Mr. Sunit Parmar
(Teacher’s Signature)
Page | 30
Rana Keval Y.(1087) 4CE2
PRACTICAL – 7
Date: / /
Exception Handing
1. Write a program for creating a Bank class, which is used to manage the bank
account of customers. Class has two methods, Deposit () and withdraw ().
Deposit method display old balance and new balance after depositing the
specified amount. Withdrew method display old balance and new balance after
withdrawing. If balance is not enough to withdraw the money, it throws
ArithmeticException and if balance is less than 500rs after withdrawing then it
throw custom exception, NotEnoughMoneyException.
Program :
package Lab7;
class NotEnoughMoneyException extends Exception
{
NotEnoughMoneyException(String msg)
super(msg);
class Bank
{
double balance;
Bank(double balance)
{
[Link] = balance;
}
void deposit(double amount)
Page | 31
Rana Keval Y.(1087) 4CE2
double oldBalance = balance;
balance = balance + amount;
[Link]("Old Balance: " + oldBalance);
[Link]("New Balance: " + balance);
}
void withdraw(double amount) throws NotEnoughMoneyException
{
double oldBalance = balance;
if(amount > balance)
throw new ArithmeticException("Insufficient Balance");
balance = balance - amount;
if(balance < 500)
throw new NotEnoughMoneyException("Balance less than 500");
[Link]("Old Balance: " + oldBalance);
[Link]("New Balance: " + balance);
}
public static void main(String[] args)
{
Bank b = new Bank(2000);
try
[Link](500);
[Link](1700);
}
catch(ArithmeticException e)
Page | 32
Rana Keval Y.(1087) 4CE2
[Link]([Link]());
catch(NotEnoughMoneyException e)
{
[Link]([Link]());
OUTPUT :
2. Write a complete program for calculation average of n +ve integer numbers
of Array A. a. Read the array form keyboard b. Raise and handle Exception if i.
Element value is -ve or non-integer. ii. If n is zero.
Program :
package Lab7;
import [Link];
class Average
{
public static void main(String[] args)
Page | 33
Rana Keval Y.(1087) 4CE2
Scanner sc = new Scanner([Link]);
try
[Link]("Enter n: ");
int n = [Link]();
if(n == 0)
throw new ArithmeticException("n cannot be zero");
int arr[] = new int[n];
int sum = 0;
[Link]("Enter " + n + " positive integers:");
for(int i = 0; i < n; i++)
{
arr[i] = [Link]();
if(arr[i] < 0)
throw new Exception("Negative numbers not allowed");
sum += arr[i];
double avg = (double)sum / n;
[Link]("Average = " + avg);
}
catch(Exception e)
{
[Link]("Error: " + [Link]());
Page | 34
Rana Keval Y.(1087) 4CE2
OUTPUT :
Mr. Sunit Parmar
(Teacher’s Signature)
Page | 35