Java - Class - Objects Example

Java

In this tutorial we will write code in Java programming language to work with class and object.

If you are reading this tutorial then it is assumed that you know the following points.

In the following example we are using the PackagingBox class to create an object.

The PackagingBox class has the following member variables.

Access ModifierData TypeVariable
privatedoublelength
privatedoublebreadth
privatedoubleheight
publicdoublevolume
doubleweight
doubleprice

And it has the following methods.

Access ModifierReturn TypeMethodParameters
publicvoidsetLengthdouble length
publicdoublegetLength
publicvoidsetBreadthdouble breadth
publicdoublegetBreadth
publicvoidsetHeightdouble height
publicdoublegetHeight
publicvoidsetWeightdouble weight
publicdoublegetWeight
publicvoidsetPricedouble price
publicdoublegetPrice
publicvoidcomputeVolume
publicdoublegetVolume

Code

/**
 * The PackagingBox Class
 */
class PackagingBox {

  // member variables
  private double length;
  private double breadth;
  private double height;
  public double volume;
  double weight;
  double price;

  // methods

  //---- get and set length

  public void setLength(double length) {
    this.length = length;
  }
  
  public double getLength() {
    return this.length;
  }

  //---- get and set breadth
  
  public void setBreadth(double breadth) {
    this.breadth = breadth;
  }
  
  public double getBreadth() {
    return this.breadth;
  }

  //---- get and set height
  
  public void setHeight(double height) {
    this.height = height;
  }
  
  public double getHeight() {
    return this.height;
  }

  //---- get and set weight
  
  public void setWeight(double weight) {
    this.weight = weight;
  }
  
  public double getWeight() {
    return this.weight;
  }

  //---- get and set price
  
  public void setPrice(double price) {
    this.price = price;
  }
  
  public double getPrice() {
    return this.price;
  }

  //---- compute and get volume

  public void computeVolume() {
    this.volume = this.length * this.breadth * this.height;
  }

  public double getVolume() {
    return this.volume;
  }
}


/**
 * The main class.
 */
class ObjectExample {
  
  public static void main(String[] args) {

    // creating an object of the class
    PackagingBox myBox = new PackagingBox();

    // setting the dimensions
    myBox.setLength(10);
    myBox.setBreadth(20);
    myBox.setHeight(30);

    // setting the weight
    myBox.setWeight(120);

    // setting the price
    myBox.setPrice(299);

    // compute the volume
    myBox.computeVolume();

    // get the values
    System.out.println("Dimension of the box:");
    System.out.println("Length: " + myBox.getLength());
    System.out.println("Breadth: " + myBox.getBreadth());
    System.out.println("Height: " + myBox.getHeight());


    System.out.println("Weight, Volume and Price of the box:");
    System.out.println("Weight: " + myBox.getWeight());
    System.out.println("Volume: " + myBox.getVolume());
    System.out.println("Price: " + myBox.getPrice());
  }
}

Output

Dimension of the box:
Length: 10.0
Breadth: 20.0
Height: 30.0
Weight, Volume and Price of the box:
Weight: 120.0
Volume: 6000.0
Price: 299.0