• Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the .main() method?  RSS feed

 
Dwijesh Gajadur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I found the code below on the web. On line 15 it says volume=Calculate.get_Vol().main(a);. I want to know what does the .main() method do? And also want to know why the .get_Vol() method is called on the Calculate class name instead of the object variable cal.

The Calculate class is supposed to be a separate class which contains methods to calculate volume of cube,cuboid, hemisphere and cylinder.


 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

We would need to see the code for the class Calculate. Calculate.get_Vol() must be returning an object of some kind, and we would need to see the code for that class.

And when I say "we", I really mean "you". ;-)
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch, Dwijesh!

Without seeing the source code for the Calculate class, it is difficult to tell you exactly what it does, but it does seem to be the method that performs the actual volume calculations. It is probably an overloaded method (meaning there is actually more than one method with that name, each taking a different number of parameters). Based on the number of parameters, it is determining what volume calculation to use. (There is another option, that the "main" method uses variable length arguments, but I would argue that a series of overloaded methods would be a better design choice in this case. But that is an argument beyond the realm of the beginner, so I won't bore you with it. Learning the basics of a language can be overwhelming enough.)

Normally the "main" method is one reserved for starting the application (as in line 4 of your code), but there is nothing prohibiting other code from calling it. In this case, however, I think it is just poorly-named ("runCalculation" might be better). Having a better name would make the code "self-documenting," which basically means easier to understand when reading it over.

As for why the get_Vol() method is being called on the Calculate class instead of the cal instance, that is because the get_Vol() method is static; you do not need an instance of the class to call it. In this case, I would imagine that there are other methods (like get_Area()) that can also be used to perform calculations, and since these calculations are mathematic functions independent of data, you don't actually need an instance of the Calculate method to invoke them.
 
Dwijesh Gajadur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick reply

The code is actually from a task from Hacker Rank. It is actually my task to create the Calculate class. The Solution class is already created and given in the task and cannot be modified. What I don't understand is why they have used the .main method. If I were to create the Solution class, I would have never call the .get_Vol method that way. I want to know the reason why they have done so.
The task problem description is below:

You are given a class Solution and its main method in the editor. In each test cases, it takes an input ch which represents a choice of the following:

ch=1 represents the volume of a cube that has to be calculated where a represents the length of the sides of the cube.
ch=2 represents the volume of a cuboid that has to be calculated where l,b,h represent the dimensions of a cuboid.
ch=3 represents the volume of a hemisphere that has to be calculated where r represents the radius of a hemisphere.
ch=4 represents the volume of a cylinder that has to be calculated where r,h represent the radius and height of the cylinder respectively.
Your task is to create the class Calculate and the required methods so that the code prints the volume of the figures rounded to exactly 33 decimal places.


Note: Use Math.PI or 3.141592653.14159265 as the value of pi.

Input Format

First line contains T, the number of test cases. Each test case contains ch, representing the choice as given in the problem statement.

When ch=1, Next line contains a, length of the sides of the cube.
When ch=2, Next three lines contain l, b, h representing length, breadth and height of the cuboid respectively. l, b, h will be in three separate lines
When ch=3, Next line contains r, the radius of the hemisphere
When ch=4, Next two lines contain r, h representing the radius and height of the cylinder respectively. r, h will be in two separate lines.
Note: You have to determine the data type of each parameter by looking at the code given in the main method.

Constraints
1≤ch≤41≤ch≤4
−100≤a,l,b,h,r≤100−100≤a,l,b,h,r≤100
There will be at most 33 digits after decimal point in input.

Output Format

For each test case, print the answer rounded up to exactly 3 decimal places in a single line. For example, 1.2345 should be rounded to 1.235, 3.12995 should be rounded to 3.130.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!