Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Exercises
  


5. Defining and Using Java Methods: Help page 15 of 31




  • Task 1

    Define a method called sayHello() in class Methodcall, provided in the skeleton code, that has no arguments and no return value. Make the body of the method simply print "Hello."


    The signature of this method is:

    
      public void sayHello();
    
  • Task 2

    Make the start() method of Methodcall call sayHello().


    Add this code:

    
    sayHello();
    
  • Task 3

    Define a method called addTwo() that takes an integer argument, adds 2 to it, and returns that result.


    Use an int, not an Integer

  • Task 4

    In the start() method of Methodcall, define a local integer variable and initialize it to the result of calling addTwo(3). Print out the variable; that is, print out the result of the method call. Define another local integer variable initialize it to the result of calling addTwo(19). Print out its result.


    To print out a value:

    
    System.out.println("msg");
    


copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact