0% found this document useful (0 votes)
8 views29 pages

Java Programming Practical Exercises

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

Java Programming Practical Exercises

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

K.V.

R Shinde Shikshan Sanstha’s


Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 1 Date:-

Title :- Write a java program that test a number as input and print its
multiplication table.

package practical1;

import [Link].*;

public class Main

public static void main(String[] args)

int a,i;

Scanner s=new Scanner([Link]);

[Link]("Enter Number=");

a=[Link]();

for(i=1;i<=10;i++)

[Link](" " + (a*i));

}
}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 2 Date:-

Title :- Write a java program that takes a year from the user and prints
whether it is a leap year or not

package practical2;

import [Link].*;

public class Main

public static void main(String[] args)

int y;

Scanner s=new Scanner([Link]);

[Link]("Enter year=");

y=[Link]();

if(y%4==0)

[Link]("This is leap year");

else

{
[Link]("This is not leap year");

Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 3 Date:-15/07/2025

Title :- Write a java program to take number from user and display whether
it is an Armstrong number.

package practical3;

import [Link].*;

import [Link];

public class Main

public static void main(String[] args)

Scanner s=new Scanner([Link]);

[Link]("Enter a number=");

int n = [Link]();

int a,re,result=0;

int original_n = n;

while(n>0)

re=n%10;

result+=[Link](re,3);
n=n/10;

if(original_n==result)

[Link]("This is armstrong number");

else

[Link]("This is not armstrong number");

}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 4 Date:-29/07/205

Title :- Write a java program to display a right angle triangle pattern with
numbers up to 10.

package practical4;

public class Main {

public static void main(String[] args) {

int n = 10;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

[Link](j + " ");

[Link]();

Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 5 Date:-05/08/2025

Title :- Write a java program to create a class called Book with instance
variables title, author and price. Implement different constructors.

package practical5;
public class book {

private String title;

private String author;

private int price;

book() {

[Link] ="The Hobbit";

[Link] ="J.R.R. Tolkien";

book(String title, String author) {

[Link]=title;

[Link]=author;

[Link]=price;

book(String title, String author, int price)

{
[Link]=title;

[Link]=author;

[Link]=price;

void display()

[Link]("Book Title="+title);

[Link]("Book Author="+author);

[Link]("Book Price="+price);

public static void main(String[] args)

book b1 = new book();

[Link]();

book b2 = new book("The Lord of the Rings", "J.R.R. Tolkien", 879);

[Link]();

}
}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 6 Date:-12/08/2025

Title :- Write a java program to create a class called Shape with a method
getArea(). Create a subclass Rectangle that overrides getArea().

package practical6;

import [Link].*;

class shape {

public double getArea() {

return 0;

class rectangle extends shape {

private double height;

private double width;

public rectangle(double h, double w) {

[Link] = h;

[Link] = w;

public double getArea() {

return height * width;


}

public class main {

public static void main(String[] args) {

rectangle r = new rectangle(9.0, 7.0);

[Link]("Area of rectangle=" + [Link]());

}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 7 Date:-19/08/2025

Title :- Write a java program to create a class BankAccount with deposit and
withdraw methods. Create a subclass SavingsAccount that prevents
withdrawal beyond balance.

package practical7;

class BankAccount {

protected double balance;

public BankAccount(double initialBalance) {

balance = initialBalance;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

[Link]("Deposited: ₹" + amount);

} else {

[Link]("Invalid deposit amount.");

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {

balance -= amount;

[Link]("Withdrew: ₹" + amount);

} else {

[Link]("Insufficient balance or invalid amount.");

public void checkBalance() {

[Link]("Current Balance: ₹" + balance);

class SavingsAccount extends BankAccount {

public SavingsAccount(double initialBalance) {

super(initialBalance);

@Override

public void withdraw(double amount) {

if (balance - amount < 100) {

[Link]("Cannot withdraw. Balance cannot fall below


₹100.");

} else {

[Link](amount);

}
}

public class Main {

public static void main(String args[]) {

BankAccount account = new BankAccount(500);

[Link](200);

[Link](100);

[Link]();

SavingsAccount savings = new SavingsAccount(300);

[Link](50);

[Link](100);

[Link](200);

[Link]();

}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 8 Date:-26/08/2025

Title :- Write a java program to create an interface Flyable with method


fly(). Create 3 classes SpaceShuttle, Aeroplane, Helicopter that implement
Flyable.

package practical8;

interface Flyable {

void fly_obj();

class Spacecraft implements Flyable {

@Override

public void fly_obj() {

[Link]("Spacecraft");

class Airplane implements Flyable {

@Override

public void fly_obj() {

[Link]("Airplane");

}
}

class Helicopter implements Flyable {

@Override

public void fly_obj() {

[Link]("Helicopter");

public class Main {

public static void main(String[] args) {

Flyable s = new Spacecraft();

Flyable a = new Airplane();

Flyable h = new Helicopter();

s.fly_obj();

a.fly_obj();

h.fly_obj();

}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :-9 Date:-09/09/2025

Title :- Write a java program to demonstrate method overloading.

package practical9;
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}

public class Main {


public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]([Link](5, 10));
[Link]([Link](3.5, 2.5));
[Link]([Link](1, 2, 3));
}
}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 10 Date:-16/09/2025

Title :- Write a java program to create a producer-consumer problem using


wait() and notify().

package practical0;

class SharedBuffer {

private int data;

private boolean hasData = false;

public synchronized void produce(int value) throws InterruptedException


{

while (hasData) {

wait();

data = value;

hasData = true;

[Link]("Produced: " + data);

notify();

public synchronized void consume() throws InterruptedException {

while (!hasData) {
wait();

[Link]("Consumed: " + data);

hasData = false;

notify();

class Producer extends Thread {

private SharedBuffer buffer;

public Producer(SharedBuffer buffer) {

[Link] = buffer;

@Override

public void run() {

for (int i = 1; i <= 5; i++) {

try {

[Link](i);

[Link](1000);

} catch (InterruptedException e) {

[Link]();

}
}

class Consumer extends Thread {

private SharedBuffer buffer;

public Consumer(SharedBuffer buffer) {

[Link] = buffer;

@Override

public void run() {

for (int i = 1; i <= 5; i++) {

try {

[Link]();

[Link](1500);

} catch (InterruptedException e) {

[Link]();

public class Main {

public static void main(String[] args) {

SharedBuffer buffer = new SharedBuffer();

Producer producer = new Producer(buffer);


Consumer consumer = new Consumer(buffer);

[Link]();

[Link]();

}
Output:
K.V.R Shinde Shikshan Sanstha’s
Shivraj College of Arts, Commerce and [Link]
Science College, Gadhinglaj. ([Link])
BCOM-IT (3rd Year)

Practical No :- 11 Date:- 23/09/2025

Title :- Write a java program that reads a list of numbers from a file and
throws an exception if any number is negative.

:
package practical1;

import [Link].*;

import [Link];

class PositiveNumberException extends Exception {

public PositiveNumberException(String message) {

super(message);

public class Main {

public static void main(String[] args) {

String filePath = " D:\Siddharth\Java\\[Link]";

try {

readFileAndCheckNumbers(filePath);

} catch (PositiveNumberException e) {

[Link]("Exception caught: " + [Link]());

} catch (IOException e) {
[Link]("File error: " + [Link]());

public static void readFileAndCheckNumbers(String filePath) throws


PositiveNumberException, IOException {

File file = new File(filePath);

Scanner scanner = new Scanner(file);

while ([Link]()) {

if ([Link]()) {

int number = [Link]();

[Link]("Read number:"+ number);

if (number > 0) {

throw new PositiveNumberException("Positive number


detected:" +number);

} else {

[Link]();

[Link]();

}
Output:

You might also like