Tuesday, March 10, 2020

iibscitjavalabcycleprograms

                                         7.Fibonacci Series
Aim: Write a Java program to generate fibonacci series 
import java.io.*;
import java.util.*;
class fib
{
public static void main(String args[])
{
int n,f,f1,f2,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
f1=0;
f2=1;
System.out.println("  "+f1);
System.out.println("   "+f2);
for(i=3;i<=n;i++)
{
f=f1+f2;
System.out.println("  "+f);
f1=f2;
f2=f;
|}
}
}
D:\javac fib.java
D:\java fib
Result
Enter the number    10
0
1
1
2
3
5
8
13
21
34
                               8.Multi level Inheritance

Aim: To write a JAVA program to implement multi-level Inheritance

class A
{
A()
{
System.out.println("Inside A's Constructor");
}
}
class B extends A
{
B()
{
System.out.println("Inside B's Constructor");
}
}
class C extends B
{
C()
{
System.out.println("Inside C's Constructor");
}
}
class multi
{
public static void main(String args[])
{
C c1=new C();
}
}

D:\>javac multi.java
D:\>java multi
Output:
Inside A's Constructor
Inside B's Constructor
Inside C's Constructor

                                                    9. Interface

Aim: To write a JAVA program to implement Interface.


interface A
{
void display();
}
class B implements A
{
public void display()
{
System.out.println("B's method");
}
}
class C extends B
{
public void callme()
{
System.out.println("C's method");
}
}
class inter
{
public static void main(String args[])
{
C c1=new C();
c1.display();
c1.callme();
}
}
 
D:\java inter.java
D:\java inter
Output:
B's method
C's method
                        10.String operation 

class stringoperation 
{
public static void main(String args[])
{
String s1 ="Hello";
String s2 ="world";
System.out.println("the strings are "+s1+"and "+s2);
int len1=s1.length();
int len2=s2.length();
System.out.println("the length of "+s1+"is "+len1);
System.out.println("the length of "+s2+"is "+len2);
System.out.println("the concatenation of strings "+s1.concat(s2));
System.out.println("First character of" + s1+"is="+s1.charAt(0));
System.out.println("The upper case of " + s1+"is="+s1.toUpperCase());
System.out.println("The Lower case of " + s2+"is="+s2.toLowerCase());
}
}
d:\>javac stringoperation.java
d:\>java stringoperation 

OUTPUT
the strings are Hello and world
the length of Hello is 5
the length of world is 5
the concatenation of strings Helloworld
First character ofHello is=H
The upper case of Hello is=HELLO
The Lower case of world is=world



                                    1.Sum of Digits


Aim :  Calculate the sum of individual digits of a given number 

import java.util.*;
public class sum 
{
    public static void main(String args[])
    {
        int m, n, sum = 0;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the number:");
        m = s.nextInt();
        while(m > 0)
        {
            n = m % 10;
            sum = sum + n;
            m = m / 10;
        }
        System.out.println("Sum of Digits:"+sum);
    }
}
d:\>javac sum.java
d:\>java sum


                                           2.Exception 

Aim : Java program to divide two numbers and catch the exception, if divisor is 0


import java.util.*;

class exec
{
   public static void main(String args[])
   {int n1,n2,m;
    Scanner s = new Scanner(System.in);
     
    System.out.println("emter the number 1");
    n1=s.nextInt();
    System.out.println("enter the number 2");
    n2=s.nextInt(); 
      try{
         m=n1/n2;
         System.out.println ("Result: "+m);
      }
      catch(ArithmeticException e){
         System.out.println ("You Shouldn't divide a number by zero");
      }
   }
}
d:\>javac exec.java
d:\>java exec 

                      3.Thread 

Aim: Creating three threads using the class Thread and then running them concurrently

class B extends Thread {

public void run() {
System.out.println();
for (int i = 1; i <= 5; i++) {
System.out.println(" Thread name = "+ Thread.currentThread().getName());
}
}
public static void main(String[] args) {
B t1 = new B();
t1.start();

B t2 = new B();
t2.start();

B t3 = new B();
t3.start();
}
}
d:\>javac B.java
d:\>java B.java

                              4. Inheritance 

Aim: Write a Java program to find addition of two numbers using single inheritance     


class A
{int a=2, b=3;
    void methodA()
   {
     System.out.println("Base class method");
     System.out.println("a="+a);
     System.out.println("b="+b);
   }
}

class add extends A
{
    void methodB()
   { int c = a + b;     
     System.out.println("Child class method c= "+c );
   }
   public static void main(String args[])
   {
     add obj = new add();
     obj.methodA(); 
     obj.methodB(); 
  }
}

d:\>javac add.java
d:\>java add


                                    5. Factorial
Aim: write a Java  program to compute the factorial of a given number 


import java.util.*;
public class factorial
{
    public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
        System.out.println("emter the number");
        int num = s.nextInt();
        int f = 1;
        for(int i = 1; i <= num; ++i)
        {
            
            f = f * i;
        }
        System.out.println("Factorial="+f);
    }
}
d:\>javac factorial.java
d:\>java factorial
                            6.Applet

Aim: Write a java applet program to draw a rounded rectangle 


import java.applet.*;
import java.awt.*;
public class myapplet extends Applet
{
public void Paint(Graphics g)
{
g.drawRoundRect(10,30,120,120,2,3);
}
}

D:\>javac myapplet.java
D:\>java myapplet

D:\>notepad t1.html

<html>
<body>
<applet code=”myapplet.class” height=300 width=200>
</applet>
</body>
</html>

D:\>appletviewer t1.html





No comments:

Post a Comment