Session: 2021-22
JAVA PROGRAMMING
Submitted To : - Submitted By:-
Miss Prachi Rohit Kumar
Assistant Professor (202046)
CSE Department
SR.NO. PROGRAMS/TOPICS DATE SIGNATURE
1. Write a program to print whether the
given number is Armstrong or Not
2. Write a program to demonstrate method
overloading with different number of
parameters in argument list
3. Write a program to print prime number
between the given range
4. Write a program to facilitates the
multilevel inheritance
5. Try to implement the concept of
multiple inheritance with use of
Interfaces
6. Write a program that demonstrate use of
package and import statement
7. Write a program to demonstrate user
define exception
8. Write a program to implement the
concept of inter thread communication
producer consumer problem.
PROGRAM -1
Write a program to print whether the given number is Armstrong or
Not.
import java.util.Scanner;
public class JavaExample {
public static void main(String[] args) {
int num, number, temp, total = 0;
System.out.println("Ënter 3 Digit Number");
Scanner scanner = new Scanner(System.in);
num = scanner.nextInt();
scanner.close();
number = num;
for( ;number!=0;number /= 10)
temp = number % 10;
total = total + temp*temp*temp;
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
PROGRAM-2
Write a program to demonstrate method overloading with different number of
parameters in argument list.
class MethodOverloading {
private static void display(int a){
System.out.println("Arguments: " + a);
}
private static void display(int a, int b){
System.out.println("Arguments: " + a + " and " + b);
}
public static void main(String[] args) {
display(1);
display(1, 4);
}
}
PROGRAM -3
Write a program to print prime number between the given range .
import java.util.Scanner;
public class Prime
{
public static void main(String args[])
{
int s1, s2, s3, flag = 0, i, j;
Scanner s = new Scanner(System.in);
System.out.println ("Enter the lower limit :");
s1 = s.nextInt();
System.out.println ("Enter the upper limit :");
s2 = s.nextInt();
System.out.println ("The prime numbers in between the entered limits are :");
for(i = s1; i <= s2; i++)
{
for( j = 2; j < i; j++)
{
if(i % j == 0)
{
flag = 0;
break;
}
else
flag = 1;
}
if(flag == 1)
{
System.out.println(i);
}
}
}}
PROGRAM-4
Write a program to facilitates the multilevel inheritance.
class Car{
public Car()
System.out.println("Class Car");
public void vehicleType()
System.out.println("Vehicle Type: Car");
class Maruti extends Car{
public Maruti()
System.out.println("Class Maruti");
public void brand()
System.out.println("Brand: Maruti");
public void speed()
System.out.println("Max: 90Kmph");
public class Maruti800 extends Maruti{
public Maruti800()
System.out.println("Maruti Model: 800");
}
public void speed()
System.out.println("Max: 80Kmph");
public static void main(String args[])
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
PROGRAM-5
Try to implement the concept of multiple inheritance with use of
Interfaces .
interface AnimalEat {
void eat();
interface AnimalTravel {
void travel();
class Animal implements AnimalEat, AnimalTravel {
public void eat() {
System.out.println("Animal is eating");
public void travel() {
System.out.println("Animal is travelling");
public class Demo {
public static void main(String args[]) {
Animal a = new Animal();
a.eat();
a.travel();
}
PROGRAM-6
Write a program that demonstrate use of package and import statement.
1).
package pack;
public class A{
public void add(int a,int b)
{
System.out.println(a+b);}
}
2).
package mypack;
Import pack.*;
Class B{
Public static void main(String args[])
{
A.obj=new a();
Obj.add(5,10);
}
}
PROGRAM-7
Write a program to demonstrate user define exception .
class MyException extends Exception {
public MyException(String s)
{
// Call constructor of parent Exception
super(s);
}
}
// A Class that uses above MyException
public class Main {
// Driver Program
public static void main(String args[])
{
try {
// Throw an object of user defined exception
throw new MyException("rohit");
}
catch (MyException ex) {
System.out.println("Caught");
// Print the message from MyException object
System.out.println(ex.getMessage());
}
}
}
PROGRAM-8
Write a program to implement the concept of inter thread communication
producer consumer problem.
public class ProducerConsumer
public static void main(String[] args)
Shop c = new Shop();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
class Shop
private int materials;
private boolean available = false;
public synchronized int get()
while (available == false)
try
wait();
catch (InterruptedException ie)
available = false;
notifyAll();
return materials;
public synchronized void put(int value)
while (available == true)
try
wait();
catch (InterruptedException ie)
ie.printStackTrace();
materials = value;
available = true;
notifyAll();
class Consumer extends Thread
private Shop Shop;
private int number;
public Consumer(Shop c, int number)
Shop = c;
this.number = number;
public void run() {
int value = 0;
for (int i = 0; i < 10; i++)
value = Shop.get();
System.out.println("Consumed value " + this.number+ " got: " + value);
class Producer extends Thread
private Shop Shop;
private int number;
public Producer(Shop c, int number)
Shop = c;
this.number = number;
public void run()
for (int i = 0; i < 10; i++)
Shop.put(i);
System.out.println("Produced value " + this.number+ " put: " + i);
try
sleep((int)(Math.random() * 100));
catch (InterruptedException ie)
ie.printStackTrace();
} }}}