Java Lab Manual
Java Lab Manual
import java.util.Scanner;
}
}
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
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
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();
}
}