Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Exclamation Constructors pls explan the code line by line in comments

    class Cube1 {

    int length;
    int breadth;
    int height;
    public int getVolume() {
    return (length * breadth * height);
    }
    Cube1() {
    length = 10;
    breadth = 10;
    height = 10;
    }
    Cube1(int l, int b, int h) {
    length = l;
    breadth = b;
    height = h;
    }
    public static void main(String[] args) {
    Cube1 cubeObj1, cubeObj2;
    cubeObj1 = new Cube1();
    cubeObj2 = new Cube1(10, 20, 30);

    System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
    System.out.println("Volume of Cube1 is : " + cubeObj2.getVolume());
    }
    }

    what does this line do?
    public static void main(String[] args) {
    Cube1 cubeObj1, cubeObj2;
    cubeObj1 = new Cube1();
    cubeObj2 = new Cube1(10, 20, 30);

    where is the constructor?
    Last edited by vibaviattigala; 02-19-2011 at 05:16 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by vibaviattigala View Post
    class Cube1 {

    int length;
    int breadth;
    int height;
    public int getVolume() {
    return (length * breadth * height);
    }
    Cube1() {
    length = 10;
    breadth = 10;
    height = 10;
    }
    Cube1(int l, int b, int h) {
    length = l;
    breadth = b;
    height = h;
    }
    public static void main(String[] args) {
    Cube1 cubeObj1, cubeObj2;
    cubeObj1 = new Cube1();
    cubeObj2 = new Cube1(10, 20, 30);

    System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
    System.out.println("Volume of Cube1 is : " + cubeObj2.getVolume());
    }
    }

    i dont know what did in the example pls explain these code line by line in comments //
    Please ask a specific question. What in particular do you not understand? If it is the entire code, then your best bet is to start at the beginning of the tutorials and study Java: Java Language Basics

Similar Threads

  1. Replies: 1
    Last Post: 02-13-2011, 04:09 PM
  2. Replies: 8
    Last Post: 12-28-2010, 03:24 PM
  3. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  4. Replies: 4
    Last Post: 09-10-2009, 11:18 AM
  5. Replies: 9
    Last Post: 04-07-2009, 04:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •