1. ALPHABETICAL TRIANGLE
Source Code: Typecast.java
public class Typecast
{
public static void main(String args[])
{
int a=65; //value of A in ASCII
for(int i=0;i<26;i++)
{
System.out.println(" ");
for(int j=0;j<=i;j++)
{
char c=(char)a;
System.out.print(c);
}
a++;
}
}
}
Output:
2.MATRICES MULTIPLICATION
Source Code: Matmul.java
import java.util.*;
public class Matmul
{
public static void main(String args[])
{
int n;
Scanner s=new Scanner(System.in);
System.out.println("Enter the base of the sq matrics:");
n=s.nextInt();
int a[][]= new int[n][n];
int b[][]= new int[n][n];
int c[][]= new int[n][n];
System.out.println("Enter the elements of 1st matrix in rowwise \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=s.nextInt();
}
}
System.out.println("Enter the elements of 2nd matrix in rowwise \n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
b[i][j]=s.nextInt();
}
}
System.out.println("Multiplying the matrices ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
c[i][j]=0;
c[i][j]=0;
for(int k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
Output:
3.CONSTRUCTOR OVERLOAD
Source Code: ConstOver.java
class Rectco
{
int l,b;
Rectco(int x,int y)
{
l=x;
b=y;
}
Rectco(int x)
{
l=b=x;
}
Rectco()
{
l=25;
b=10;
}
float getdata()
{
return(l*b);
}
}
class ConstOver
{
public static void main(String args[])
{
Rectco r1=new Rectco();
Rectco r2=new Rectco(200);
Rectco r3=new Rectco(700,250);
System.out.println("Area of 1st Rect is "+r1.getdata());
System.out.println("Area of 2nd Rect is "+r2.getdata());
System.out.println("Area of 3rd Rect is "+r3.getdata());
}
}
Output:
4. SINGLE INHERITANCE
Source Code: Programmer.java
class Employee
{
{
float salary=40000;
}
class Programmer extends Employee
{
{
int bonus=10000;
public static void main(String args[])
{
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
Output:
5. HIRARCHICAL INHERITANCE
Source Code: inheritance.java
class Shape
{
public int l=10;
}
class sq extends Shape
{
void area()
{
int area=l*l;
System.out.println("Area of sq is "+area);
}
}
class rect extends Shape
{
void area()
{
int b=7;
int area=l*b;
System.out.println("Area of Rect is "+area);
}
}
class circle extends Shape
{
void area()
{
float area=3.14F*l*l;
System.out.println("Area of circle is "+area);
}
}
class inheritance
{
public static void main(String args[])
{
sq s=new sq();
rect r=new rect();
circle c=new circle();
s.area();
r.area();
c.area();
}
}
{
public int l=10;
}
class sq extends Shape
{
void area()
{
int area=l*l;
System.out.println("Area of sq is "+area);
}
}
class rect extends Shape
{
void area()
{
int b=7;
int area=l*b;
System.out.println("Area of Rect is "+area);
}
}
class circle extends Shape
{
void area()
{
float area=3.14F*l*l;
System.out.println("Area of circle is "+area);
}
}
class inheritance
{
public static void main(String args[])
{
sq s=new sq();
rect r=new rect();
circle c=new circle();
s.area();
r.area();
c.area();
}
}
Output:
6. THREAD IN APPLET
Source Code: ThreadApplet.java
import java.applet.Applet;
import java.awt.Graphics;
public class ThreadApplet extends Applet
{
public void paint(Graphics g)
{
int a=10,b=20,c=30,d=40;
for(int i=0;i<50;i++)
{
try
{
Thread.sleep(40);
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
g.drawRect(a,b,c,d);
a+=5;b+=5;c+=5;d+=5;
}
}
}
HTML Code: ThreadApplet.html
<html>
<head>
<applet code="ThreadApplet.class"height="728",width="1366">
</applet>
</head>
</html>
Output:
7. METHOD OVERLOADING
Source Code: TestOver.java
class Adder
{
{
static int add(int a, int b)
{
return a+b;
}
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
}
{
return a+b;
}
}
class TestOver
{
{
public static void main(String[] args)
{
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(12.3,12.6));
}
}
Output:
8. DRAWING FACE
Source Code: face.java
import java.awt.*;
import java.applet.*;
public class face extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawOval(40,40,120,150);
g.drawOval(50,75,50,20);
g.drawOval(100,75,50,20);
g.setColor(Color.black);
g.fillOval(68,81,10,10);
g.fillOval(115,81,10,10);
g.fillOval(80,100,30,30);
g.setColor(Color.blue);
g.fillArc(60,125,80,40,180,180);
g.setColor(Color.red);
g.drawOval(25,92,15,30);
g.drawOval(160,92,15,30);
}
}
HTML Code: face.html
<html>
<head>
<applet code="face.class"height="70",width="100">
</applet>
</head>
</html>
Output:
9. OLYMPIC RING
Source Code: Olympic.java
import java.awt.*;
import java.applet.*;
public class Olympic extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawOval(30,30,30,30);
g.setColor(Color.blue);
g.drawOval(50,45,30,30);
g.setColor(Color.green);
g.drawOval(70,30,30,30);
g.setColor(Color.yellow);
g.drawOval(90,45,30,30);
g.setColor(Color.black);
g.drawOval(110,30,30,30);
}
}
HTML Code: Olympic.html
<html>
<head>
<applet code="Olympic.class",width='728',height='1366'>
</applet>
</head>
</html>
Output:
10. EXCEPTION HANDLING
Source Code: excep.java
class excep
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x,y,z;
try
{
x=a;
y=0;
z=(x)/(y);
}
catch(Exception e)
{
System.out.println("division by zero");
}
y=a/(b+c);
System.out.println("y="+y);
int n[]={5,10};
try
{
x=n[0]/(b-n[0]);
}
catch(ArithmeticException e)
{
System.out.println("division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("wrong datatype");
}
y=n[1]/n[0];
System.out.println("y="+y);
}
}
Output:
11. METHOD OVERRIDING
Source Code: Override.java
class rectangle
{
public int l=10,b=10;
int area()
{
return l*b;
}
}
class cuboid extends rectangle
{
int h=10;
int area()
{
return l*b*h;
}
}
public class override
{
public static void main(String agrs[])
{
rectangle obj1=new rectangle();
cuboid obj2=new cuboid();
System.out.println("rectangle :"+obj1.area());
System.out.println("cuboid :"+obj2.area());
}
}
{
public int l=10,b=10;
int area()
{
return l*b;
}
}
class cuboid extends rectangle
{
int h=10;
int area()
{
return l*b*h;
}
}
public class override
{
public static void main(String agrs[])
{
rectangle obj1=new rectangle();
cuboid obj2=new cuboid();
System.out.println("rectangle :"+obj1.area());
System.out.println("cuboid :"+obj2.area());
}
}
Output:
12. ABSTRACT CLASS AND METHOD
Source Code: Abs.java
abstract class Bank
{
abstract int getrateofInterest();
}
class SBI extends Bank
{
int getrateofInterest()
{
return(7);
}
}
class ICICI extends Bank
{
int getrateofInterest()
{
return(8);
}
}
class Abs
{
public static void main(String args[])
{
Bank b;
b=new SBI();
System.out.println("Rate of interest SBI :"+b.getrateofInterest()+"%");
b=new ICICI();
System.out.println("Rate of interest ICICI :"+b.getrateofInterest()+"%");
}
}
Output:
13. FACTORIAL
Source Code: factorial.java
import java.io.*;
import java.util.Scanner;
public class factorial
{
public static void main(String args[])
{
int s=1;
Scanner S=new Scanner(System.in);
System.out.println("Enter the number");
int n=S.nextInt();
if(n>0)
{
for(int i=1;i<=n;i++)
{
s=i*s;
}
System.out.println("The factorial of "+n+"="+s);
}
else
{
System.out.println("0");
}
}
}
Output:
14. FIBONACCI
Source Code: fib.java
import java.util.*;
class fib
{
public static void main(String args[])
{
int n,n1,n2,n3,i;
System.out.println("Enter the limit");
Scanner input=new Scanner(System.in);
n=input.nextInt();
System.out.println(" ");
if(n<0)
{
System.out.println("Not possible");
}
else
{
n1=0;
n2=1;
System.out.print(n1+" ");
System.out.print(n2+" ");
for(i=2;i<n;i++)
{
n3=n1+n2;
System.out.print(n3+" ");
n1=n2;
n2=n3;
}
}
}
}
Output:
15. MULTIPLICATION TABLE
Source Code: Multable.java
import java.io.*;
import java.util.Scanner;
public class Multable
{
public static void main(String args[])
{
Scanner S=new Scanner(System.in);
System.out.println("Enter the number");
int n=S.nextInt();
for(int i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+(n*i));
}
}
}
Output:
16. PRIME NUMBER
Source Code: Primeno.java
import java.util.*;
class Primeno
{
public static void main(String agrs[])
{
int i,j,n,k=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the limit");
n=s.nextInt();
System.out.println("Prime No are");
for(i=1;i<=n;i++)
{
int c=0;
for(j=i;j>0;j--)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
System.out.print(i+" ");
k++;
}
}
if(k==0)
{
System.out.println("No Prime");
}
}
}
class Primeno
{
public static void main(String agrs[])
{
int i,j,n,k=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the limit");
n=s.nextInt();
System.out.println("Prime No are");
for(i=1;i<=n;i++)
{
int c=0;
for(j=i;j>0;j--)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
System.out.print(i+" ");
k++;
}
}
if(k==0)
{
System.out.println("No Prime");
}
}
}
Output:
Java Lab Program
Reviewed by admin
on
October 18, 2019
Rating:
olympic ring :applet code="Olympic.class",width='750',height='100'>
ReplyDeleteprime :import java.io.*;
ReplyDeleteimport java.util.Scanner;
import java.lang.Math.*;
public class Prime
{
public static void main(String args[])
{
int k,j,m;
Scanner S=new Scanner(System.in);
System.out.println("Enter the number Limit");
int n=S.nextInt();
for(int i=0;i<n;i++)
{
if(isPrime(i)==true)
System.out.print(i+" ");
}
}
static boolean isPrime(int n)
{
if(n<2)return false;
if(n==2)return true;
if(n%2==0)return false;
for(int d=3;d<=Math.sqrt(n);d+=2){
if(n%d==0)return false;}
return true;
}
}
Da mwone njan totte
ReplyDeletechill mwone parisha kazinjilalo athinu
ReplyDelete