-2

How to use the getvolume() method defined by the Box class in the boxweight class? I know that I have to instantiate the Box class in order to use the method defined by it, but how?

class Box {
    private int lenght;
    private int breadth;
    private int height;
    int price;

    Box(int l, int b, int h) {
        lenght= l;
        breadth= b;
        height= h;
    }

    public Box(int p) {
        price= p;
    }

    double getvolume() {
        return lenght*height*breadth;
    }

    void setsize(int $l, int $b, int $h  ) {
        lenght= $l;
        breadth= $b;
        height= $h;
    }
}

public class boxclassdemo {
    public static void main(String[] args) {
        Box mybox1=new Box(10,10,10);
        Box mybox2=new Box(5,5,5);
        Box mybox3=new Box(20);

        System.out.println(mybox1.getvolume());
        System.out.println(mybox2.getvolume());
        System.out.println(mybox3.price);
    }
}

The boxweight class:

public class boxweight  {
    int weight;
    int length,breadth,height;  
    public static void main(String[] args) {
        boxweight myboxx =  new boxweight();
        myboxx.weight= 25;
        myboxx.length=10;
        myboxx.breadth=20;
        myboxx.height=30;
    }
}
Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
Srujan
  • 93
  • 1
  • 10
  • Create an instance of Box class and use it – Suresh Atta Mar 30 '16 at 11:15
  • you will need to define an object of the class Box and call the method to that instance – ΦXocę 웃 Пepeúpa ツ Mar 30 '16 at 11:15
  • 1
    And you want to learn about java coding conventions. There are rules on how to name classes, methods and so on; and you don't seem to know them. For your actual question: that is like super basic knowledge; from the sort ... that well, you should not ask other people. You should turn to books and tutorials and **learn** about the foundations of Java; instead of relying on other people to explain them to you. – GhostCat Mar 30 '16 at 11:20
  • @sᴜʀᴇsʜᴀᴛᴛᴀ can you please elaborate ? – Srujan Mar 30 '16 at 11:21
  • @Jägermeister Why dont you just write exact solution instead of all that stuff. I know that i didn't followed java coding conventions. – Srujan Mar 30 '16 at 11:24
  • 1
    @SrujanSujju That "stuff" is a polite information about how this site works. But it seems you did not get it, so again: this is not a site where you just order "help", and other people spend their free time to just provide that. For example, it is expected that you do prior research. Besides: you expect other people to help you; so you want to make that as easy as possible. Not following conventions simply makes it harder for other people to read your code. So, we shall help you; but you are not even willing to provide input in the best way possible?! So, why do you think we should help you? – GhostCat Mar 30 '16 at 11:35

3 Answers3

1

You can find the answer to your question, as well to many other questions you doubtlessly will have soon on Oracle Java Tutorial on Object Creation

As you know, a class provides the blueprint for objects; you create an object from a class. Each of the following statements taken from the CreateObjectDemo program creates an object and assigns it to a variable:

Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);

The first line creates an object of the Point class, and the second and third lines each create an object of the Rectangle class.

The same goes for actually using these, the very next tutorial:

Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in:

objectReference.fieldName

I recommend for you to either start reading these tutorials or get yourself a good java book, there's plenty out there.

dot_Sp0T
  • 389
  • 4
  • 26
0

(By the way, the first letter of class name usually be upcast.
If you use Eclipse or other IDE, it will help you create when you do the follow step.

Box box = new Box(25, 10, 20, 30);
Then,
System.out.println(box.getvolume());

小土儿
  • 11
  • 4
  • Please take the time to better format your answer. Hint: vertical spacing (called paragraphs) typically make reading text easier. Then: you can't say from his code that he needs an import ... – GhostCat Mar 30 '16 at 11:36
0

It looks as though you want to use inheritance. If you define BoxWeight like this, note the 'extends Box' after the class name:

public class BoxWeight extends Box {
    int weight;

    BoxWeight(int l, int b, int h, int w) {
        super(l, b, h);
        weight = w;
    }
}

Then you can treat a boxweight object as if it were a Box object, which means you can use it's public methods, like so:

public static void main(String[] args) {
    BoxWeight myboxx =  new BoxWeight(10, 20, 30, 25);
    double volume = myboxx.getvolume();
}
Oliver H Gray
  • 105
  • 1
  • 15