You are on page 1of 22

Department of Computer Science and Engineering

Bangamata Sheikh Fojilatunnesa Mujib Science & Technology


University

Assignment on:
Course name: Basic Electronics
Course Code: CSE 2131

Submitted by: Submitted to:


Nusrat Jahan Sujit Roy
ID: 21211103, Session:2020-21 Assistant Professor
Dept. of Computer Science &Engineering Dept. of Computer Science &Engineering
Bangamata Sheikh Fojilatunnesa Mujib Bangamata Sheikh Fojilatunnesa Mujib
Science & Technology University, Science & Technology University,
Jamalpur Jamalpur

Date of submission:

1
Serial Experiment Name Page
1 Write a java program to print BSFMSTU using for loop. 2

2 Write a java program to print a teacher’s information using 3


class and object.
3 Write a java program by using Single inheritance. 4

Write a java program by using Single inheritance. 5


4
5 Write a java program by using overloading method. 6

6 Write a java program by using abstract method. 7

Write a java program by using interface method(multiple 8


7 inheritance).
Write a java program by using method overriding. 9
8
9 Write a java program to find largest number in array. 10

10 Write a java program for matrix addition. 11

11 Write a java program for matrix substraction. 13

12 Write a java program bubble sort in array. 15

13 Write a java program fibonacci series using array in java . 17

14 Write a java program to print pyramid in binary form. 18

15 Write a java program by use try, catch, finally keyword in 20


exception handling.

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;

public class Javafor {

public static void main(String[] args) {

int n,i;

Scanner sc = new Scanner(System.in);

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 {

public void teacher(){

int age;

String name,dprt,email;

name ="Sujit Roy";

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{

public static void main (String[]args){

a obj=new a();

obj.teacher();

4
}

output: name =Sujit Roy

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;

public class Inheritance {

public void display1(){

System.out.println("super class");

class b extends Inheritance{

public void display2()

System.out.println("sub-class");

class c{

public static void main(String[] args) {

b obj = new.b();

obj.display1();

obj.display2();

5
}

output: super class


sub class

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");

public class B extends A{

void display2(){

System.out.println("second-class");

public class C extends B{

void display3(){

System.out.println("Final-class");

public static void main(String[] args) {

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;

public class Over {

public void add(int a, int b){

int x=(a+b);

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

int y=(a+b+c);

public class overloadding{

public static void main(String[] args) {

over r=new over();

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 {

abstract void add();

class B extends A{

void add(){

System.out.println("Abstract class");

public static void main(String[] args){

A obj = new B();

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();

class multiple implements C,D

public void show()

System.out.println("Interfce class");

public static void main (String[]args)

multiple obj=new multiple();

obj.show();

9
}

output: interface class

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");

class sub extends base{

void output(){

System.out.println("Sub-class");

public static void main(String[] args){

sub obj =new sub();

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;

public class array {

public static void main(String[] args){

int i,n;

Scanner r=new Scanner(System.in);

n=r.nextInt();

int a[]=new int[n];

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];

System.out.println("largest number is="+a[0]);

input:4

11
67 33 101 45 80

output: 101 is largest number


EXPEREMENT NO:10
EXPEREMENT NAME: Write a java program for matrix addition.
SOLVING TECHNIQUE: To solve this problem i used NetBeans IDE. we learn how to add two
array of a matrix get input by user in java code.

JAVA PROGRAM

package com.mycompany.mavenproject11;

import java.util.Scanner;

class matrix {

public static void main(String[] args){

int i,j,r,c;

Scanner sc= new Scanner(System.in);

r=sc.nextInt();

c=sc.nextInt();

int a[][]=new int[r][c];

int b[][]=new int[r][c];

int d[][]=new int[r][c];

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 {

public static void main(String[] args){

int i,j,r,c;

Scanner sc= new Scanner(System.in);

r=sc.nextInt();

c=sc.nextInt();

int a[][]=new int[r][c];

int b[][]=new int[r][c];

int d[][]=new int[r][c];

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;

import java .util.Scanner;

public class bubble {

public static void main(String[]args){

int n,i,j,t;

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

int a[]=new int[n];

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;

public class fibo {

public static void main(String[]args){

int n,i;

Scanner sc=new Scanner(System.in);

n=sc.nextInt();

int a[]= new int[n];

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 {

public static void main(String[]args){

int n,i,j,k;

Scanner sc= new Scanner(System.in);

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

You might also like