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

Java

The document provides a comprehensive guide on creating Java programs using Eclipse IDE, covering various experiments such as writing a HelloWorld program, finding prime numbers, method and constructor overloading, matrix multiplication, inheritance, polymorphism, file writing, and utilizing new Java features. Each experiment includes step-by-step instructions and code examples to help beginners understand Java programming concepts. The document serves as a practical resource for learning object-oriented programming with Java.

Uploaded by

km952838
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 views19 pages

Java

The document provides a comprehensive guide on creating Java programs using Eclipse IDE, covering various experiments such as writing a HelloWorld program, finding prime numbers, method and constructor overloading, matrix multiplication, inheritance, polymorphism, file writing, and utilizing new Java features. Each experiment includes step-by-step instructions and code examples to help beginners understand Java programming concepts. The document serves as a practical resource for learning object-oriented programming with Java.

Uploaded by

km952838
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

OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment -1
Create First Java Program in Eclipse for Java
Programming
In this experiment we will learn to develop the first project of the HelloWorld
program in Eclipse IDE. If you are a beginner in Java programming and Eclipse IDE,
this step-by-step tutorial will help you get familiar with the Eclipse IDE and we will
create first Java program in Eclipse.

And then, you will be easily able to build and run your program inside Eclipse IDE.

Prerequisites

You need to have the following two things to build a program in Eclipse IDE:

1. JDK (Java Development Kit)


2. Eclipse IDE
Downloading Eclipse IDE

Eclipse IDE is the most popular Integrated Development Environment for


developing Java applications. This IDE is a robust, feature-rich, easy-to-use, and
powerful which is the first choice of almost Java programmers in the world. Eclipse
IDE is totally free.

As of now, the latest release of Eclipse is Oxygen (version 4.7), which was released
in [Link] you need to download Eclipse.
You can install Eclipse IDE either by downloading the Eclipse Installer or package
(zip file). We would recommend you to download it by the package. Eclipse
provides various packages for different development purposes. There are two main
packages listed in Eclipse for Java:

 Eclipse IDE for Java EE Developers: For developing Java EE


applications (web applications using Servlets & JSP).
 Eclipse IDE for Java Developers: For developing Java SE applications,
which is a subset of the Java EE Developer package.
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

To start downloading the package, click on the link 32-bit or 64-bit, depending
upon the bit version of your operating system. You will see the package name
like [Link]
Now, extract this ZIP file into a directory on your computer. Install the Eclipse IDE.
Now let us start building the first project in Eclipse:

Create a Java Project


Step 1: To create a new Java project in Eclipse, go to File > New >
Project.

Step 2: The New Java Project wizard dialogue appears to let you specify
configurations for the project. Select the Java Project option in it.
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Step 3: After that, you will see the below screen. Enter the project name
as HelloWorld. After that, click the Finish button.

Step 4: It is recommended to create a package for your project. Right-click


on the project option, and select New > Package from the context menu
like this:
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Step 5: In the New Java Package dialog, enter the name of your package.
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Write Your First Java Program


Step 6: To create a new Java class under a specified package, right-click on
the package and select New > Class from the context menu:
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Step 7: The New Java Class dialogue appears, type the name of the class as
HelloWorld and choose the option to generate the main() method:

And click Finish. The HelloWorld class is generated.

Write Java Code


Step 8: Edit the generated ‘HelloWorld’ java class as per the following code.
File: [Link]

package [Link];

public class HelloWorld {

public static void main(String[] args) {

[Link]("Hello World!");

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Step 9: Run Your Code


Right-click on ‘[Link]’ and select from context menu Run As >
Java Application.

After running the program, you will get the following output:
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

We created the first java program in Eclipse to create a HelloWorld


program. Now, you can start developing your programs and applications in
Java in the Eclipse IDE.
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-2

Aim: Write a program to find prime numbers between 1 to n .

import [Link].*;

class PrimeExample

public static void main(String arg[])

int i,count;

[Link]("Enter n value : ");

Scanner sc=new Scanner([Link]);

int n=[Link]();

[Link]("Prime numbers between 1 to "+n+" are ");

for(int j=2;j<=n;j++){

count=0;

for(i=1;i<=j;i++){

if(j%i==0){

count++;

if(count==2)

[Link](j+" ");

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-3

Aim: Write a java program to illustrate the concept of class with


method overloading.

class Sum {

int sum(int x, int y)

{ return (x + y); }

int sum(int x, int y, int z)

{ return (x + y + z); }

double sum(double x, double y)

{ return (x + y); }

public class MethodOverloadingDemo {

public static void main(String args[])

Sum s = new Sum();

[Link]([Link](10, 20));

[Link]([Link](10, 20, 30));

[Link]([Link](10.5, 20.5));

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-4
Aim: Write a java program to multiply two given matrices.

public class MatrixEx {

public static void main(String args[]) {

int a[][]={{1,1,1},{2,2,2},{3,3,3}};

int b[][]={{1,1,1},{2,2,2},{3,3,3}};

//creating another matrix to store the multiplication of two matrices

int c[][]=new int[3][3]; //3 rows and 3 columns

//multiplying and printing multiplication of 2 matrices

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

c[i][j]=0;

for(int k=0;k<3;k++)

c[i][j]+=a[i][k]*b[k][j];

} //end of k loop

[Link](c[i][j]+" "); //printing matrix element

} //end of j loop

[Link](); //new line

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-5
Aim: Write a java program for Method overloading and Constructor
overloading.

a )Method overloading:
import [Link].*;

class MethodOverloadingEx {

static int add(int a, int b)

return a + b;

static int add(int a, int b, int c)

return a + b + c;

public static void main(String args[])

[Link]("add() with 2 parameters");

[Link](add(4, 6));

[Link]("add() with 3 parameters");

[Link](add(4, 6, 7));

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

b )Constructor overloading.

public class Student {

//instance variables of the class

int id;

String name;

Student(){

[Link]("this a default constructor");

Student(int i, String n){

id = i;

name = n;

public static void main(String[] args) {

//object creation

Student s = new Student();

[Link]("\nDefault Constructor values: \n");

[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);

[Link]("\nParameterized Constructor values: \n");

Student student = new Student(10, "Kalpana");

[Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-7
Aim: Create Java programs using inheritance .

class A{

void display(){

[Link]("This is class A");

class B extends A{

void show(){

[Link]("This is class B");

public class Inheritance extends B {

void msg(){

[Link]("This is class C");

public static void main(String[] args) {

Inheritance obj =new Inheritance();

[Link]();

[Link]();

[Link]();

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-8
Aim: Create Java programs using polymorphism.

class A{

void add(int a,int b){

[Link]("Sum of the number ="+(a+b));

void add(int a,int b, int c){

[Link]("Sum of the number="+(a+b+c));

public class Poltoverloading {

public static void main(String[] args) {

A obj = new A();

[Link](7,9,8);

[Link](7,8);

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-9
Aim: Construct a Java program to write into a text file.

import [Link];

import [Link];

public class WriteToFile {

public static void main(String[] args) {

try {

FileWriter myWriter = new FileWriter("[Link]");

[Link]("Files in Java might be tricky, but it is fun enough!");

[Link]();

[Link]("Successfully wrote to the file.");

catch (IOException e) {

[Link]("An error occurred.");

[Link]();

}
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

Experiment-10
Aim: create a java program using java new features

import [Link];

public class MonthSwitch {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the number of the month (1-12): ");

int month = [Link]();

[Link]("Enter the year: ");

int year = [Link]();

switch (month) {

case 1:

[Link]("January has 31 days.");

break;

case 2:

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

[Link]("February has 29 days in a leap year.");

} else {

[Link]("February has 28 days in a non-leap year.");

break;

case 3:

[Link]("March has 31 days.");

break;
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

case 4:

[Link]("April has 30 days.");

break;

case 5:

[Link]("May has 31 days.");

break;

case 6:

[Link]("June has 30 days.");

break;

case 7:

[Link]("July has 31 days.");

break;

case 8:

[Link]("August has 31 days.");

break;

case 9:

[Link]("September has 30 days.");

break;

case 10:

[Link]("October has 31 days.");

break;

case 11:

[Link]("November has 30 days.");

break;
OBJECT ORIENTED PROGRAMMING WITH JAVA BCS-452

case 12:

[Link]("December has 31 days.");

break;

default:

[Link]("Invalid month number.");

break;

You might also like