Java Lab-Report Main

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

Index

Sl. Experiment Name Page


No Number
01 A program that uses a class. 02
02 A program that declares two objects of a class. 03
03 A program that includes a method inside a class. 04
04 A program which returns a value of a method. 05
05 A program which includes method that takes parameters 06
06 A program that uses a constructor of a class. 07
07 A program that uses a parameterized constructor in a class. 08
08 A program to find the area and perimeter of a circle 9-10
09 A program that uses ‘this’ keyword. 11
10 A program uses stack (of STL). 12-14
11 Overloading methods in Java. 15-16
12 Overloading constructors in Java 17-18
13 Passing objects as parameters of a method in Java. 19
14 Passing objects as parameters of a constructor in Java. 20-21
15 Argument passing using call-by-value in Java. 22
16 Argument passing using call-by-reference in Java 23
17 Returning Objects in Java. 24-25
18 Factorial with recursion in Java. 26
19 Fibonacci series with recursion in java. 27
20 Access control in Java. 28
21 Access using static keyword in Java. 29
22 static keyword in Java. 30
23 Nested and inner class in Java. 31-32
24 Inner class in Java 33
25 Inheritance in java. 34-35
26 Accessing member & inheritance in Java. 36-37
27 Implementing inheritance using practical example in Java. 38-39
28 Reference of a superclass variable to a subclass object in Java. 40-41
29 Using “super” keyword in Java. 42-44
30 Showing method overriding in Java. 45-46
31 Java Code using abstract keyword. 47-48
32 Implementing Bubble Sort 49-50
33 Implementing Insertion Sort 51-52
34 Implementing Selection Sort 53-54
35 Implementation of queue in Java 55-58

Page | 0
Experiment No: 01
Experiment Name: Write a program that uses a class.
Objectives: A class is used in object-oriented programming to describe one or more objects.
It serves as a template for creating, or instantiating, specific objects within a program. By
creating this program, we can learn about class and apply it which templet of object or we can
say core of object-oriented programming.
Source Code:
public class NewClass {
public static void main(String[] args) {
Test ob= new Test();
System.out.println ("Value of a= "+ob.a);
System.out.println ("Value of b= "+ob.b);

}
}
class Test{
int a=25, b=27;
}
Output:
Value of a= 25
Value of b=27
Discussion:
We have created and run this program by using Jcreator and this was a successful program
execution.

Page | 1
Experiment no: 02
Experiment name: Write a program that declares two objects of a class.
Objectives: Object is the root of object-oriented programming language. By this program we
will be able to know about object. We can create and use object for better programming
application.
Source Code:
public class TwoClass {
public static void main(String[] args) {
Test ob1=new Test();
Test ob2=new Test();
ob1.a=33;
ob2.a=55;
System.out.println ("For ob1 a="+ob1.a);
System.out.println ("For ob2 a="+ob2.a);
}
}
class Test{
int a;
}
Output:
For ob1 a=33
For ob2 a=55
Discussion:
I have run this program successfully using JCreator and it was a great experience to use object
and know about it.

Page | 2
Experiment no: 03
Experiment name: Write a program that include a method inside a class.
Objectives: Method is a important topic in object oriented programming. A method is
a collection of statements that perform some specific task and return the result to the caller. A
method can perform some specific tasks without returning anything. By this program we can use
method in programming.
Source Code:
public class Demo {
public static void main(String[] args) {
Test ob= new Test();
ob.meth();
}
}
class Test{
void meth(){
System.out.println ("first time learning method");
}
}
Output:
first time learning method
Discussion:
The program has run successfully in JCreator. Overall, it was great experience to use method
first time.

Page | 3
Experiment no: 04
Experiment name: Write a program which returns a value of a method.
Objectives: By this program, we can be more clear about class, object and method. Also we can
use it in real world programming using method.
Source Code:
public class Demo {
public static void main(String[] args) {
Test ob= new Test();
int x;
x=ob.meth(15,30);
System.out.println ("This method return x="+x);
}
}
class Test{
int meth(int a, int b){
return a+b;
}
}

Output:
This method return x=45
Discussion:
It was great to use method and this is a successful java program. This program is run and
compiled by JCreator. Finally, get output successfully.

Page | 4
Experiment no: 05
Experiment name: A program which includes method that takes parameters.
Objectives: By this program we can learn about parameterized method and how pass value
inside it after creating.
Source Code:
class Box{
double height, width, depth;
double volume(){
return width*height*depth;
}
void setDim(double w, double h, double d){
width=w; height=h; depth=d;
}
}
class BoxDemo {
public static void main (String[] args) {
Box mybox = new Box();
Box mybox2 = new Box();
mybox.setDim(10,5,5);
mybox2.setDim(5,2,2);
System.out.println("Volume is: "+mybox.volume());
System.out.println("Volume is: "+mybox2.volume());
}
}
Output:
Volume is: 250.0
Volume is: 20.0

Page | 5
Discussion: By this program we have passed value in method and got volume. Overall it was a
successful program executed in Jcreator.

Experiment no: 06
Experiment name: Write a program that uses a constructor of a class.
Objectives: Through this program we can know about constructor what is it and can we use it.
Source Code:
public class Demo {
public static void main(String[] args) {
Test ob= new Test();
System.out.println ("a="+ob.a);
System.out.println ("b="+ob.b);
}
}
class Test{
int a,b;
Test(){
a=4;
b=28;
}
}

Output:
a=4
b=28

Discussion:
It was a great experience to use constructor and the program was executed in Jcreator.It was a
successful program.

Page | 6
Experiment no: 07
Experiment name: Write a program that uses a parameterized constructor in a class.
Objectives: The objective of this program is to know how parameter is used in constructor and
working process of parameterized constructor.
Source Code:
public class Demo {
public static void main(String[] args) {
Test ob= new Test(12,46);
System.out.println ("a="+ob.a);
System.out.println ("b="+ob.b);
}
}
class Test{
int a,b;
Test(int i, int j){
a=i;
b=j;
}

Output:
a=12
B=46
Discussion:
It was great to use parameter in constructor and it was a successful program. It has run in
JCreator.

Page | 7
Experiment no: 08
Experiment name: Write a program to find the area and perimeter of a circle
Objectives: The purpose of this program is to use class, object, method to create a program and
their combined working process.
Source Code:
class Circle{
double radius;
double area(){
return 3.1416*radius*radius;
}
double perimeter(){
return 2*3.1416*radius;
}
}
class CircleDemo{
public static void main (String[] args) {
Circle c1=new Circle();
double a;
double p;
c1.radius=6;
a=c1.area();
p=c1.perimeter();
System.out.println ("Area is:"+a);
System.out.println ("Perimeter is:"+p);
}
}
Output
Area is:113.0976
Perimeter is:37.6992

Page | 8
Discussion: The program was successful and created and run in Jcreator. Overall it has given a
combine of phase of working with class, object, method.

Experiment no: 09
Experiment name: Write a program that uses ‘this’ keyword.
Objectives: Main objective is to write such a java program that uses “this” keyword. To know
about this keyword, its use and working process.
Source Code:
public class Demo {
public static void main(String[] args) {
Test ob= new Test(10,20);
System.out.println ("a= "+ob.a);
System.out.println ("b= "+ob.b);
}
}
class Test{
int a,b;
Test(int a, int b){
this.a=a;
this.b=b;
}
}
Output:
a= 10
b= 20
Discussion:
This is a successful java program. We are now able to know about this keyword. This program is
run and compiled by Jcreator. Finally, get output successfully.

Page | 9
Experiment no: 10
Experiment name: Write a program uses stack (of STL).
Objectives: The purpose of this program is to implement stack using java. To know process and
using criteria.
Source Code:
class Stack{
int stck[]=new int[10];
int top;
Stack(){
top=-1;
}
void push(int item){
if(top==9)
System.out.println("Stack is full.");
else
stck[++top]=item;
}
int pop(){
if(top<0){
System.out.println("Stack underflow.");
return 0;
}
else
return stck[top--];
}
}
class Demo{
public static void main(String args[]){
Stack mystack1=new Stack();
Stack mystack2=new Stack();

Page | 10
for(int i=0;i<10;i++) mystack1.push(i);
for(int i=10;i<20;i++) mystack2.push(i);
System.out.println("Stack in mystack1:");
for(int i=0;i<10;i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0;i<10;i++)
System.out.println(mystack2.pop());
}
}

Output:
Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16
15
14

Page | 11
13
12
11
10

Discussion:
This is a successful java program. We have known how to use java in implementing stack. This
program is run and compiled by Jcreator. Finally, get output successfully.

Experiment No : 11
Experiment Name : Overloading methods in Java.
Objective : the purpose of this program is to learn about method overloading and its use. How
we can overload method and its criteria.
Source Code:
class Overload {
void test () {
System.out.println("No parameters");
}
void test (int a) {
System.out.println("a: " + a);
}
void test (int a, int b) {
System.out.println("a and b: " + a +" " + b);
}
double test (double a) {
System.out.println("double a: " + a);
return a*a;
}

Page | 12
}
class OverloadDemo {
public static void main(String args[]) {
Overload ob=new Overload();
double result;
ob.test();
ob.test(10);
ob.test(10,20);
result=ob.test(123.25);
System.out.println("Result of ob.test(123.25): "+result);
}
}
Output:
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625

Discussion:
The program was great to create and known to overloading method. We have run this program in
Jcreator. Overall programming experience was great.

Page | 13
Experiment No : 12
Experiment Name: Overloading constructors in Java.
Objective: From this program we can learn about overloading constructor. Also its use and
framework can be known. Further we can use it.
Source code:
class Box {
double height;
double width;
double depth;
Box (double h,double w,double d) {
width=w;
depth=d;
height=h;
}
Box (double len){
height=width=depth=len;
}
Box (){
height=width=depth=-1;
}
double volume () {
return width*depth*height;
}
}
class OverloadCons {
public static void main(String args[]) {
Box mybox1=new Box(10,20,15);
Box mybox2=new Box();
Box mycube=new Box(7);
Page | 14
double vol;
vol=mybox1.volume();
System.out.println("Volume of mybox1 is "+vol);
vol=mybox2.volume();
System.out.println("Volume of mybox2 is "+vol);
vol=mycube.volume();
System.out.println("Volume of mycube is "+vol);
}
}
Output :
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0

Discussion:
It was pleasure to learn about constructor overloading. The program was successful and I used
JCreator and run the program.

Page | 15
Experiment no : 13
Experiment Name: Passing objects as parameter of a method in Java.
Objective: The purpose of the program is to learn how to pass object as a parameter and its
working process and how to use in program.
Source Code:
class Test {
int a,b;
Test(int i, int j) {
a=i; b=j;
}
boolean equalTo(Test o) {
if(o.a==a && o.b==b) return true;
else return false;
}
}
class PassOb {
public static void main(String args[]){
Test ob1=new Test(100,22);
Test ob2=new Test(100,22);
Test ob3=new Test(-1,-1);
System.out.println("ob1==ob2: " +ob1.equalTo(ob2));
System.out.println("ob1==ob3: " +ob1.equalTo(ob3));
}
}

Output :
ob1==ob2: true;
ob1==ob3: false

Page | 16
Discussion :
From this program we have learn to use parameter as object in method. The program was
successful and run in JCreator.

Experiment no : 14
Experiment Name: Passing objects as parameter of a constructor in Java.
Objective : To write a program to use object as parameter in constructor and learn about its
criteria and working process.
Source Code:
class Box{
double height, width, depth;
Box(double w, double h, double d){
width=w; height=h; depth=d;
}
Box(Box ob){
width=ob.width; height=ob.height; depth= ob.depth;
}
Box(){
width=1; height=-1; depth=-1;
}
Box(double len){
width=height=depth=len;
}
double volume(){
return width*height*depth;
}
}
class BoxDemo {

Page | 17
public static void main (String[] args) {
Box mybox = new Box(10,5,5);
Box mybox2 = new Box();
Box mybox3 = new Box(7);
Box myclone=new Box(mybox);
System.out.println("Volume of mybox is: "+mybox.volume());
System.out.println("Volume of mybox2 is: "+mybox2.volume());
System.out.println("Volume of mybox3 is: "+mybox3.volume());
System.out.println("Volume of myclone is: "+myclone.volume());
}
}
Output :
Volume of mybox is: 250.0
Volume of mybox2 is: 1.0
Volume of mybox3 is: 343.0
Volume of myclone is: 250.0
Discussion :
The program was successful and I use JCreator for programming. So After doing the above study
we have understood passing objects as parameters to constructor.

Page | 18
Experiment No : 15
Experiment Name: Argument passing using call-by-value in Java.
Objective: In argument passing whenever an object is passed as an argument, an exact value
copy of the reference variable is created which points to the same location of the object in
heap memory as the original reference variable. From this program we can learn it clearly.
Source Code:
class Test {
void meth(int i,int j){
i*=2;
j/=2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob=new Test();
int a=15;
int b=20;
System.out.println("a and b before call " +a+ " " +b);
ob.meth(a,b);
System.out.println("a and b after call " +a+ " " +b);
}
}

Output:
a and b before call 15 20
a and b after call 15 20
Discussion:
It was a successful program. Created and run in JCreator. So After doing the above study we
have understood how to pass value using call by value.

Page | 19
Experiment No : 16
Experiment Name: Argument passing using call-by-reference in Java.
Objective: from this program we can learn about call by reference. Call by reference method
copies the address of an argument into the formal parameter. In this method, the address is used
to access the actual argument used in the function call. It means that changes made in the
parameter alter the passing argument.
Source Code:
class Test {
int a,b;
Test(int i,int j) {
a=i;
b=j;
}
void meth(Test o) {
o.a*=2;
o.b/=2;
}
}
class PassObRef {
public static void main(String args[]) {
Test ob=new Test(15,20);
System.out.println("ob.a and ob.a before call " +ob.a+ " " +ob.a);
ob.meth(ob);
System.out.println("ob.a and ob.b after call " +ob.a+ " " +ob.b);
}
}
Output:
ob.a and ob.b before call 15 20
ob.a and ob.b after call 30 10

Page | 20
Discussion:
It was great to know about argument passing in call by reference. The program was successful.
Created and run in JCreator.

Experiment No : 17
Experiment Name: Returning Objects in Java.
Objective: To write a program for returning objects and know about how to return object and its
using process.
Source Code:
class Test{
int a;
Test(int i){
a=i;
}
Test IncrByTen(){
Test temp = new Test(a+10);
return temp;
}
}
class RetOb {
public static void main(String args[]){
Test ob1 = new Test(2);
Test ob2;
ob2=ob1.IncrByTen();
System.out.println("ob1.a="+ob1.a);
System.out.println("ob2.a="+ob2.a);
ob2=ob2.IncrByTen();
System.out.println("ob2.a after second increase="+ob2.a);
}

Page | 21
}
Output:
ob1.a : 2
ob2.a : 12
ob2.a after second increase : 22
Discussion:
So After doing the above study we have learn how to return objects. The program was useful and
we run it in JCreator.

Experiment no : 18
Experiment Name: Factorial recursion in Java.
Objective : from the program we can learn about recursion process and find the factorial of a
number using recursion.
Source Code:
class Factorial {
int fact(int n) {
int result; if(n==1) return 1;
result = fact(n-1) * n;
return result;
}
}
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}

Page | 22
Output:
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
Discussion:
So After doing the above study we can find factorial using java. The program was easy to learn
and a successful program.

Experiment No: 19
Experiment Name: Fibonacci series with recursion in Java.
Objective: Our objective is to create a Fibonacci series with recursion using Java and know its
criteria.
Source Code:
class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print("First " + n + " : ");

for (int i = 1; i <= n; ++i) {


System.out.print(a + " , ");
int sum = a + b;
a = b;
b = sum;
}
}
}
Output:
First 10: 0 , 1 ,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

Page | 23
Discussion:
The program was successful. It was run in JCreator. So after doing above code we can now
create Fibonacci series using Java
Experiment no : 20
Experiment Name: Access control in Java.
Objective : Access control is a mechanism, an attribute of encapsulation which restricts the
access of certain members of a class to specific parts of a program. For more clear concept and to
find the difference between public and private.
Source Code:
class Test {
int a;
public int b;
private int c;
void setc(int i) {
c = i;
}
int getc() {
return c;
}
}
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
ob.a = 10;
ob.b = 20;
ob.setc(100);
System.out.println("a, b, and c: " + ob.a + " " + ob.b + " " + ob.getc());
}
}

Page | 24
Output:
a, b, and c : 10 20 100
Discussion:
The program was successful and we have learnt about access control in java. So After doing the
above study we have understood the difference between public and private.
Experiment no: 21
Experiment Name: Access using static keyword in Java.
Objective : From this program we will have a clear concept about access using static keyword.
Further we can use static keyword.
Source Code:
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
System.out.println("b = " + StaticDemo.b);
}
}
Output:
a = 42
b = 99
Discussion:
It was a successful program. We have run it in JCreator. So After doing the above study we have
understood access using static keyword.

Page | 25
Experiment no : 22
Experiment Name: Static keyword in Java.
Objective: From this program we will have a clear concept about static keyword. Further we can
use static keyword. To write a program using static keyword.
Source Code:
class UseStatic {
static int a = 3;
static int b;
static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
Output:
Static block initialized.
x = 42
a=3
b = 12
Discussion :
The program was a concept of static keyword. So After doing the above study we have
understood the use of static keyword. Overall it was easy to learn.

Page | 26
Experiment no : 23
Experiment Name: Nested and inner class in Java.
Objective: To write a program using nested and inner class. Java inner class or nested class is a
class that is declared inside the class or interface. We use inner classes to logically group classes
and interfaces in one place to be more readable and maintainable. The program will clear the
concept more.
Source Code:
class Outer {
int outer_x = 100;
void test() {
for(int i=0; i<10; i++) {
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
Inner inner = new Inner();
inner.display();
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}

Page | 27
Output:
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x = 100
display: outer_x =100
display: outer_x = 100
display: outer_x = 100

Discussion :
It was a successful program. So After doing the above study we have understood nested and
inner class. The using process and its criteria.

Page | 28
Experiment no : 24
Experiment Name: Inner class in Java.
Objective: To write a program using nested and inner class. Java inner class or nested class is a
class that is declared inside the class or interface. We use inner classes to logically group classes
and interfaces in one place to be more readable and maintainable. The program will clear the
concept more.
Source Code:
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Output:
display: outer_x = 100
Discussion :
It was a successful program. The using process and its criteria. So After doing the above study
we have understood inner class.

Page | 29
Experiment No : 25
Experiment Name: Inheritance in Java.
Objective: Inheritance in Java is a mechanism in which one object acquires all the properties
and behaviors of a parent object. Objectives of this experiment is to implement a program using
inheritance. To know about its criteria.
Source Code:
class Sample{
int i,j;
void showij()
{
System.out.println(i+" "+j);
}
}
class B extends Sample
{
int k;
void showk()
{
System.out.println(k);
}
void sum()
{
System.out.println((i+j+k));
}
}
public class SimpleInheritance{
public static void main(String [] args){
Sample superOb = new Sample();
B subOb = new B();
superOb.i=10;

Page | 30
superOb.j=20;
System.out.println("Contents of superob: ");
superOb.showij();
System.out.println();
subOb.i=7;
subOb.j=8;
subOb.k=9;
System.out.println("Contents of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum of i, j and k in subob:");
subOb.sum();
}
}
Output:
Contents of superob:
10 20
Contents of subOb:
78
9
Sum of i, j and k in subob:
24

Discussion:
This is a successful program. It was run in JCreator. So, After doing the above study we have
learn about inheritance in Java.

Page | 31
Experiment No : 26
Experiment Name: Accessing member & inheritance in Java.
Objective: From this program we can learn about accessing member in inheritance. Objectives
of this experiment is to access a member using inheritance.
Source Code:
class Sample{
int i;
private int j;
void setij(int x,int y)
{
i=x;
j=y;
}
}
class B extends Sample {
int total;
void sum()
{
total = i+ j;
}
}
class Access {
public static void main(String args[])
{
B subOb = new B();
subOb.setij(10,12);
subOb.sum();
System.out.println("Total is "+subOb.total);
}
}

Page | 32
Output :
Error: j has private access in Sample
Discussion: The program is successful. It run in JCreator. So After doing the above study we
have learn accessed member of inheritance in Java.

Experiment no : 27
Experiment Name: Implementing inheritance using practical example in Java.
Objective : From this program we can learn how to implement inheritance with practical
knowledge and a clear view.
Source Code:
class Box {
double height;
double width;
double depth;
Box(Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w,double h,double d) {
width=w;
height=h;
depth=d;
}
Box() {
width=height=depth=-1;
}
Box(double len) {
width = height = depth = len;
}

Page | 33
double volume() {
return width*height*depth;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(double w,double h,double d,double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10,20,15,34.3);
BoxWeight mybox2 = new BoxWeight(2,3,4,0.076);
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is "+vol);
System.out.println("Weight of mybox1 is "+mybox1.weight);
vol = mybox2.volume();
System.out.println("Volume of mybox2 is "+vol);
System.out.println("Weight of mybox2 is "+mybox2.weight);
}
}
Output :
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0

Page | 34
Weight of mybox2 is 0.076
Discussion : It was a successful program. So After doing the above study we have used
constructor as well as inheritance using practical example of box.

Experiment No : 28
Experiment Name: Reference of a superclass variable to a subclass object in Java.
Objective: From this program we can learn about superclass and subclass reference. Objectives
of this experiment is to make a reference of a subclass object by a superclass variable.
Source Code:
class Box {
double height;
double width;
double depth;
Box (Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box (double w,double h,double d) {
width=w;
height=h;
depth=d;
}
Box () {
width=height=depth=-1;
}
Box (double len) {
width = height = depth = len;
}
double volume() {

Page | 35
return width*height*depth;
}
}
class BoxWeight extends Box {
double weight;
BoxWeight(double w,double h,double d,double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(10,20,15,34.3);
Box plainbox = new Box ();
double vol;
vol = weightbox.volume();
System.out.println("Volume of weightbox is :"+vol);
System.out.println("Weight of weightbox is :"+weightbox.weight);
plainbox = weightbox;
vol = plainbox.volume();
System.out.println("Volume of plainbox is :"+vol);
}
}
Output:
Volume of weightbox is :3000.0
Weight of weightbox is :34.3
Volume of plainbox is :3000.0

Page | 36
Discussion:
So, After doing the above study we have learned to make a reference of a subclass object with
superclass. Overall, it was a successful program.

Experiment No : 29
Experiment Name: Using “super” keyword in Java.
Objective: The super keyword in Java is a reference variable which is used to refer immediate
parent class object. Objective of this experiment is to write a program using “super” keyword
and learn about it.
Source Code:
class Box {
private double width;
private double height;
private double depth;
Box (Box ob) {
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box (double w, double h, double d) {
width = w;
height = h;
depth = d;

}
Box (){
width=-1;
height = -1;
depth = -1;
}
Box (double len){

Page | 37
width = height = depth = len;
}
double volume() {
return width*height*depth;
}
}
class BoxWeight extends Box{
double weight;
BoxWeight(BoxWeight ob){
super(ob);
weight = ob.weight;
}
BoxWeight(double w, double h, double d, double m) {
super(w, h, d);
weight = m;
}
BoxWeight() {
super();
weight =-1;
}
BoxWeight(double len,double m){
super(len);
weight = m;
}
}
class DemoSuper{
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10,20,15,34.3);
BoxWeight mybox2 = new BoxWeight();
BoxWeight mycube = new BoxWeight(3, 2);

Page | 38
double vol;
vol = mybox1.volume();
System.out.println("Volume of mybox1 is "+vol);
System.out.println("Weight of mybox1 is "+mybox1.weight);
vol=mybox2.volume();
System.out.println ("Volume of mybox2 is "+vol);
System.out.println ("Weight of mybox2 is "+mybox2.weight);
vol=mycube.volume();
System.out.println ("Volume of mycube is "+vol);
System.out.println ("Weight of mycube is "+mycube.weight);
}
}
Output:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is -1.0
Weight of mybox2 is -1.0
Volume of mycube is 27.0
Weight of mycube is 2.0
Discussion:
After doing the above study we are acknowledged “super” keyword successfully. The program
was easy to learn. It executed in JCreator.

Page | 39
Experiment No : 30
Experiment Name: Showing method overriding in Java.
Objective: If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java. Objectives of this experiment is to show method overriding
in Java and learn about it.
Source Code:
class Sample {
int i,j;
Sample(int a,int b){
i=a;
j=b;
}
void show(){
System.out.println("i and j : " +i+ " "+j);
}
}
class B extends Sample{
int k;
B(int a, int b, int c){
super(a,b);
k=c;
}
void show(){
System.out.println("K: " +k);
}
}
class Override {
public static void main(String args[]) {
B subOb =new B(1,2,3);
subOb.show();

Page | 40
}
}
Output:
K: 3
Discussion: We have run this code in JCreator. So after writing above code we’ve successfully
done method overriding.

Experiment No : 31
Experiment Name: Java Code using abstract keyword.
Objective: The abstract keyword is used to achieve abstraction in Java. It is a non-access
modifier which is used to create abstract class and method. Objectives of this experiment is to
know and learn about the uses of ‘abstract’ keyword.
Source Code:
abstract class Figure {
double dim1,dim2;
Figure(double d1,double d2) {
dim1=d1;
dim2=d2;
}
abstract area();
}
class Rectangle extends Figure {
Rectangle(double a, double b)
{
super(a,b);
}
double area() {
return dim1*dim2;
}
}
class Triangle extends Figure {

Page | 41
Triangle(double a , double b) {
super(a,b);
}
double area() {
return dim1*dim2;
}
}
class Main {
public static void main(String args[]) {
Rectangle r = new Rectangle(2,5);
Triangle t = new Triangle(5,6);
Figure figref;
figref = t;
System.out.println(figref.area());
figref = r;
System.out.println(figref.area());
}
}

Output:
No output
Discussion:
It is a successful program. Here we can see the uses of “abstract” keyword and there’s no output
because we have just used an abstraction of area() .

Page | 42
Experiment No: 32
Experiment Name: Implementing Bubble Sort.
Objectives: Bubble sorts are used to sort lists in ascending or descending order. They work by
comparing adjacent values and swapping them if they are in the wrong order. Objective of this
program is to learn Implementation Bubble Sort.

Source Code:
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int n, i, j, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (i = 0; i < n; i++)
array[i] = in.nextInt();
for (i = 0; i < ( n - 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1])
/* For descending order use < */ {
swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
System.out.println("Sorted list of numbers");

Page | 43
for (j = 0; j < n; j++)
System.out.print(array[j]+" ");
System.out.println("\n");
}
}
Output:
Input number of integers to sort
5
Enter 5 integers
4
5
2
3
1
Sorted list of numbers
12345
Discussion:
This is a successful program run id JCreator. Completing the above study, we have implemented
bubble sort in java and learned it.

Page | 44
Experiment No : 33
Experiment Name: Implementing Insertion Sort.
Objectives: Insertion sort is a simple sorting algorithm that allows for efficient, in-place sorting
of the array, one element at a time.Objective of this program is to learn Implementation Insertion
Sort.
Source Code:
import java.util.Scanner;
class InsertionSort{
public static void main(String[] args){
int n, i, j, element;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of Array: ");
n = scan.nextInt();
int[] arr = new int[n];
System.out.println("Enter " +n+ " Elements: ");
for(i=0; i<n; i++)
arr[i] = scan.nextInt();
for(i=1; i<n; i++)
{
element = arr[i];
for(j=(i-1); j>=0 && arr[j]>element; j--)
arr[j+1] = arr[j];
arr[j+1] = element;
}
System.out.println("\nThe new sorted array is: ");
for(i=0; i<n; i++)
System.out.print(arr[i]+ " ");
System.out.print("\n");
}

Page | 45
}
Output :
Enter the Size of Array:
5
Enter 5 Elements:
1
4
3
2
5

The new sorted array is:


12345
Discussion:
This is a successful program. Completing the above study we have implemented insertion sort in
java. It is created and run in JCreator.

Page | 46
Experiment No: 34
Experiment Name: Implementing Selection Sort.
Objectives: The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the beginning.
Objective of this program is to learn Implementation Selection Sort.
Source Code:
import java.util.Scanner;
class SelectionSort{
public static void main(String[] args){
int n, i, j, count, small, index=0, x;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of Array: ");
n = scan.nextInt();
int[] arr = new int[n];
System.out.println("Enter " +n+ " Elements for the Array: ");
for(i=0; i<n; i++)
arr[i] = scan.nextInt();
for(i=0; i<(n-1); i++){
count=0;
small = arr[i];
for(j=(i+1); j<n; j++){
if(small>arr[j])
{
small = arr[j];
count++;
index = j;
}
}
if(count!=0)

Page | 47
{
x = arr[i];
arr[i] = small;
arr[index] = x;
}
}
System.out.println("The new sorted array is: ");
for(i=0; i<n; i++)
System.out.print(arr[i]+ " ");
System.out.println("\n");
}
}
Output :
Enter the Size of Array:
5
Enter 5 Elements for the Array:
7
3
2
1
4
The new sorted array is:
12347
Discussion:
Completing the above study, we have implemented selection sort in java. It was a nice program
to learn. Program is created and run in JCreator.

Page | 48
Experiment No : 35
Experiment Name: Implementation of queue in Java.
Objectives: Objective of this program is to learn Implementation of queue in Java. Learn about
queue and its working process.
Source Code:
import java.util.*;
class Queue{
public static void main(String args[])
{
Qu1 obj=new Qu1();
obj.enqueue();
obj.enqueue();
obj.enqueue();
obj.enqueue();
obj.enqueue();
obj.enqueue();
obj.enqueue();
//obj.dequeue();
//obj.dequeue();
obj.show();
}
}

class Qu1 {
final int CAPACITY=6;
int qu[]=new int[CAPACITY];
int n=CAPACITY;
int front=-1;
int rear=-1;

Page | 49
void enqueue(){
System.out.println("Enter the element");
Scanner sr=new Scanner(System.in);
int x=sr.nextInt();
if(front==0 && rear==CAPACITY-1){
System.out.println("Queue is full");
}
else{
if(front==-1)
{
front=0;
}
rear++;
qu[rear]=x;
}
}
int dequeue()
{
int x;
if(front==-1|| front>rear)
{
System.out.println("Queue underflow");
}
else
{
x = qu[front];
if ( front == rear)
{

Page | 50
front = -1;
rear = -1;
}
else
front++;
}
return front;
}
void show(){
if(front==-1)
{
System.out.println("Queue is Empty");
}
else {
System.out.println("Element on queue");
for(int i=front;i<=rear;i++)
System.out.println(qu[i]);
}
}
}

Page | 51
Output:
Enter the element
24
Enter the element
25
Enter the element
54
Enter the element
56
Enter the element
44
Enter the element
23
Enter the element
1
Queue is full
Element on queue
24
25
54
56
44
23
Discussion:
This is a successful program. From above study we have implemented queue in Java. This
program created using JCreator.

Page | 52

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy