This repository contains a Java program demonstrating Object-Oriented Programming concepts with a Box
class.
The Box
class implements a simple geometric object with the following features:
- Data Members:
height
,width
,depth
(private double variables) - Constructors: Parameterized and default constructors
- Methods: Volume calculation, getters, setters, and display functionality
Main Program: Box.java
private double height
- Height of the boxprivate double width
- Width of the boxprivate double depth
- Depth of the box
- Parameterized Constructor:
Box(double height, double width, double depth)
- Default Constructor:
Box()
- Initializes all dimensions to 0.0
double volume()
- Calculates and returns the volume (height × width × depth)getHeight()
,getWidth()
,getDepth()
- Getter methodssetHeight()
,setWidth()
,setDepth()
- Setter methodsdisplayBox()
- Displays box information and volume
// Create a box with specific dimensions
Box box1 = new Box(10.5, 8.0, 6.5);
box1.displayBox();
// Create a box using default constructor
Box box2 = new Box();
box2.setHeight(5.0);
box2.setWidth(4.0);
box2.setDepth(3.0);
// Calculate volume
double volume = box2.volume();
System.out.println("Volume: " + volume);
-
Compile the program:
javac Box.java
-
Run the program:
java Box
Box 1:
Box dimensions:
Height: 10.5
Width: 8.0
Depth: 6.5
Volume: 546.0
Box 2:
Box dimensions:
Height: 5.0
Width: 4.0
Depth: 3.0
Volume: 60.0
Volume of Box 1: 546.0
Volume of Box 2: 60.0
This program demonstrates:
- Encapsulation: Private data members with public methods
- Constructors: Both parameterized and default constructors
- Method Implementation: Business logic in member functions
- Object Creation: Instantiating objects and calling methods
- Data Validation: Using getters and setters for controlled access
- Java Development Kit (JDK) 8 or higher
- Any Java IDE or text editor
- Command line/terminal for compilation and execution
sarithdm - GitHub Profile
This code is part of the OOP (Object-Oriented Programming) repository:
- Repository: https://github.com/sarithdm/oop
- Direct Link to Box.java: https://github.com/sarithdm/oop/blob/master/Box.java
- Object-Oriented Programming (OOP)
- Java Classes and Objects
- Encapsulation
- Constructor Overloading
- Method Implementation