Mega Code Archive

 
Categories / Java Book / 002 Class
 

0138 Adding a Method to the Class

Add a method to Box,as shown here: class Box { int width; int height; int depth; void calculateVolume() { System.out.print("Volume is "); System.out.println(width * height * depth); } } public class Main { public static void main(String args[]) { Box mybox1 = new Box(); mybox1.width = 10; mybox1.height = 20; mybox1.depth = 15; mybox1.calculateVolume(); } } This program generates the following output: Volume is 3000