Java Practical
Java Practical
Practical File
BCA SEM-4
Mer Jesal N.
Roll No. 230
Java Practical
Guided by Mrs. Jinita Kapure
Page 1 of 29
Index
Unit I
Java Introduction
Program Page
Name of Program Signature
No. No.
Write a program to evaluate simple interest of a
1 5 to 23
given principle, rate and time.
A motor cycle dealer sells two-wheelers to his customer on
loan, which is to be repaid in 5 years. The dealer charges
simple interest for the whole term on the day of giving the
loan itself. The total amount is then divided by 60(months)
2 24 to 38
and is collected as equated monthly instalment (EMI). Write
a program to calculate the EMI for a loan of Rs. X, where X
is given from command line argument. Print the EMI value
in rupees
A car accessories shop assigns code 1 to seat covers, 2 to
steering wheel covers , 3 to car lighting and 4 for air
purifiers. All other items have code 5 or more. While
selling the goods, a sales tax of 2% to seat covers ,3% to
3 steering wheel covers, 4% to car lighting, 2.5% to air 39 to 40
purifiers and 1.2% for all other items is charged. A list
containing the product code and price is given for making
a bill. Write a java program using switch statements to
prepare a bill.
Page 2 of 29
of Rs.1,500 or more. Write a program to implement the
above scheme for a given sales and print out the sales and
print out the sales value, discount and net amount payable
by a customer. Create necessary methods and constructors
A bank gives 6.5% per annum interest on deposits made in
that bank. Write a program to calculate the total amount that
9 a person will receive after the end of 5 years for a deposit of 53 to 55
Rs.5000 for compound interest. Create necessary methods
and constructors.
Unit II
Array, Inheritance and Interface
Write a program to sort the elements of one
1 dimensional array. Read value of array elements 60 to 61
through command line argument..
Write a program to create an array to store 5 integer
2 values. Also initialize the array with 5 numbers and
display the array Elements in reverse order
Page 3 of 29
Declare an abstract class Vehicle with an abstract
method named numWheels().provide subclasses
7 Car and Truck that each implements this method.
Create instance of these subclasses and
demonstrate the use of this method
Write an interface called Exam with a method Pass(int
mark) that returns a
Boolean. Write another interface called Classify with a
method Division(int
average) which returns a string. Write a class called Result
which implements
both Exam and Classify. The pass method should return true
8
if the marks is
greater than or equal to 35 else false. The division method
must return “First”
when the parameter average is 60 or more, “second” when
average is 50 or
more but below 60, “no division” when average is less than
50
Create class calculation with an abstract method
9 area( ). Create Rectangle and
Triangle subclasses of calculation and find area of
rectangle and triangle.
The abstract Vegetable class has four subclasses named
cabbage, carrot and
potato. Write an application that demonstrates how to
establish this class
10 hierarchy. Declare one instance variable of type string that
indicates the color of
a vegetable. Create and display instances of these object.
Override the toString() method of object to return a string
with the name of the vegetable and its color
Page 4 of 29
Unit I
Java Introduction
1. Write a program to evaluate simple interest of a given principle, rate and time.
import java.util.Scanner;
class P1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Output:
import javax.swing.JOptionPane;
class Loan{
int loan;
Page 5 of 29
int interest;
double emi;
Loan(int loan,int interest){
this.loan=loan;
this.interest=interest;
}
double calcEmi(){
return loan*interest/(100*60);
}
}
class P2
{
public static void main(String[] args){
int loan;
int interest;
Output:
Page 6 of 29
3. A car accessories shop assigns code 1 to seat covers, 2 to steering wheel covers , 3
to car lighting and 4 for air purifiers. All other items have code 5 or more. While
selling the goods, a sales tax of 2% to seat covers ,3% to steering wheel covers,
4% to car lighting, 2.5% to air purifiers and 1.2% for all other items is charged.
A list containing the product code and price is given for making a bill. Write a
java program using switch statements to prepare a bill.
import java.util.*;
class P3
{
public static void main(String args[])
{
double seat_cover=0.02;
double steering_wheel =0.03;
double car_lighting=0.04;
double air_purifiers =0.025;
double all_other=0.012;
double total=0;
Scanner s=new Scanner(System.in);
int count=s.nextInt();
s.nextLine();
for(int i=0;i<count;i++)
{
System.out.println("Enter The product code and
price -");
int prodcode=Integer.parseInt(input[0]);
double price=Double.parseDouble(input[1]);
double taxcal=0;
switch(prodcode)
{
case 1:
taxcal=seat_cover;
break;
Page 7 of 29
case 2:
taxcal=steering_wheel;
break;
case 3:
taxcal=car_lighting;
break;
case 4:
taxcal=air_purifiers;
break;
default:
taxcal=all_other;
break;
double taxamount=price*taxcal;
double totalprice=price+taxamount;
total=total+totalprice;
}
System.out.println("The Total Bill is:-"+total);}
}
Output:
Page 8 of 29
4. Write a java program to scan 3 integer values from the command line argument
import java.util.*;
class P4{
public static void main(String args[]){
int a,b,c,max;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
c=Integer.parseInt(args[2]);
max=a>b?(a>c?a:c):(b>c?b:c);
Output:
5. Write a program to calculate the hypotenuse of right angled triangle when other
sides of the triangle are given. (Hypotenuse = square root (x*x + Y *Y)).
import java.util.Scanner;
class P5{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the Value of X: ");
int x = input.nextInt();
System.out.print("Enter the Value of Y: ");
int y = input.nextInt();
Page 9 of 29
6. Write a program to calculate the area of square and rectangle by overloading the
area method.
import java.util.Scanner;
class P6{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Output:
Page 10 of 29
7. Create a complex number class. The class should have a constructor and methods
to add, subtract and multiply two complex numbers and to return the real and
imaginary parts
class Complex{
int real,imag;
Complex(){}
class P7{
public static void main(String[] args){
Complex c1=new Complex(4,8);
Complex c2=new Complex(5,7);
Complex c3=new Complex();
c3=c3.addComplex(c1,c2);
System.out.println("Sum: " + c3.real + " + i" + c3.imag);
c3=c3.multiComplex(c1,c2);
System.out.println("Multiplication: " + c3.real + " + i" + c3.imag);
c3=c3.subComplex(c1,c2);
System.out.println("Substraction: " + c3.real + " + i" + c3.imag);
}
Page 11 of 29
}
Output:
8. A shop during festival season offers a discount 10% for purchase made up to
Rs.1,000, 12% for purchase value of Rs.1,000 or more up to Rs 1,500 and 15%
for
purchase value of Rs.1,500 or more. Write a program to implement the above
scheme for a given sales and print out the sales and print out the sales value,
discount and net amount payable by a customer. Create necessary methods and
constructors.
import java.util.Scanner;
class Discount{
Discount(int amt){
if(amt<1000)
calcDiscount(10,amt);
else if(amt>=1500)
calcDiscount(15,amt);
}
dis=(amt * dis)/100;
System.out.print("\nDiscount on your Purchase: " + dis);
System.out.println();
amt-=dis;
System.out.print("\nPayable amount after deducting Discount: " +
amt);
}
}
Page 12 of 29
class P8{
public static void main(String[] args){
Output:
9. A bank gives 6.5% per annum interest on deposits made in that bank. Write a
program to calculate the total amount that a person will receive after the end of
5 years for a deposit of Rs.5000 for compound interest. Create necessary methods
and constructors.
import java.util.*;
class CompoundInterest {
double p, r, n, fn;
CompoundInterest(){
p = 5000;
r = 6.5;
Page 13 of 29
n = 5;
}
void calInt() {
fn = p * Math.pow(1 + r / 100, n);
System.out.println("Compound interest is: " + fn);
}
}
class P9 {
public static void main(String args[]) {
CompoundInterest com = new CompoundInterest();
com.calInt();
}
}
Output:
10. Write a java program to display powers of 2 i.e. 2,4,8,16 etc up to 1024 using
bitwise operators
class P10{
public static void main(String[] args){
int a=1;
for(int i=0;i<10;i++){
a<<=1;
System.out.println(a);
}
}
}
Output:
Page 14 of 29
Unit II
Array, Inheritance and Interface
1. Write a program to sort the elements of one dimensional array. Read value of
array elements through command line argument.
int i,j,n,temp=0;
n=args.length;
int a[] = new int[n];
for(i=0;i<n;i++){
a[i]=Integer.parseInt(args[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("Array Elements After Sorting: ");
for(i=0;i<n;i++){
System.out.println(a[i]);
}
}
}
Output:
Page 15 of 29
2. Write a program to create an array to store 5 integer values. Also initialize the array
with 5 numbers and display the array Elements in reverse order.
class P2{
public static void main(String[] args){
int arr[] = new int[] {1,2,3,4,5};
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i] + "");
}
System.out.println();
for(int i=arr.length-1;i>=0;i--)
{
System.out.print(arr[i] + "");
}
}
}
Output:
class P3{
public static void main(String[] args){
int m,n,c,d;
for(c=0;c<m;c++)
{
for(d=0;d<n;d++)
{
sum[c][d]=first[c][d]+second[c][d];
}
}
Output:
4. Write program to create an array of company name and another array of price
quoted by the company. Fetch the company name who has quoted the lowest
amount.
class P4{
public static void main(String[] args){
int n,i;
String company[] = {"LG","Samsung","Apple","Jio","Realme"};
int price[]= {2000,5000,1000,999,3000};
n=price.length;
int low = price[0];
for(i=1;i<=n-1;i++)
{
if(low>price[i])
low=price[i];
}
for(i=1;i<=n-1;i++)
Page 18 of 29
{
if(low==price[i])
System.out.print("Company Name: " + company[i]);
}
}
}
Output:
5. Write an interface called numbers, with a method in Process(int x, int y). Write a
class called Sum, in which the method Process finds the sum of two numbers and
returns an int value. Write another class called Average, in which the Process
method finds the average of the two numbers and returns an int.
interface Numbers{
public int process(int x, int y);
}
class P5{
public static void main(String[] args){
int a,b;
Sum add = new Sum();
a = add.process(10,20);
System.out.print("Addition of 2 numbers using Interface: " + a);
6. Create a class called NumberData that accept any array of the five numbers. Create
a sub class called Numplay which provides methods for followings: 1. Display
numbers entered. 2. Sum of the number. 3. Average of the numbers. 4. Maximum
of the numbers. 5. Minimum of the numbers. Create a class that provides menu for
above methods. Give choice from the command-line argument.
import java.util.Scanner;
class NumberData{
Scanner input;
int n;
int arr[];
class P6{
public static void main(String[] args){
switch(choice){
case 1:
obj.Display();
break;
case 2:
obj.Sum();
break;
case 3:
obj.Average();
break;
case 4:
obj.Max();
break;
case 5:
obj.Min();
break;
}
}while(choice!=0);
}
}
Output:
Page 22 of 29
Page 23 of 29
7. Declare an abstract class Vehicle with an abstract method named numWheels(
).provide subclasses Car and Truck that each implements this method. Create
instance of these subclasses and demonstrate the use of this method
class P7{
public static void main(String[] args){
Car c = new Car();
c.numWheels();
Truck t = new Truck();
t.numWheels();
}
}
Output:
8. Write an interface called Exam with a method Pass(int mark) that returns a
Boolean. Write another interface called Classify with a method Division(int
average) which returns a string. Write a class called Result which implements both
Exam and Classify. The pass method should return true if the marks is greater than
or equal to 35 else false. The division method must return “First” when the
parameter average is 60 or more, “second” when average is 50 or more but below
60, “no division” when average is less than 50.
import java.util.Scanner;
interface Exam{
boolean Pass(int mark);
}
interface Classify{
String Division(double avg);
}
Page 25 of 29
class Result implements Exam,Classify{
public boolean Pass(int mark){
if(mark>=35)
return true;
else
return false;
}
class P8{
public static void main(String[] args){
boolean pass;
int mark;
double avg;
String division;
Scanner input = new Scanner(System.in);
if(pass)
System.out.print("Passed: " + division);
else
System.out.print("Failed: " + division);
}
}
Output:
Page 26 of 29
9. Create class calculation with an abstract method area( ). Create Rectangle and
Triangle subclasses of calculation and find area of rectangle and triangle.
import java.util.Scanner;
class P9{
public static void main(String[] args){
Output:
10. The abstract Vegetable class has four subclasses named cabbage, carrot and
potato. Write an application that demonstrates how to establish this class
hierarchy. Declare one instance variable of type string that indicates the color of a
vegetable. Create and display instances of these object. Override the toString()
method of object to return a string with the name of the vegetable and its color
Page 28 of 29
class Carrot extends Vegetables{
public String toString(){
color = "has Orange Color";
return "Carrot " + color;
}
}
class P10{
System.out.println(c);
System.out.println(ca);
System.out.println(p);
System.out.println(b);
}
}
Output:
Page 29 of 29