[go: up one dir, main page]

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

Program1,2,3,4

JAVA

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Program1,2,3,4

JAVA

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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Program: 1

Install Java compiler and eclipse platform 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:
https://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.
 After installation, you may need to set the JAVA_HOME environment
variable and update the PATH.
➕ Windows:
1. Right-click "This PC" → Properties → Advanced system settings →
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 Installation


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: https://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 installation 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
static void main(String[] args) → Finish.
5. Inside the class, add:
java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
6. Click the green “Run” button (▶️) on the toolbar
Program: 2
Create a simple java programs using command line arguments

Program: GreetUser.java
public class GreetUser {
public static 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
 Encapsulation
 Inheritance
 Polymorphism (Method Overriding)
 Abstraction (using abstract class)

✅ Java Program: Demonstrating OOP Concepts


// Abstract class - demonstrates Abstraction
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 Encapsulation


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;
}

// Getter and Setter - Encapsulation


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 static void main(String[] args) {
// Creating object - Object Instantiation
Student s1 = new Student("Alice", 20, "S101");

// Accessing data using encapsulated methods


s1.setStudentId("S102"); // updating 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
Encapsulation studentId with getters/setters
Abstraction 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 static 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 runtime

You might also like