JAVA PROGRAMMING LAB
[Link] eclipse or Netbean platform and acquaint with the various menus, create a test
project, add a test class and run it see how you can use auto suggestions, auto fill. Try code
formatter and code refactoring like renaming variables, methods and classes. Try debug step
by step with a small program of about 10 to 15 lines which contains at least one if else
condition and a for loop.
SOURCE CODE:/* Sample java program to check given number is prime or not */
//Importing packages
import [Link];
import [Link];
class Sample_Program
{
public static void main(String args[]) {
int i,count=0,n;
Scanner sc=new Scanner([Link]);
[Link]("Enter Any Number : ");
n=[Link]();
for(i=1;i<=n;i++) {
if(n%i==0) {
count++;
}
}
if(count==2)
[Link](n+" is prime");
else
[Link](n+" is not prime");
}
}
OUTPUT:
Enter Any Number : 25
25 is not prime
JAVA PROGRAMMING LAB
Write a Java program to demonstrate the OOP principles. [i.e., Encapsulation, Inheritance,
Polymorphism and Abstraction]
Encapsulation:
class Employee {
// Private fields (encapsulated data)
private int id;
private String name;
// Setter methods
public void setId(int id) {
[Link] = id;
public void setName(String name) {
[Link] = name;
// Getter methods
public int getId() {
return id;
public String getName() {
return name;
public class Main {
JAVA PROGRAMMING LAB
public static void main(String[] args) {
Employee emp = new Employee();
// Using setters
[Link](101);
[Link]("Geek");
// Using getters
[Link]("Employee ID: " + [Link]());
[Link]("Employee Name: " + [Link]());
Output:
Employee ID: 101
Employee Name: Geek
Inheritance:
// Superclass (Parent)
class Animal {
void eat() {
[Link]("Animal is eating...");
void sleep() {
[Link]("Animal is sleeping...");
// Subclass (Child) - Inherits from Animal
JAVA PROGRAMMING LAB
class Dog extends Animal {
void bark() {
[Link]("Dog is barking!");
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
// Inherited methods (from Animal)
[Link]();
[Link]();
// Child class method
[Link]();
OUTPUT:
Animal is eating...
Animal is sleeping...
Dog is barking!
Polymorphism:
// Parent Class
class Parent {
// Overloaded method (compile-time polymorphism)
public void func() {
JAVA PROGRAMMING LAB
[Link]("[Link]()");
// Overloaded method (same name, different parameter)
public void func(int a) {
[Link]("[Link](int): " + a);
// Child Class
class Child extends Parent {
// Overrides [Link](int) (runtime polymorphism)
@Override
public void func(int a) {
[Link]("[Link](int): " + a);
public class Main {
public static void main(String[] args) {
Parent parent = new Parent();
Child child = new Child();
// Dynamic dispatch
Parent polymorphicObj = new Child();
// Method Overloading (compile-time)
[Link]();
[Link](10);
JAVA PROGRAMMING LAB
// Method Overriding (runtime)
[Link](20);
// Polymorphism in action
[Link](30);
[Link]()
[Link](int): 10
[Link](int): 20
[Link](int): 30
ABSTRACTION:
abstract class Vehicle {
// Abstract methods (what it can do)
abstract void accelerate();
abstract void brake();
// Concrete method (common to all vehicles)
void startEngine() {
[Link]("Engine started!");
// Concrete implementation (hidden details)
class Car extends Vehicle {
@Override
void accelerate() {
JAVA PROGRAMMING LAB
[Link]("Car: Pressing gas pedal...");
// Hidden complex logic: fuel injection, gear shifting, etc.
@Override
void brake() {
[Link]("Car: Applying brakes...");
// Hidden logic: hydraulic pressure, brake pads, etc.
public class Main {
public static void main(String[] args) {
Vehicle myCar = new Car();
[Link]();
[Link]();
[Link]();
Engine started!
Car: Pressing gas pedal...
Car: Applying brakes...