dcsimg
Results 1 to 2 of 2

Thread: Why don't we put all the code in one class?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2016
    Posts
    111

    Why don't we put all the code in one class?

    HI.

    Its my second question of Java.

    Here is a code to understand
    Code:
    class Box {
    double width;
    double height;
    double depth;
    }
    
    // This class declares an object of type Box.
    class BoxDemo {
    public static void main(String args[]) {
    Box mybox = new Box();
    double vol;
    // assign values to mybox's instance variables
    mybox.width = 10;
    mybox.height = 20;
    mybox.depth = 15;
    // compute volume of box
    vol = mybox.width * mybox.height * mybox.depth;
    System.out.println("Volume is " + vol);
    }
    }
    Question:
    This is the first class program in the book I'm following.
    1. Why the author has used 2 classed, the Box and the BoxDemo? Thought in comments it is written as
    // This class declares an object of type Box.
    But why the entire code is not put into a single class Box.?

    2. How will I understand that how many classes I have to make for my program?


    Please guide me with this.

    Thanks.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,582

    Re: Why don't we put all the code in one class?

    What if you want five boxes, with different width, height and depth?

    If you had the width, height and depth code in the box class, you would only be able to have a fixed size box.

    Since you have Box declared, you can create multiple boxes in other classes.
    Code:
    // This class declares an object of type Box.
    class BoxDemo {
    public static void main(String args[]) {
    Box mybox = new Box();
    Box anotherBox = new Box();
    double vol;
    // assign values to mybox's instance variables
    mybox.width = 10;
    mybox.height = 20;
    mybox.depth = 15;
    
    // assign values to anotherBox 's instance variables
    anotherBox.width = 20;
    anotherBox.height = 7;
    anotherBox.depth = 35;
    
    // compute volume of myBox box
    vol = myBox.width * myBox.height * myBox.depth;
    System.out.println("Volume of myBox is " + vol);
    
    // compute volume of anotherBox box
    vol = anotherBox.width * anotherBox.height * anotherBox.depth;
    System.out.println("Volume of anotherBox is " + vol);
    }
    }
    You make as many classes as you want. It is generally best if a class represents one thing and the methods and properties needed to represent and manipulate that thing.
    The two primary reason for creating a class is to gather related things that apply to some object together so it is easier to understand and use, and second to allow reusing the class to have multiple instances of the object the class represents.
    Last edited by passel; May 12th, 2018 at 12:01 PM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width