[go: up one dir, main page]

0% found this document useful (0 votes)
4 views8 pages

OOPS With Java Experiment 1 to 4

The document outlines various Java programming concepts and provides step-by-step instructions for using the Eclipse IDE to write and execute Java programs. It covers topics such as command-line arguments, object-oriented programming principles like abstraction, encapsulation, inheritance, and polymorphism, along with sample code for practical understanding. Additionally, it explains the benefits of using OOP in Java and demonstrates inheritance and polymorphism through example programs.

Uploaded by

ayushvishwkrma1
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)
4 views8 pages

OOPS With Java Experiment 1 to 4

The document outlines various Java programming concepts and provides step-by-step instructions for using the Eclipse IDE to write and execute Java programs. It covers topics such as command-line arguments, object-oriented programming principles like abstraction, encapsulation, inheritance, and polymorphism, along with sample code for practical understanding. Additionally, it explains the benefits of using OOP in Java and demonstrates inheritance and polymorphism through example programs.

Uploaded by

ayushvishwkrma1
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/ 8

Program 1

Objective: Use Java Compiler and Eclipse platform to write and execute java program.

Related Theory: Eclipse is the most popular IDE for Java application development. In 2001, IBM formed a
consortium to support the development of Eclipse IDE. In 2004, it become the Eclipse Foundation whose main
vision was to guide, implement, and share the development of open-source (software whose source code is
released under a license) Eclipse projects in a vendor-natural environment. It is an open-source Integrated
Development Environment (IDE). It is written in Java. It is used to develop Java applications (Java SE and Java EE). It
is also used to develop applications in other languages, such as C, C++, PHY, Python, Perl, and other web projects
with the help of extensible plug-ins. We can run it on different platforms, like Windows, Linux, and macOS.
Presently, it is the widely used IDE for developing Java applications.

Here are the steps on how to write and execute a Java program using the Java compiler and Eclipse platform:

1. Open Eclipse.

2. Create a new Java project.

3. Create a new class in the project.

4. Write your Java code in the class file.

5. Save the class file.

6. Compile the Java code using the Java compiler.

7. Execute the Java program.

Here are some more details about each step:

1. To open Eclipse, double-click on the Eclipse icon on your desktop.

2. To create a new Java project, select File > New > Java Project.

3. In the New Java Project dialog, enter a name for your project and click Finish.

4. To create a new class in the project, right-click on the project name in the Package Explorer and select New
> Class.

5. In the New Java Class dialog, enter a name for your class and select the public static void main(String[] args)
checkbox. Click Finish.

6. Write your Java code in the class file.

7. To save the class file, click the Save button on the toolbar.

8. To compile the Java code, click the Run button on the toolbar.

9. To execute the Java program, click the Run button on the toolbar.
Program Code:

public class HelloWorld

public static void main(String args[])

System.out.println(“Hello World!”);

Output:
Program: 2

Objective: Creating simple java programs using command line arguments.

Related Theory: What Are Command Line Arguments?

The command-line arguments are passed to the program at run-time. Passing command-line arguments in a Java
program is quite easy. They are stored as strings in the String array passed to the args parameter of main() method
in Java.

To compile and run a java program in command prompt follow the steps written below.

• Save your program in a file with a .java extension

• open the command prompt and go to the directory where your file is saved.

• Run the command – javac filename.java

• After the compilation run the command – java filename

• Make sure the Java path is set correctly.

Program Code:

class Multvalue

public static void main(String args[])

System.out.println("This is muliple command line argument");

for(String value: args)

System.out.println(value);

}
Output:
Program: 3

Objective: Understand OOP concepts and basics of Java Programming.

Related Theory:

• Abstraction:
Abstraction is the process of hiding the implementation details of an object and exposing only its essential
features. This allows us to focus on what an object does, rather than how it does it.
• Encapsulation:
Encapsulation is the process of wrapping data and code together into a single unit, called a class. This
protects the data from being accessed or modified in unintended ways.
• Inheritance:
Inheritance is the process of creating a new class that inherits the properties and methods of an existing
class. This allows us to reuse code and create hierarchies of classes.
• Polymorphism:
Polymorphism is the ability of an object to take on many different forms. This allows us to write code that
can work with different types of objects without having to know the specific type of object at compile time.

Here are some of the basics of Java programming:

• Java is a class-based language:


This means that all code in Java is organized into classes. A class is a blueprint for creating objects.

• Java is an object-oriented language:


This means that Java programs are made up of objects that interact with each other.

• Java is a platform-independent language:


This means that Java code can run on any platform that has a Java Virtual Machine (JVM).

• Java is a compiled language:


This means that Java code is converted into bytecode before it is executed. Bytecode is a platform-
independent intermediate representation of Java code.

Here are some of the benefits of using OOP in Java:

• OOP makes code more modular and reusable:


We can break down our code into smaller, more manageable pieces by using classes. This makes our code
easier to understand, maintain, and reuse.
• OOP makes code more extensible:
We can extend the functionality of our code by creating new classes that inherit from existing classes. This
allows us to add new features to our code without having to rewrite the existing code.
• OOP makes code more secure:
We can protect our data from being accessed or modified in unintended ways by using encapsulation. This
makes our code more secure and reliable.
Program: 4

Objective: Create Java programs using Inheritance and polymorphism.

Related Theory:

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object.
It is an important part of OOPs (Object Oriented programming system).

The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When
you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add
new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known
as a parent-child relationship.

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is
derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So
polymorphism means many forms.

There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can
perform polymorphism in java by method overloading and method overriding.

Program Code:

class Area

void area(int r)

System.out.println("Area f circle is : " +(3.14*r*r));

void area(int l, int w)

System.out.println("area of rectangle is : " + (l*w));

public static void main(String args[])

Area obj = new Area();

obj.area(100); //compile time polymorphism

obj.area(20,30);

}
Output:

Compile-time polymorphism

Program Single-Level Inheritance:


class Employee
{
float salary=40000;

class Programmer extends Employee


{

int bonus=10000;

public static void main(String args[])


{
Programmer p = new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);

}
Output:

You might also like