Professional Documents
Culture Documents
Assignment on:
Course name: Basic Electronics
Course Code: CSE 2131
Date of submission:
1
Serial Experiment Name Page
1 Write a java program to print BSFMSTU using for loop. 2
2
EXPEREMENT NO:01
EXPEREMENT NAME: Write a java program to print BSFMSTU using for
loop.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE with for loop. From this
problem we learn how to solve a problem by using for loop.
JAVA PROGRAM
package com.mycompany.javafor;
import java.util.Scanner;
int n,i;
n=sc.nextInt();
for(i=1;i<=n;i++)
System.out.println("BSFMSTU");
input: 2
output: bsfmstu
bsfmstu
3
EXPEREMENT NO:02
EXPEREMENT NAME: Write a java program to print a teacher’s information using
class and object.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE with class and object. A
class is a collection of object and an object is intance of class. Object contain all
properties of class. Some information contain class and object call this method by using
main method .From this problem we learn how to implements class and object in java .
JAVA PROGRAM
public class a {
int age;
String name,dprt,email;
dprt="CSE";
email="sujitroy@email.com";
age=28;
System.out.println("name");
System.out.println("dprt");
System.out.println("email");
System.out.println("age");
class b{
a obj=new a();
obj.teacher();
4
}
dprt =CSE
email=sujitroy@email.com
age=28;
EXPEREMENT NO:03
EXPEREMENT NAME: Write a java program by using Single inheritance.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE . Single inheritance is a
mechanism in this process one class is inherit from only one class .First class contain
some information an second class contain other information and second class extends
first class and call this two method by main method. we learn single inheritance
implementation in java.
JAVA PROGRAM
package com.mycompany.inheritance;
System.out.println("super class");
System.out.println("sub-class");
class c{
b obj = new.b();
obj.display1();
obj.display2();
5
}
EXPEREMENT NO:04
EXPEREMENT NAME: Write a java program by using Single inheritance.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE with multilevel inheritance.
Multiple inhritance is which one class is inherit from another class . it method has ,ore then one
class and one class implements another class . To make object by the name of last class and call
it by using main method. we learn how to implements multiple inheritance in java code.
JAVA PROGRAM
package com.mycompany.a;
public class A {
void display1(){
System.out.println("first-class");
void display2(){
System.out.println("second-class");
void display3(){
System.out.println("Final-class");
6
C obj =new C();
obj.display1();
obj.display2();
obj.display3();
output: First-class
second-class
Third-class
EXPEREMENT NO:05
EXPEREMENT NAME: Write a java program by using overloading method.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE . Method overloading is a
process which have more then one class with same name but their parameter list are different.
same class 1st function contain some value which is different from another function in same
class their value is different but functuion name same. Create object in main function by using
class name call function.we learn how to implements this problem by java code.
JAVA PROGRAM
package com.mycompany.over;
int x=(a+b);
int y=(a+b+c);
7
System.out.println(r.add(1,2);
System.out.println(r.add(1,2,3);
output: 3
6
EXPEREMENT NO:06
EXPEREMENT NAME: Write a java program by using abstract method.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE . Abstract method is a
method that is declared as a base class. abstract is keyword that is used to class declaration. In
first class only abstract function is declared an second class contain come information under
same function name and extends first class . call this class by creat object with last class to main
method. we learn how to implements abstract method in java code.
JAVA PROGRAM
package com.mycompany.mavenproject4;
abstract class A {
class B extends A{
void add(){
System.out.println("Abstract class");
obj.add();
8
output: Abstract class
EXPEREMENT NO:07
EXPEREMENT NAME: Write a java program by using interface
method(multiple inheritance).
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE . Interface is just like a class
which which contain only abstract method. Interface is keyword that is used to class declaration.
Interface support multiple inheritance. In this method create more then one class and function.
Their function name is same. In last class contain all class and same function contain some
information what we want .At last create a object with last class name and call all function in
main function. we learn how to implements interface in multiple inheritance in java code.
JAVA PROGRAM
package com.mycompany.mavenproject4;
interface C {
void show();
interface D{
void show();
System.out.println("Interfce class");
obj.show();
9
}
EXPEREMENT NO:08
EXPEREMENT NAME: Write a java program by using method overriding.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE . Method overriding occurs
when a sub class has a same method as parent class. we learn how to implements method
overriding in java code.
JAVA PROGRAM
package com.mycompany.mavenproject5;
class base {
void output(){
System.out.println("super-class");
void output(){
System.out.println("Sub-class");
obj.output();
output: super-class
sub -class
10
EXPEREMENT NO:09
EXPEREMENT NAME: Write a java program to find largest number in array.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE. we learn how to find largest
number in array to get input by user in java code.
JAVA PROGRAM
package com.mycompany.mavenproject6;
import java.util.Scanner;
int i,n;
n=r.nextInt();
for(i=0;i<n;i++){
a[i]=r.nextInt();
for(i=0;i<n;i++)
if(a[0]<a[i]){
a[0]=a[i];
input:4
11
67 33 101 45 80
JAVA PROGRAM
package com.mycompany.mavenproject11;
import java.util.Scanner;
class matrix {
int i,j,r,c;
r=sc.nextInt();
c=sc.nextInt();
for(i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=sc.nextInt();
for(i=0;i<r;i++)
12
for(j=0;j<c;j++)
b[i][j]=sc.nextInt();
for(i=0;i<r;i++)
for(j=0;j<c;j++)
d[i][j]=a[i][j]+b[i][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
System.out.print(d[i][j]);
System.out.println("");
input: 2 2 2
1 2 3 4
13
2 2 2 3
output: 4 6
4 6
EXPEREMENT NO:11
EXPEREMENT NAME: Write a java program for matrix substraction.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE. we learn how to substract
two array to get input by user in java code.
JAVA PROGRAM
package com.mycompany.mavenproject11;
import java.util.Scanner;
class matrix {
int i,j,r,c;
r=sc.nextInt();
c=sc.nextInt();
for(i=0;i<r;i++)
for(j=0;j<c;j++)
a[i][j]=sc.nextInt();
14
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
b[i][j]=sc.nextInt();
for(i=0;i<r;i++)
for(j=0;j<c;j++)
d[i][j]=a[i][j]-b[i][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
System.out.print(d[i][j]);
System.out.println("");
15
}
input: 2 2 2
1 2 3 4
2 2 2 3
output: -2 -2
0 -1
EXPEREMENT NO:12
EXPEREMENT NAME: Write a java program bubble sort in array.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE. Bubble Sort is the simplest
sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the
wrong order .we learn how to implements the code in array to get input by user in java code.
JAVA PROGRAM
package com.mycompany.bubblesort;
int n,i,j,t;
n=sc.nextInt();
for(i=0;i<n;i++)
a[i]=sc. nextInt();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
16
if(a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
for(i=0;i<n;i++)
System.out.println(a[i]);
input: 5
50 30 60 10 80
output: 10 30 50 60 80
17
EXPEREMENT NO:13
EXPEREMENT NAME: Write a java program fibonacci series using array in
java .
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE. In fibonacci series, next
number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc. The
first two numbers of fibonacci series are 0 and 1. we learn how to implements fibonacci sreies in
array by java code.
JAVA PROGRAM
package com.mycompany.fibonacci;
import java.util.Scanner;
int n,i;
n=sc.nextInt();
for(i=0;i<n;i++)
a[i]=sc.nextInt();
a[0]=0;
a[1]=1;
for(i=2;i<n;i++)
a[i]=a[i-2]+a[i-1];
for(i=0;i<n;i++)
System.out.println(a[i]);
18
}
input :4
output: 0 1 1 2
EXPEREMENT NO:14
EXPEREMENT NAME: Write a java program to print pyramid in binary form.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE to print pyramid in binary
form.
JAVA PROGRAM
package com.mycompany.pyramid;
import java.util.Scanner;
public class p {
int n,i,j,k;
n=sc.nextInt();
for(i=0;i<n;i++)
for(j=2*n-i-1;j>0;j--)
System.out.println(" ");
for(k=0;k<2*i+1;k++)
if(k==0||k==2*i)
System.out.println(1);
19
else
System.out.println("0");
System.out.println();
20
EXPEREMENT NO:15
EXPEREMENT NAME: Write a java program by use try, catch, finally keyword in
exception handling.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE . The try block is used
to specify a block of code that may throw an exception. The catch block is used to
handle the exception if it is thrown. The finally block is used to execute the code after
the try and catch blocks have been executed.we learn how to implements try catch
finally keyword in exception handling.
JAVA PROGRAM
class GFG
{
public static void main (String[] args)
{
int[] arr = new int[4];
try
{
int i = arr[4];
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println("Exception caught in catch
block");
}
finally
{
System.out.println("finally block executed");
}
}
}
output
Exception caught in catch block
finally block executed
Outside try-catch-finally clause
21
22