C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

How to add a method to a class?

0 votes
18 views
asked Apr 25, 2014 by taye Sr Member (4,970 points)

1 Answer

0 votes
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();
  }
}
answered Apr 25, 2014 by sara Guru (24,570 points)

Related questions

0 votes
1 answer 89 views
89 views asked May 10, 2014 by augustaina Sr Member (4,820 points)
0 votes
1 answer 39 views
39 views asked May 11, 2014 by rock Expert (14,990 points)
0 votes
1 answer 51 views
51 views asked Apr 25, 2014 by taye Sr Member (4,970 points)
0 votes
1 answer 81 views
81 views asked May 10, 2014 by augustaina Sr Member (4,820 points)
0 votes
1 answer 16 views
16 views asked May 11, 2014 by augustaina Sr Member (4,820 points)
...