Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Methods in Java

From Juneday education
Jump to: navigation, search

Functions

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

Without functions it would be hard to write programs of today. Methods and variables are also crucial in Object Oriented (OO) languages so understanding methods is important to understand OO and Java.


Purpose

Knowing how to write methods in is important to learn OO.

Goal

The student shall:

  • be able to write method using Java syntax
  • understand and use parameters
  • understand and use return values


Concepts

Instructions to the teacher

Common problems

Videos

All videos in this chapter:

See below for individual links to the videos.

Methods in Java

Description

Exercises

  1. Write a method stupid that does nothing and returns nothing.
  2. Write a method that prints "Hello Cleveland" on the screen (hint: System.out.println).
  3. Write a method that prints "Hello Cleveland" on the screen two times.
  4. Write a method that prints "Hello Cleveland" on the screen 100 times.
  5. Write a method that prints "Hello Cleveland" on the screen as many times as you supply with an argument.
  6. Write a method that prints a string on the screen as many times as
  7. you supply with an argument. The string should also be supplied as an argument.
    Typically the method should start look something like this:
     public void printStringNTimes(String text, int times) {
    
     }
    
  8. Write a method that returns 2 (int). Yup, it's kind of a stupid method.
  9. Write a method that returns true (boolean). Yup, it's kind of a stupid method.
  10. Write a method, mult, that takes two integers as parameters. The method should multiply the parameters and return the product.
  11. Spend around 30 seconds on the following code and try to find out what it does?
     public void washingMachineTransparency(int text, String nrTimes) {
    
    	       String xyz = nrTimes;
    
    int temperature = text;
    
    	       for(int i=0; i<temperature; i++) {
    
    	    System.out.println(xyz);}
    
    Do you think that the code below is easier to understand?
    public void printStringNTimes(String text, int nrTimes) {
    
      for(int i=0; i<nrTimes; i++) {
    
        System.out.println(text);
    
      }
    
    }
    
  12. Write a Java method, closeApp, that calls the methods cleanCache and shutdown. The methods do not have any parameters. We can pretend, to get some background, that the method cleanCache is cleaning some cache somewhere. Such a method is often found in mobile phones. The shutdown method we pretend to, as you may guess, be shutting down the device.
  13. Look at the method below.
    int add(int a, int b) {
    
        return a+b;
    
    }
    
    a) What does the method do?
    b) What is the value of the variable sum after the lines of code below?
    int sum = add(2,3);
    
    c) What is the value of the variable sum after the lines of code below?
    int sum = add(2,3) + add(4,5);
    
    d) What is the value of the variable sum after the lines of code below?
    int sum = add(2,add(4,5));
    
    e) What is the value of the variable sum after the lines of code below?
    int sum = add(add(2,3), add(4,5));
    

Solutions

Expand using link to the right to see the full content.


  1. void stupid() {
    
    }
    
    What does it all mean?
    void - return nothing
    stupid - name of the function. Parentesises since Java demands it.
    { - start of the method's code block
    } - end of the method's code block
  2. 	public void cleveland() {
    
    		System.out.println("Hello Cleveland");
    
    	}
    
  3. 	public void clevelandTwoTimes() {
    
    		System.out.println("Hello Cleveland");
    
    		System.out.println("Hello Cleveland");
    
    	}
    
  4. 	public void clevelandHundredTimes() {
    
    	    for(int i=0; i<100; i++) {
    
    	        System.out.println("Hello Cleveland");
    
    	    }
    
    	}
    
  5. 	public void clevelandNTimes(int nrTimes) {
    
    	    for(int i=0; i<nrTimes; i++) {
    
    	        System.out.println("Hello Cleveland");
    
    	    }
    
    	}
    
  6. 	public void printStringNTimes(String text, int nrTimes) {
    
    	    for(int i=0; i<nrTimes; i++) {
    
    	        System.out.println(text);
    
    	    }
    
    	}
    
  7. 	public int stupidity() {
    
    	    return 2;
    
    	}
    
  8. 	public boolean stupidityReborn() {
    
    	    return true;
    
    	}
    
  9. We can write this method in several different ways. Here are two examples:
    	public int mult(int a, int b) {
    
    	    int result = a##b;
    
    	    return result;
    
    	}
    
    	public int mult(int a, int b) {
    
    	    return a##b;
    
    	}
    
  10. The meaning of the question is to illustrate the importance of using good names for methods and variables as well as structuring your code.
  11. void closeApp() {
    
      cleanCache();
    
      shutdown();
    
    }
    
    Let's analyse this:
    void - this is the return type. Since we're not instructed to return something - what would we retrn? - we might as well do so, so let's use void
    closeApp() - well, the nae should be closeApp and the paraenthesises are there since Java demands it.
    { - this is the start of the block so when someone calls this method execution starts here
    cleanCache() - a simple call to the method cleanCache. This method is now called and when finished execution continues on the next line...
    shutdown() - after that cleanCache call we're now continuing with the shutdown call.
    } - this is the end of the block so when someone calls this method execution stops here

Presentation

Videos

Links