0% found this document useful (0 votes)
7 views7 pages

P 1,2,3,4

JAVA LAB FILE UP TO 4 PRACTICALS

Uploaded by

devilsgreat7
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)
7 views7 pages

P 1,2,3,4

JAVA LAB FILE UP TO 4 PRACTICALS

Uploaded by

devilsgreat7
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
You are on page 1/ 7

Program: 1

Install Java compiler and eclipse pla orm to write and


execute java program

Step 1: Install the Java Development Kit (JDK)


Download the JDK:
 Go to the official Oracle JDK download page:
h ps://www.oracle.com/java/technologies/javase-downloads.html
 Choose your OS (Windows/macOS/Linux) and download the latest JDK
(e.g., Java 17 or Java 21).
Install the JDK:
 Run the installer and follow the prompts.
 A er installa on, you may need to set the JAVA_HOME environment
variable and update the PATH.
Windows:
1. Right-click "This PC" → Proper es → Advanced system se ngs →
Environment Variables.
2. Add a new System Variable:
o JAVA_HOME = C:\Program Files\Java\jdk-xx
3. Add %JAVA_HOME%\bin to your Path.
macOS/Linux:
Add this to your .bashrc, .zshrc, or .bash_profile:
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=$JAVA_HOME/bin:$PATH

Step 2: Verify Java Installa on


Open a terminal or command prompt and type:
java -version
javac -version
You should see version info like:
java version "21.0.1"
javac 21.0.1

Step 3: Install Eclipse IDE


Download Eclipse:
 Go to: h ps://www.eclipse.org/downloads/
 Click “Download” for Eclipse IDE for Java Developers.
Install Eclipse:
 Run the Eclipse installer and choose:
o Eclipse IDE for Java Developers
 Choose the default installa on folder or set your own.

Step 4: Run Eclipse and Write Your First Java Program


1. Open Eclipse.
2. Create a new workspace or use the default.
3. Go to File > New > Java Project → Name your project → Finish.
4. Right-click src → New > Class → Name it HelloWorld → Check public
sta c void main(String[] args) → Finish.
5. Inside the class, add:
java
public class HelloWorld {
public sta c void main(String[] args) {
System.out.println("Hello, world!");
}
}
6. Click the green “Run” bu on ( ) on the toolbar
Program: 2
Create a simple java programs using command line arguments

Program: GreetUser.java
public class GreetUser {
public sta c void main(String[] args) {
// Check if the user passed an argument
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello, stranger!");
}
}
}
How to Compile and Run (Using Command Line)
1. Open your terminal or command prompt.
2. Navigate to the directory where your file GreetUser.java is saved.
3. Compile the program:
javac GreetUser.java

4.Run the program with a command-line argument:


java GreetUser Aman

Output:
Hello,Aman

If you run it without an argument:


java GreetUser

Output:
Hello, stranger!
Program: 3
WAP to implement OOP concepts and basics of Java programming .

Java program that demonstrates OOP concepts like:


 Class and Object
 Encapsula on
 Inheritance
 Polymorphism (Method Overriding)
 Abstrac on (using abstract class)

Java Program: Demonstra ng OOP Concepts


// Abstract class - demonstrates Abstrac on
abstract class Person {
protected String name;
protected int age;

// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Abstract method
abstract void displayInfo();
}

// Student class - demonstrates Inheritance and Encapsula on


class Student extends Person {
private String studentId;

// Constructor
public Student(String name, int age, String studentId) {
super(name, age); // calling superclass constructor
this.studentId = studentId;
}

// Ge er and Se er - Encapsula on
public String getStudentId() {
return studentId;
}
public void setStudentId(String id) {
studentId = id;
}

// Method Overriding - Polymorphism


@Override
public void displayInfo() {
System.out.println("Student Name: " + name);
System.out.println("Age: " + age);
System.out.println("Student ID: " + studentId);
}
}

// Main class
public class OOPDemo {
public sta c void main(String[] args) {
// Crea ng object - Object Instan a on
Student s1 = new Student("Alice", 20, "S101");

// Accessing data using encapsulated methods


s1.setStudentId("S102"); // upda ng ID
s1.displayInfo(); // method call
}
}

Output:
Student Name: Alice
Age: 20
Student ID: S102

Concepts Used:
Concept Example Used
Class & Object Student, Person, OOPDemo
Inheritance Student extends Person
Encapsula on studentId with ge ers/se ers
Abstrac on Abstract class Person
Polymorphism displayInfo() overridden in Student
Program: 4
Create Java programs using inheritance and polymorphism.

Java Program: Using Inheritance and Polymorphism


// Superclass (Base class)
class Animal {
public void makeSound() {
System.out.println("Some generic animal sound");
}
}

// Subclass - Dog inherits Animal


class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Dog says: Woof Woof!");
}
}

// Subclass - Cat inherits Animal


class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("Cat says: Meow Meow!");
}
}

// Subclass - Cow inherits Animal


class Cow extends Animal {
@Override
public void makeSound() {
System.out.println("Cow says: Moo Moo!");
}
}

// Main class
public class InheritancePolymorphismDemo {
public sta c void main(String[] args) {
// Inheritance + Polymorphism using superclass reference
Animal myAnimal; // reference of base class
myAnimal = new Dog(); // Dog object
myAnimal.makeSound(); // Outputs Dog sound

myAnimal = new Cat(); // Cat object


myAnimal.makeSound(); // Outputs Cat sound

myAnimal = new Cow(); // Cow object


myAnimal.makeSound(); // Outputs Cow sound
}
}

Output:
Dog says: Woof Woof!
Cat says: Meow Meow!
Cow says: Moo Moo!

Key Concepts:
Concept How It's Used
Inheritance Dog, Cat, Cow extend Animal class
Polymorphism makeSound() method is overridden in each class
Dynamic Binding Method called based on actual object type at run me

You might also like