0% found this document useful (0 votes)
19 views21 pages

Java Programming Experiments Guide

The document describes 11 experiments related to Java programming. The experiments cover topics like Hello World program, arithmetic operations, factorials, Fibonacci series, checking palindromes, variable hiding, stacks, queues, method overloading, and call by value. For each experiment, the name, objective, program code, input/output and a discussion section is provided. The programs demonstrate basic Java concepts and data structures.

Uploaded by

N A Khan Niloy
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)
19 views21 pages

Java Programming Experiments Guide

The document describes 11 experiments related to Java programming. The experiments cover topics like Hello World program, arithmetic operations, factorials, Fibonacci series, checking palindromes, variable hiding, stacks, queues, method overloading, and call by value. For each experiment, the name, objective, program code, input/output and a discussion section is provided. The programs demonstrate basic Java concepts and data structures.

Uploaded by

N A Khan Niloy
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

Experiment No: 01

Name of Experiment: A program of Java Basic “Hello World”.


Objective: We want to run a program that will show the text ‘Hello World’.Here we will use java.
Program code:

class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}

Input and Output:

Discussion: We have successfully run the program.

Discussion: We have successfully run the program.


Experiment No: 02
Name of Experiment: A program of Addition, Subtraction, Multiplication and Division of two
numbers.
Objective: We want to run a program that will show Addition,Subtraction ,Multiplication and division of
two numbers.
Program Code:
import [Link];

public class Airthmatic {

public static void main(String[] args) {

Scanner input=new Scanner([Link]);

int x,y;

int sum,mul,div,sub;

[Link]("Enter Two Numbers = ");

x=[Link]();

y=[Link]();

sum=x+y;

mul=x*y;

div=x/y;

sub=x-y;
[Link]("Results:");

[Link]("Addition = "+sum);

[Link]("Multiplication = "+mul);

[Link]("Division = "+div);

[Link]("Subtraction = "+sub);

}
Input and Output:

Discussion: we have successfully run the program of addition,subtraction,multiplication and division of two
numbers.
Experiment No: 03
Name of Experiment: A program of Factorial of a number
Objective:we have to run a program which will show Factorial of a number. For this program we will use
JAVA and the compiler Eclipse.
Program Code:
import [Link];
public class Factorial
{
public static void main(String[] args)
{
int num, i, fact=1;
Scanner input = new Scanner([Link]);
[Link]("Enter a Number you want: ");
num = [Link]();
for(i=num; i>=1; i--)
{
fact = fact*i;
}
[Link]("\nFactorial of "+num+" is : " +fact);

}
}
Input and Output:

Discussion:We successfully run the program to execute factorial of a number.


Experiment No:04
Name of Experiment:A program of Fibonacci Series.
Objective:We have to run a program that will print Fibonacci series. For this program we
use JAVA and compiler Eclipse.
Program Code:
import [Link];
public class Fibonacci{
public static void main(String[] args)
{
Scanner input=new Scanner([Link]);
int num1 = 0,num2 = 1,n,sum,i;
[Link]("Enter the number of terms: ");
n=[Link]();
[Link]("First " + n + " terms of fibonnaci series: ");
for (i = 1; i <= n; ++i)
{
[Link](num1 + " ");
sum = num1 + num2;
num1 = num2;
num2 = sum;
}
}
}
Input and Output:

Discussion: We have successfully run the program to print fibonacci series.


Experiment No:05
Name of Experiment:A program of making Palindrome of a String
Objective:We have to make a program that will check a string is Palindrome or not. For this we use JAVA
and compiler Eclipse.
Program Code:

import [Link];
public class PalindromeString{
public static void main(String[] args){
Scanner input = new Scanner([Link]);
String s1,s2;
[Link]("Enter a string:");
s1 = [Link]();
StringBuffer st = new StringBuffer(s1);
s2 = [Link]().toString();
if([Link](s2)){
[Link]("The string "+s1+" is Palindrome");
}
else{
[Link]("The string "+s1+" is not palindrome");
}
}
}
Input and Output:

Discussion: The program run successfully and it can check a string is palindrome or not.
Experiment No:06
Name of Experiment:A simple program on Instance Variable Hiding
Objective:In Java, if there is a local variable in a method with the same name as the instance
variable, then the local variable hides the instance variable. Here we want to run a program of
instance variable hiding. For this program we use JAVA and compiler Eclipse.
Program Code:
class Test
{
int num = 33;
void method()
{
int num = 89;
[Link]("Value of Instance variable :"
+ [Link]);
[Link]("Value of Local variable :"
+ num);
}
}
class UseTest
{
public static void main(String args[])
{
Test obj1 = new Test();
[Link]();
}
}
Input and Output:

Discussion:We successfully run the programme and learned how to hide instance variable.
Experiment No:07
Name of Experiment: A program of Stack.
Objective:We want to run a program of [Link] we will use the programming language JAVA and the
compiler Eclipse.
Program Code:
import [Link];
public class Main {
public static void main(String args[]){
Stack<Integer> stack = new Stack<>()
[Link]("Initial stack : " + stack);
[Link]("Is stack Empty? : " + [Link]());
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]("Stack after push operation: " + stack);
[Link]("Element popped out:" + [Link]());
[Link]("Stack after Pop Operation : " + stack);
[Link]("Element 10 found at position: " + [Link](10));
[Link]("Is Stack empty? : " + [Link]());
}
}
Input and Output:

Discussion:We have successfully run the program of stack.


Experiment No:08
Name of Experiment:A program of Queue.
Objective: A queue is a collection of entities that are maintained in a sequence and can be modified by the
addition of entities at one end of the sequence and the removal of entities from the other end of the sequence.
.We want to run a program of queue by using the programming language JAVA and the compiler Eclipse.

Program Code:
class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue(int size) {
front = rear = 0;
capacity = size;
queue = new int[capacity];
}
static void queueEnqueue(int item) {
if (capacity == rear) {
[Link]("\nQueue is full\n");
return;
}
else {
queue[rear] = item;
rear++;
}
return;
}
static void queueDequeue() {
if (front == rear) {
[Link]("\nQueue is empty\n");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
return;
}
static void queueDisplay()
{
int i;
if (front == rear) {
[Link]("Queue is Empty\n");
return;
}
for (i = front; i < rear; i++) {
[Link](" %d , ", queue[i]);
}
return;
}
static void queueFront()
{
if (front == rear) {
[Link]("Queue is Empty\n");
return;
}
[Link]("\nFront Element of the queue: %d", queue[front]);
return;
}
}
public class QueueArrayImplementation {
public static void main(String[] args) {
Queue q = new Queue(4);
[Link]("Initial Queue:");
[Link]();
[Link](10);
[Link](30);
[Link](50);
[Link](70);
[Link]("Queue after Enqueue Operation:");
[Link]();
[Link]();
[Link](90);
[Link]();
[Link]();
[Link]();
[Link]("\nQueue after two dequeue operations:");
[Link]();
[Link]();
}
}
Input and Output:

Discussion: : We have successfully run this programme .By running this program we have learned about the
concept of Queue with java.
Experiment No:09
Name of Experiment:A simple program of Method Overloading.
Objective:In Java, it is possible to define two or more methods within the same class that share the same
name, as long as their parameter declarations are different. In this program we want to run a program of
method overloading. There will be three methods of same name but their parameters will different. We will
use the compiler Eclipse.
Program Code:

public class MethodOverloading{


static int addition(int x, int y) {
return x + y;
}
static double addition(double x, double y) {
return x + y;
}
public static void main(String[] args) {
int num1 = addition(8, 5);
double num2 = addition(4.3, 6.26);
[Link]("integer value: " + num1);
[Link]("double value: " + num2);
}
}
Input and Output:

Discussion: We have successfully run a program of method overloading. The popular programming
language Java has been used here.
Experiment No: 10
Name of Experiment: A program of call-by-value.
Objective: If we call a method passing a value, it is known as call by value. The changes being done in the
called method, is not affected in the calling [Link] this program we want to run a program call by value.
We will use the language java and the compiler Eclipse.
Program Code:
public class CallByValue{
int value=66;
void change(int value){
value=value+100;
}
public static void main(String args[]){
CallByValue ob=new CallByValue();
[Link]("before change "+[Link]);
[Link](150);
[Link]("after change "+[Link]);
}
}
Input and Output:

Discussion:: We have successfully run a program of call by value. The popular programming language Java
has been used here.
Experiment No:11
Name of Experiment: A program of Call By Reference.
Objective:Call by Reference means calling a method with a parameter as a reference. Through this, the
argument reference is passed to the [Link] this program we want to run a program call by reference.
We will use the language java and the compiler Eclipse.
Program Code:
public class CallByReference{
int value=66;
void change(CallByReference ob){
[Link]=[Link]+100;
}
public static void main(String args[]){
CallByReference ob = new CallByReference();
[Link]("before change "+[Link]);
[Link](ob);
[Link]("after change "+[Link]);
}
}
Input and Output:

Discussion: We have successfully run a program of call by reference .The popular programming language
Java has been used here.
Experiment No:12
Name of Experiment: A simple program using ‘static’ keyword.
Objective: The static keyword in Java is used for memory management mainly. We can apply static
keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an
instance of the [Link] this program we want to run a program using static keyword.
Program Code:

import [Link].*;
public class BlockExample{
// static variable
static int j = 13;
static int n;
// static block
static {
[Link]("Static block initialized.");
n = j * 9;
}
public static void main(String[] args)
{
[Link]("Inside main method");
[Link]("Value of j : "+j);
[Link]("Value of n : "+n);
}
}
Input and Output:

Discussion: We successfully run the program using static ketword and learned about the keyword.
Experiment No:13
Name of Experiment: A simple program of Inheritance.
Objective: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors
of a parent object.
Program Code:
class Animal{
void eat(){
[Link]("eating...");
}
}
class Dog extends Animal{
void bark(){
[Link]("barking...");
}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
[Link]();
}
}
Input and Output:

Discussion: We have successfully run the program of a simple inheritance.


Experiment No:14
Name of Experiment:A program of inheritance using “super” Keyword.
Objective: The super keyword in Java is a reference variable which is used to refer immediate parent class
object .
Program Code:
class Animal{
String color="Gray";
}
class Dog extends Animal{
String color="black";
void printColor(){
[Link](color); //prints color of Dog class
[Link]([Link]); //prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
}
}
Input and Output:

Discussion: we have successfully run the program of inheritance using ‘super’ keyword.
Experiment No: 15
Name of Experiment: A program of Method Overriding.
Objective: If subclass (child class) has the same method as declared in the parent class, it is known as
method overriding in [Link] overriding is used to provide the specific implementation of a method
which is already provided by its superclass.
We want to run a program about method overriding. For this program we use JAVA and compiler Eclipse.
Program Code:
//Creating a parent class.
class Vehicle{
//defining a method
void run(){
[Link]("Vehicle is running");
}
}
//Creating a child class
class Car extends Vehicle{
//defining the same method as in the parent class
void run(){
[Link]("Car is running safely");
}
public static void main(String args[]){
Car obj = new Car();//creating object
[Link]();//calling method
}
}
Input and Output:

Discussion: We have successfully run the program of method overriding and learned about it.
Experiment No:16
Name of Experiment: A program of Dynamic Method dispatch
Objective: Dynamic method dispatch is the mechanism in which a call to an overridden method is
resolved at run time instead of compile time. This is an important concept because of how Java
implements run-time [Link] will use the programming language JAVA and the software
Eclipse to implement the code.
Program Code:
// Implementing Dynamic Method Dispatch
class Apple
{
void display()
{
[Link]("Inside Apple's display method");
}
}
class Banana extends Apple
{
void display() // overriding display()
{
[Link]("Inside Banana's display method");
}
}
class Cherry extends Apple
{
void display() // overriding display()
{
[Link]("Inside Cherry's display method");
}
}
class Fruits_Dispatch
{
public static void main(String args[])
{
Apple a = new Apple(); // object of Apple
Banana b = new Banana(); // object of Banana
Cherry c = new Cherry(); // object of Cherry

Apple ref; // taking a reference of Apple

ref = a; // r refers to a object in Apple


[Link](); // calling Apple's version of display()

ref = b; // r refers to a object in Banana


[Link](); // calling Banana's version of display()

ref = c; // r refers to a object in Cherry


[Link](); // calling Cherry's version of display()
}
}
Input and Output:

Discussion:
We have successfully run the program dynamic method dispatch.
Experiment No: 17
Name of Experiment: A program of executing the area of a rectangle, circle and a triangle using
abstract class.

Objective: To write a java program to calculate the area of rectangle, circle


and triangle using the concept of an abstract class.
Program Code:
Input and Output:
Discussion:

Common questions

Powered by AI

Method overriding allows a subclass to provide a specific implementation of a method already defined in its superclass. In the example, `Car` overrides the `run()` method from `Vehicle`, enabling it to execute `Car's` version of the method instead of `Vehicle's`. This allows `Car` to have a behavior differing from its parent class, exemplifying polymorphism in object-oriented programming .

The Fibonacci program asks the user for the number of terms to generate. It initializes the first two terms, `num1` and `num2`, as 0 and 1. In a loop, it calculates the next term `sum` as the sum of `num1` and `num2`, prints `num1`, and then updates `num1` and `num2` for the next iteration. This continues for `n` terms, producing the sequence up to the number specified by the user .

Java implements stack operations using the `Stack` class. It provides methods like `push()` for adding elements, `pop()` for removing the top element, and `search()` for finding an element's position. In the example, the stack is initially empty, elements are pushed and then popped, demonstrating dynamic changes to its content. The program outputs the stack before and after operations, showing usage of these methods and checking if the stack is empty .

Understanding instance variable hiding is crucial for preventing unexpected behaviors in Java due to naming conflicts between local and instance variables. The provided program illustrates this by defining both a local and an instance variable named `num`. The local `num` in the `method()` function hides the instance variable, and the output reflects this by separately displaying their values. It teaches how to access the instance variable using `this.num`, distinguishing it from the local scope variable .

To write and run a 'Hello, World!' program in Java, create a class named `HelloWorld` with a `main` method. Inside this method, use `System.out.println("Hello, World!")` to print the message to the console. Compile and run the program using a Java compiler such as Eclipse to see the output .

The Java program shows method overloading by defining two `addition` methods in the `MethodOverloading` class, each with different parameter types—one accepts `int` and the other `double`. This allows the correct method version to be called based on the types of arguments passed during invocation, thus facilitating polymorphic behavior at compile-time .

Dynamic method dispatch in Java allows a call to an overridden method to be resolved at runtime, enabling run-time polymorphism. The implementation achieves this by creating a superclass `Apple` with a method `display`, which is overridden in two subclasses `Banana` and `Cherry`. An `Apple` reference variable is used to point to objects of these subclasses, and the method calls are resolved dynamically at runtime based on the actual object’s type, demonstrating run-time polymorphism .

The process starts with defining an abstract class that includes an abstract method for calculating area. Subclasses like `Rectangle`, `Circle`, and `Triangle` inherit from this abstract class and provide specific implementations of the abstract method to calculate their areas. This enables different shapes to have their methods for area calculation while sharing the same interface, demonstrating the power of abstraction and code reusability in Java .

The program demonstrates the usage of the Scanner class by creating an instance called `input` to capture user input from the console. Two integers, `x` and `y`, are read using `input.nextInt()`. These values are then used to perform arithmetic operations like addition, subtraction, multiplication, and division, and the results are printed .

The 'super' keyword in Java is important for accessing members of the superclass that are hidden by subclass members. In the given example, the `Dog` class overrides a variable `color` present in `Animal`. By using `super.color`, the subclass can access the `color` variable from the `Animal` class, which is crucial when the subclass has a variable with the same name, thus preserving parent class data .

You might also like