Nothing Special   »   [go: up one dir, main page]

Java - Lab - Report Original

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

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) {
System.out.println("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 java.util.Scanner;

public class Airthmatic {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

int x,y;

int sum,mul,div,sub;

System.out.println("Enter Two Numbers = ");

x=input.nextInt();

y=input.nextInt();

sum=x+y;

mul=x*y;

div=x/y;

sub=x-y;
System.out.println("Results:");

System.out.println("Addition = "+sum);

System.out.println("Multiplication = "+mul);

System.out.println("Division = "+div);

System.out.println("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 java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{
int num, i, fact=1;
Scanner input = new Scanner(System.in);
System.out.print("Enter a Number you want: ");
num = input.nextInt();
for(i=num; i>=1; i--)
{
fact = fact*i;
}
System.out.println("\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 java.util.Scanner;
public class Fibonacci{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int num1 = 0,num2 = 1,n,sum,i;
System.out.print("Enter the number of terms: ");
n=input.nextInt();
System.out.println("First " + n + " terms of fibonnaci series: ");
for (i = 1; i <= n; ++i)
{
System.out.print(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 java.util.Scanner;
public class PalindromeString{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String s1,s2;
System.out.print("Enter a string:");
s1 = input.nextLine();
StringBuffer st = new StringBuffer(s1);
s2 = st.reverse().toString();
if(s1.equals(s2)){
System.out.println("The string "+s1+" is Palindrome");
}
else{
System.out.println("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;
System.out.println("Value of Instance variable :"
+ this.num);
System.out.println("Value of Local variable :"
+ num);
}
}
class UseTest
{
public static void main(String args[])
{
Test obj1 = new Test();
obj1.method();
}
}
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 stack.Here we will use the programming language JAVA and the
compiler Eclipse.
Program Code:
import java.util.Stack;
public class Main {
public static void main(String args[]){
Stack<Integer> stack = new Stack<>()
System.out.println("Initial stack : " + stack);
System.out.println("Is stack Empty? : " + stack.isEmpty());
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
System.out.println("Stack after push operation: " + stack);
System.out.println("Element popped out:" + stack.pop());
System.out.println("Stack after Pop Operation : " + stack);
System.out.println("Element 10 found at position: " + stack.search(10));
System.out.println("Is Stack empty? : " + stack.isEmpty());
}
}
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) {
System.out.printf("\nQueue is full\n");
return;
}
else {
queue[rear] = item;
rear++;
}
return;
}
static void queueDequeue() {
if (front == rear) {
System.out.printf("\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) {
System.out.printf("Queue is Empty\n");
return;
}
for (i = front; i < rear; i++) {
System.out.printf(" %d , ", queue[i]);
}
return;
}
static void queueFront()
{
if (front == rear) {
System.out.printf("Queue is Empty\n");
return;
}
System.out.printf("\nFront Element of the queue: %d", queue[front]);
return;
}
}
public class QueueArrayImplementation {
public static void main(String[] args) {
Queue q = new Queue(4);
System.out.println("Initial Queue:");
q.queueDisplay();
q.queueEnqueue(10);
q.queueEnqueue(30);
q.queueEnqueue(50);
q.queueEnqueue(70);
System.out.println("Queue after Enqueue Operation:");
q.queueDisplay();
q.queueFront();
q.queueEnqueue(90);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\nQueue after two dequeue operations:");
q.queueDisplay();
q.queueFront();
}
}
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);
System.out.println("integer value: " + num1);
System.out.println("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 method.In 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();
System.out.println("before change "+ob.value);
ob.change(150);
System.out.println("after change "+ob.value);
}
}
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 parameter.In 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){
ob.value=ob.value+100;
}
public static void main(String args[]){
CallByReference ob = new CallByReference();
System.out.println("before change "+ob.value);
ob.change(ob);
System.out.println("after change "+ob.value);
}
}
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 class.In this program we want to run a program using static keyword.
Program Code:

import java.util.*;
public class BlockExample{
// static variable
static int j = 13;
static int n;
// static block
static {
System.out.println("Static block initialized.");
n = j * 9;
}
public static void main(String[] args)
{
System.out.println("Inside main method");
System.out.println("Value of j : "+j);
System.out.println("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(){
System.out.println("eating...");
}
}
class Dog extends Animal{
void bark(){
System.out.println("barking...");
}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}
}
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(){
System.out.println(color); //prints color of Dog class
System.out.println(super.color); //prints color of Animal class
}
}
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}
}
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 Java.Method 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(){
System.out.println("Vehicle is running");
}
}
//Creating a child class
class Car extends Vehicle{
//defining the same method as in the parent class
void run(){
System.out.println("Car is running safely");
}
public static void main(String args[]){
Car obj = new Car();//creating object
obj.run();//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 polymorphism.We will use the programming language JAVA and the software
Eclipse to implement the code.
Program Code:
// Implementing Dynamic Method Dispatch
class Apple
{
void display()
{
System.out.println("Inside Apple's display method");
}
}
class Banana extends Apple
{
void display() // overriding display()
{
System.out.println("Inside Banana's display method");
}
}
class Cherry extends Apple
{
void display() // overriding display()
{
System.out.println("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


ref.display(); // calling Apple's version of display()

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


ref.display(); // calling Banana's version of display()

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


ref.display(); // 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:

You might also like