Java Package Examples
02-08-2017    5940 times
java package

The package is a collection of similar types of classes. Like other built-in classes in Java, we can also create our own packages. In those packages, we can put our own classes. After importing that packages we can easily create the object or instance of the classes placed in side the packages. Here we described some examples of the packaging problems.

Compute the following:
a. Create a package named org.shapes.
b. Create some classes in the package representing some common geometric shapes like Square, Triangle, Circle and so on. The classes should contain the area and perimeter methods in them.
c. Compile the package.
d. Use this package to find area and perimeter of different shapes as chosen by the user.

Step to solve
1. create a folder called org in your path.
2. open the folder, create another folder called shapes.
3. write three programs called Circle, Square, and triangle. the first line of the
program will be

Tutorial at home

package org.shapes;

4. go to your own path and write another program which invokes the package.
5. Now execute it.

package org.shape;
public class Square
{
private int side;
public Square(int s)
{
side=s;
}
public int perimeter()
{
return (4*side);
}
public int area()
{
return (side*side);
}
}

package org.shape;
public class Circle
{
private int radius;
public Circle(int r)
{
radius=r;
}
public double perimeter()
{
return (2*3.14*radius);
}
public double area()
{
return (3.14*radius*radius);
}
}

package org.shape;
public class Triangle
{
private int side1,side2,side3;
public Triangle(int s1,int s2,int s3)
{
side1=s1;
side2=s2;
side3=s3;
}
public int perimeter()
{
return side1+side2+side3;
}
public double area()
{
double s=(side1+side2+side3)/2;
double a=Math.sqrt((s-side1)+(s-side2)+(s-side3));
return a;
}
}

Tutorial at home

import org.shape.*;
import java.util.*;

class TestPackage
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter The side of the Square : ");
int s=sc.nextInt();
Square sq=new Square(s);
System.out.println("Perimeter of Square is " + sq.perimeter());
System.out.println("Area of Square is " + sq.area());

System.out.println("Enter The radius of the Circle : ");
int r=sc.nextInt();
Circle ci=new Circle(s);
System.out.println("Perimeter of Circle is " + ci.perimeter());
System.out.println("Area of Circle is " + ci.area());

Tutorial at home
Tutorial at home
System.out.println("Enter The Side1 of the Triangle : ");
int s1=sc.nextInt();
System.out.println("Enter The Side2 of the Triangle : ");
int s2=sc.nextInt();
System.out.println("Enter The Side3 of the Triangle : ");
int s3=sc.nextInt();
Triangle t=new Triangle(s1,s2,s3);
System.out.println("Perimeter of Triangle is " + t.perimeter());
System.out.println("Area of Triangle is " + t.area());
}
}

Output:

package-output
Output

Video Tutorials

freenotes
    Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
    Related Post
  Query About the post