1)Program
to accomplish following task.
i)Create a user-defined package box which has a class definition. For Box having data member and disp() method.
ii)Source file imports above package and calculates the volume of box
i)Create a user-defined package box which has a class definition. For Box having data member and disp() method.
ii)Source file imports above package and calculates the volume of box
Code:
package box;
public class Dim
{
public float L,B,H,volume;
public Dim(float l,float b,float h)
{
L=l;
B=b;
H=h;
}
public void show()
{
System.out.println("Length = "+L+", Breadth =
"+B+", Height = "+H);
System.out.println("Volume of Box="+volume);
}
}
import box.Dim;
class Boxdemo
{
public static void main(String args[])
{
Dim b=new Dim(10,20,30);
b.volume=b.L*b.B*b.H;
b.show();
}
}
Output:
C:\Program Files\Java\jdk1.6.0_07\bin>javac
Boxdemo.java
C:\Program
Files\Java\jdk1.6.0_07\bin>java Boxdemo
Length = 10.0, Breadth = 20.0,
Height = 30.0
Volume of Box=6000.0
=========================================================
=========================================================
2)
Creating Package for CD store and using it.
Code:
package MyPackage;
class CD
{
private String title;
int length;
boolean avail;
CD(String t, int l,boolean a)
{
title=t;
length=l;
avail=a;
}
public void show()
{
System.out.println("\nTitle : "+title + "\nLength :
" + length + "\nAvail : "+avail );
}
}
class CDStore
{
public static void main(String args[])
{
CD cd1=new CD("Bhool Bhulaiya", 120, true);
CD cd2=new CD("Dhoom", 120, true);
cd1.show();
cd2.show();
}
}
Output:
Title : Bhool Bhulaiya
Length : 120
Avail : true
Title : Dhoom
Length : 120
Avail : true
==================================================================================================================
3)
Program for creating simple package and importing it.
Code:
package package1;
public class ClassA
{
public void displayA()
{
System.out.println("ClassA");
}
}
import package1.ClassA;
class PackageTest1
{
public static void main(String args[])
{
ClassA A=new ClassA();
A.displayA();
}
}
Output:
ClassA
=========================================================
=========================================================
4) Creating a package Factorial for calculating factorial and using it in
program.
Code:
package package1;
public class Factorial
{
public Factorial(int x)
{
int fact=1 ;
for(int i=1;i<=x;i++)
{
fact=fact*i;
}
System.out.print(" The Factorial of "+x+" is :
"+fact);
}
}
import package1.Factorial;
class packageDemo
{
public static void main(String args[])
{
Factorial f=new Factorial(5);
}
}
Output:
The Factorial of 5 is : 120
No comments:
Post a Comment