0% found this document useful (0 votes)
22 views

Java Lab Manual

Uploaded by

Ihtisham Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Lab Manual

Uploaded by

Ihtisham Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Program 1: Matrix Addition

import java.util.Scanner;

public class MATRIX {


public static void main(String args[])
{
int m = Integer.parseInt(args[0]);
int n = Integer.parseInt(args[1]);
int A[][] = new int[m][n];
int B[][] = new int[m][n];
int C[][] = new int[m][n];
Scanner in = new Scanner(System.in);
System.out.println("Enter the elements of Matrix A: ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
A[i][j] = in.nextInt();
}
}
System.out.println("Enter the elements of Matrix B: ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
B[i][j] = in.nextInt();
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
C[i][j] = A[i][j] + B[i][j];
}
}
System.out.println("Elements of Matrix C: ");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(C[i][j]+" ");
}
System.out.println();
}

}
}
Program 2: Stack Operations

import java.util.Scanner;

class stack
{
int top = -1;
int stk[]=new int[5];
void push(int ele)
{
if(top==stk.length-1)
{
System.out.println("Stack is full ");
return;
}
stk[++top]=ele;
System.out.println(ele+" is pushed succesfully ");
}
void pop()
{
if(top==-1)
{
System.out.println("Stack is empty");
return;
}
System.out.println(" Succesfully Popped "+stk[top--]);
}
void display()
{
if(top==-1)
{
System.out.println("Stack is empty");
return;
}
System.out.println("Element of stark: ");
for(int i=top;i>=0;i--)
System.out.println(stk[i]);
}
}
public class Prog2 {
public static void main(String args[])
{
stack ob=new stack();
Scanner in = new Scanner(System.in);
while(true)
{
System.out.println(" 1.Push 2.Pop 3.Display 4.Exit");
int ch=in.nextInt();
switch(ch)
{
case 1: System.out.println("Enter the element to be inserted");
int element = in.nextInt();
ob.push(element);
break;
case 2: ob.pop();
break;
case 3: ob.display();
break;
case 4: System.exit(0);
break;
default: System.out.println("Invalid choice");
break;

}
}

}
}
Program 3: Raise Salary

import java.util.Scanner;

class employee
{
int id;
String name;
double salary;
Scanner in = new Scanner(System.in);

void input()
{
System.out.println("Enter your Name: ");
name= in.next();
System.out.println("Enter your ID: ");
id= in.nextInt();
System.out.println("Enter your Salary: ");
salary= in.nextDouble();
}
void display()
{
System.out.println(" Name: "+name);
System.out.println(" ID: "+id);
System.out.println(" Salary: "+salary);
}
void raisesalary(double per)
{
double raise= (per/100)*salary;
salary = salary+raise;
}
}
public class Prog3 {
public static void main(String args[])
{
double percentage;
employee ob = new employee();
ob.input();
ob.display();
System.out.println("Enter the percentage you want to raise");
percentage= ob.in.nextDouble();
ob.raisesalary(percentage);
ob.display();
}
}
Program 4: Test My point

import java.util.Scanner;

class Mypoint
{
int x,y;
Scanner in = new Scanner(System.in);
Mypoint()
{
x=y=0;
}
Mypoint(int a,int b)
{
x=a;
y=b;
}
void setXY()
{
System.out.println("Enter the value of X: ");
x=in.nextInt();
System.out.println("Enter the value of y: ");
y=in.nextInt();
}
int[] getXY()
{
int a[]={x,y};
return a;
}
public String toString()
{
return "Point("+x+","+y+")";
}
double distance(int x,int y)
{
return Math.sqrt(Math.pow((this.x-x),2)+Math.pow((this.y-y), 2));
}
double distance(Mypoint another)
{
return Math.sqrt(Math.pow((x-another.x), 2)+Math.pow((y-another.y), 2));
}
double distance()
{
return Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
}
}
public class TestMyPoint {
public static void main(String args[])
{
Mypoint origin = new Mypoint();
Mypoint P1 = new Mypoint(10,5);
Mypoint P2 = new Mypoint();
System.out.println("Enter Co-ordinates of point P2: ");
P2.setXY();
System.out.println("Co-ordinates of point P2 are: ");
for(int z:P2.getXY())
System.out.println(z);
System.out.println(origin);
System.out.println(P1);
System.out.println(P2);
System.out.println("Distance between P1 and point(2,3) "+P1.distance(2,3));
System.out.println("Distance between P1 and point P2 "+P1.distance(P2));
System.out.println("Distance between P1 and origin "+P1.distance());
}
}
Program 5: Draw and Erase

class shape420
{
void draw()
{
System.out.println("Drawing succesfully..");
}
void erase()
{
System.out.println("Erasing Succesfully...");
}
}
class circle extends shape420
{
void draw()
{
System.out.println("Drawing Circle..");
}
void erase()
{
System.out.println("Erasing Circle..");
}
}
class triangle extends shape420
{
void draw()
{
System.out.println("Drawing Triangle..");
}
void erase()
{
System.out.println("Erasing Triangle..");
}
}
class square extends shape420
{
void draw()
{
System.out.println("Drawing Square..");
}
void erase()
{
System.out.println("Erasing Square..");
}
}
public class prog5 {
public static void main(String args[])
{
circle c=new circle();
c.draw();
c.erase();
triangle t=new triangle();
t.draw();
t.erase();
square s=new square();
s.draw();
s.erase(); } }
Program 6: Calculate Area and Perimeter

abstract class shape


{
abstract void calculateArea();
abstract void calculatePerimeter();
}

class circle extends shape


{
double radius;
circle(int x)
{
radius=x;
}
void calculateArea()
{
double area;
area=Math.PI*radius*radius;
System.out.println("Area of circle= "+area);
}
void calculatePerimeter()
{
double perimeter;
perimeter=2*Math.PI*radius;
System.out.println("Perimeter of circle= "+perimeter);
}
}
class triangle extends shape
{
double a,b,c;
triangle(double x,double y,double z)
{
a=x;
b=y;
c=z;
}
void calculateArea()
{
double area;
double s=(a+b+c)/2;
area=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of Traingle= "+area);
}
void calculatePerimeter()
{
double perimeter;
perimeter=a+b+c;
System.out.println("Perimeter of Triangle= "+perimeter);
}
}
public class prog6 {
public static void main(String args[])
{
circle c= new circle(4);
c.calculateArea();
c.calculatePerimeter();
triangle t= new triangle(4,6,8);
t.calculateArea();
t.calculatePerimeter();
}
}
Program 7: Resizable

interface Resizeable
{
void resizeWidth(int width);
void resizeHeight(int height);
}
class Rectangle implements Resizeable
{
int width,height;
Rectangle(int w,int h)
{
width= w;
height= h;
}
public void resizeWidth(int w)
{
width=w;
}
public void resizeHeight(int h)
{
height=h;
}
void printsize()
{
System.out.println("Width= "+width+" Height= "+height);
}
}
public class prog7 {
public static void main(String args[])
{
Rectangle r= new Rectangle(200,100);
System.out.println("Reactangle height and width before resizing");
r.printsize();
r.resizeWidth(150);
r.resizeWidth(250);
System.out.println("Reactangle height and width After resizing");
r.printsize();
}
}
Program 8: Outer and Inner

class outer
{
void display()
{
System.out.println("Inside outer class... ");
}
class inner
{
void display()
{
System.out.println("Inside inner class...");
}
}
}
public class prog8 {
public static void main(String args[])
{
outer ob=new outer();
outer.inner in=ob.new inner();
ob.display();
in.display();
}
}
Program 9: Exception

class MyException extends Exception


{
public String toString()
{
return"DivideByZero Error";
}
}
class demo4
{
void compute(int a,int b)
{
System.out.println("Computed Value is called by "+a+" and "+b);
if(b==0)
{
try
{
throw new MyException();
}
catch(MyException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block.....");
}
}
else
{
int c=a/b;
System.out.println("Result = "+c);
}
}
}
public class Prog9 {
public static void main(String args[])
{
demo4 ob = new demo4();
ob.compute(10, 5);
ob.compute(9 , 0);
ob.compute(50, 25);
}
}
Program 10: package

package MyAccount;
public class Accounts {
String name;
double bal;
public Accounts(String n,double b)
{
name=n;
bal=b;
}
public void show()
{
if(bal<0)
{
System.out.print("------->");
}
System.out.println("Name = "+name+" Balance = "+bal);
}
}

Main function…..

import MyAccount.Accounts;
public class prog10 {
public static void main(String args[])
{
Accounts ob[]= new Accounts[3];
ob[0]=new Accounts("Ismail",50000);
ob[1]=new Accounts("Saahil",-50);
ob[2]=new Accounts("Bangdo",20000);
ob[0].show();
ob[1].show();
ob[2].show();
}
}

You might also like