Tuesday, December 10, 2013

Method with return type in JAVA : Day 4



a) How Method return a value? Write necessary Syntax.

b) Write any JAVA program that takes parameters in a Method.

12 comments:

  1. *********Write any JAVA program that takes parameters in a Method.

    Source code:

    package Box;

    public class Box
    {
    public double width;
    public double depth;
    public double height;
    public Box(double width,double depth,double height)
    {
    this.width=width;
    this.depth=depth;
    this.height=height;
    }
    public double calculate()
    {
    double volume = width*depth*height;
    return volume;
    }
    public static void main(String[] args)
    {
    Box box = new Box(5,4,3);
    double volumeOfBox = box.calculate();
    System.out.println("The volume is " + volumeOfBox);
    }
    }

    ReplyDelete
  2. a))) ***How Method return a value? Write necessary Syntax.

    A method returns to the code that invoked it when it:

    1.completes all the statements in the method,
    2.reaches a return statement, or
    3.throws an exception (covered later) whichever occurs first.

    Need to declare a method's return type in its method declaration. Within the body of the method use the return statement

    to return the value.
    Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such

    a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like

    this:

    return;

    If try to return a value from a method that is declared void, ir will be a compiler error.
    Any method that is not declared void must contain a return statement with a corresponding return value, like this:

    return returnValue;

    ReplyDelete
  3. b) Write any JAVA program that takes parameters in a Method.


    public class Box

    {
    public double calvol(double width,double heigth ,double depth)

    {

    return width*heigth*depth;

    }
    }


    public class Ob {

    public static void main(String[] args) {

    Box b1 = new Box();

    double vol;

    vol = b1.calvol(11,12,14);

    System.out.println("The volum is : " + vol);


    }

    }

    ReplyDelete
  4. *******a) How Method return a value? Write necessary Syntax.**********

    Solution:
    Returning value from the method: We can specify return type of the method as "primitive data type" or

    "class name". Return type can be be "void" means it does not return any value. Method can return a

    value by using "return" keyword.

    e.g. int len = R1.getLength( );

    Example of returning value from the method:

    Source code:

    package square;
    public class Square
    {
    double length;
    double width;

    void setLength(int len)
    {
    length = len;
    }

    double getLength()
    {
    return length;
    }
    }

    class SquareDemo
    {
    public static void main(String args[])
    {
    Square s1 = new Square();
    s1.setLength(20.5);
    double len = s1.getLength();

    System.out.println("Length of a Square : " + len);
    }
    }

    ReplyDelete
    Replies
    1. here is a typing mistake *void setLength(int len).....it will be *void setLength(double len)

      Delete
  5. Your technical information related with java programming is very useful and interesting. Also share updated details about java in your website. Thanks for sharing this article.
    Java training institute in chennai

    ReplyDelete
  6. This information you provided in the blog that was really unique I love it!!, Thanks for sharing such a great blog..Keep posting..

    JAVA J2EE Training in Chennai

    ReplyDelete
  7. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp in chennai.

    ReplyDelete
  8. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    Best Final Year Project Center in Chennai | No.1 Diploma Project in Chennai | Best BBA/MBA Project Center in Chennai


    ReplyDelete
  9. Sas Training Institute in Noida-Webtrackker is the best sas training institute in noida. SAS has taken the lead role for a long time, where most companies have standard software. While you have this certification under your belt, give big rewards to the IT industry, it can also serve as an important payer on the business side of the economy. SAS can read data files created by other statistical packages. Therefore, for experienced users of these statistical packages, SAS does not threaten to create data files created by these packages in a SAS file format.
    Sap Training Institute in Noida
    PHP Training Institute in Noida
    Hadoop Training Institute in Noida
    Oracle Training Institute in Noida
    Linux Training Institute in Noida
    Dot net Training Institute in Noida
    Salesforce training institute in noida
    Java training institute in noida

    ReplyDelete
  10. Hadoop Training Institute in Noida- Webtrackker is the best Hadoop training institute in noida. If you want take the training in a Hadoop than Webtrackker is the best option for you. Since then Hadoop has continued with the development of the YARN cluster manager, releasing the project from its first distribution of HadoopMap Reduce. HadoopMap Reduce is still available in Hadoop to perform static batch processes for which Map Reduce is suitable. Other data processing activities can be assigned to different processing engines (including Spark), where YARN manages the management and allocation of cluster resources.
    Projects like Apache Mesas provide a powerful and growing range of distributed cluster management capabilities. Most Spark implementations still use Apache Hadoop and its associated projects to meet these requirements.

    ReplyDelete