×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java Inheritance Programs Java Interface Programs
Java Package Examples - Java
32202    Arnab De    02/08/2017

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

softetechnologies

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;
	}
}
softetechnologies
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());
		
		
		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());
	}
}
softetechnologies

Output:

package-output
Output

Video Tutorials

Java Inheritance Programs Java Interface Programs
softetechnologies
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.
Enter New Comment
Comment History
09/03/2022: Write a program to create a class named Vehicle having protected instance variables regnNumber, speed, color, ownerName and a method showData ( ) to show “This is a vehicle class”. Inherit the Vehicle class into subclasses named Bus and Car having individual private instance variables routeNumber in Bus and manufacturerName in Car and both of them having showData ( ) method showing all details of Bus and Car respectively with content of the super class’s showData ( ) method
By: sachin prajpat [sachiin78@gmail.com]
Please Visit : https://www.tutorialathome.in/java/java-inheritance-examples
Nelson Mandela
Education is the most powerful weapon which you can use to change the world.
Nelson Mandela
235
50.98
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41497
01/01/2018     35829
28/06/2017     33822
25/06/2018     32943
02/08/2017     32202
06/07/2017     26637
01/08/2017     26614
15/05/2017     26253
14/07/2017     21574
21/04/2018     20549
softetechnologies