Methods in Java
-
Upload
jussi-pohjolainen -
Category
Education
-
view
2.112 -
download
2
description
Transcript of Methods in Java
Methods in Java
Jussi Pohjolainen Tampere University of Applied Sciences
Problem public class HugeProgram { public static void main(String [] args) {
1. codeline 2. codeline 3. codeline ... 10 000. codeline } }
How are you going to
manage this huge
codeblock?
Problem
• It's possible that your whole program is implemented inside the main – method
• Result: No one will understand the code! • You have to create the code that is – Reusable – You and others can manage it
• Well how?
How to divide your code?
• Best pracJce: Use Object-‐Orientated Programming – Classes, Methods, Inheritance, ...
• Lot of possibiliJes with OO! • But first let's concentrate on methods!
Methods: Divide your Code? public class HugeProgram { public static void main(String [] args) { doSomething1(); doSomething2(); doSomething3(); } public static void doSomething1() { // something } public static void doSomething2() { // something } public static void doSomething3() { // something } }
Methods: Divide your Code? public class HugeProgram { public static void main(String [] args) { doSomething1(); doSomething2(); } public static void doSomething1() { System.out.print("Hello "); } public static void doSomething2() { System.out.println("World!"); } } java HugeProgram
Hello World!
Method Call
Method Method
Examples
• Flash demo: Method 1 (no parameters, no return)
• MethodDemo1.java • MethodDemo2.java • MethodDemo3.java •
IT'S NOT THAT EASY: PARAMETERS AND RETURN
Real Life Example
I really want to buy christmas gi\s for my children. The doll costs 5
euros and the toy car costs 6 euros. I have 12 euros, is that enough? Why didn't I listen to the math teacher at school? Why didn't I learn sum – calculaJons? Why oh
why?
Real Life Example
Hello Hello! My name is Mr. Sum and I can sum everything! Please use my knowledge!
Real Life Example
Well Mr Sum. I have a problem I don't know what is 5 plus 6.
Real Life Example Don't understand your
problem. I only do simple calculaJons, I really need to have two integer values to be able to do the sum. Please just
give my two numbers!
Parameters!
5 6
Return!
11
Real Life Example
Well thanks, at least I know my numbers and 11 is less than 12, so great! My children will be
very happy!
Mr. Sum?
Mr. Sum
5
6
11
parameter return
Real Life Example in the Nerd World
I am implemenJng a applicaJon and I really need a random floaJng point value between
[0,1[
Well Howdy, my name is Mr. Random and I live in a town called Math. I can give you a random floaJng point value
between [0,1[
Real Life Example in the Nerd World
Give random number.
0.34523456
Mr. Random?
Mr. Random 0.34523456
return
Real Life Example in the Nerd World
Did you know that in the town Math there other guys living too? For
example: Mr. Round, Mr Max, Mr Min, Mr Sin...
LET'S START CODING
The usage of Class Library import java.lang.Math;
public class Example {
public static void main(String [] args) {
double myRandomNumber = Math.random();
System.out.println(myRandomNumber);
}
}
Import the class
class method
Mr. Max in Code import java.lang.Math;
public class Example {
public static void main(String [] args) {
int maxNumber = Math.max(5,10);
System.out.println(maxNumber );
}
}
Import the class
class method parameters
Your own Methods public class Example {
public static void main(String [] args) {
printSomething();
}
public static void printSomething() {
System.out.println("Hello World!");
}
}
Your own Methods public class Example { public static void main(String [] args) { PrintClass.printSomething(); } } class PrintClass { public static void printSomething() { System.out.println("Hello World!"); } }
Parameters public class Example { public static void main(String [] args) { MyMath.max(5, 10); } } class MyMath { public static void max(int a, int b) { if(a > b) System.out.println(a); else System.out.println(b); } }
Return public class Example { public static void main(String [] args) { int maxNumber = MyMath.max(5, 10); System.out.println(maxNumber); } } class MyMath { public static int max(int a, int b) { if(a > b) return a; else return b; } }
In One class public class Example { public static void main(String [] args) { int maxNumber = max(5, 10); System.out.println(maxNumber); } public static int max(int a, int b) { if(a > b) return a; else return b; } }
Examples
• Flash-‐demo: Method 2 • Flash-‐demo: Method 3 • Flash-‐demo: Method 4 • MethodDemo4.java
VISIBILITY OF VARIABLES
This does not work, why? import java.util.Scanner; class Example { public static void main(String [] args) { Scanner input = new Scanner(System.in); int x = input.nextInt(); if(x == 5) { int b = 5; } System.out.println(b); } }
This does not work, why? import java.util.Scanner; class MethodDemo3 { public static void main(String [] args) { askName(); printName(); } public static void askName() { Scanner input = new Scanner(System.in); String name = input.nextLine(); } public static void printName() { System.out.println(name); } }
SoluJon import java.util.Scanner; class MethodDemo3 { public static void main(String [] args) { String n = askName(); printName(n); } public static String askName() { Scanner input = new Scanner(System.in); String name = input.nextLine(); return name; } public static void printName(String name) { System.out.println(name); } }
EXAMPLE
Exercise
• Implement a Java-‐app that prints square. The height of the square is asked from the user.
java Rectangle Height? 4 XXXX X X X X XXXX
import java.util.Scanner; class Rectangle { public static void main(String [] args) { Scanner input = new Scanner(System.in)a; int height = input.nextInt(); // Go all the lines for(int line=0; line<height; line++) { // If the line is the first or last if(line == 0 || line == height-1) { // Print 'X' height amount of times } else { // Print 'X' // Print ' ' height-2 amount of times // Print 'X' } // Print enter System.out.println(); } } }
Let's use Methods! class Rectangle {
public static void print(char mark, int amount) {
for(int i=0; i<amount; i++) {
System.out.print(mark);
}
}
...
}
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
int height = input.nextInt();
// Go all the lines
for(int line=0; line<height; line++) {
// If the line is the first or last
if(line == 0 || line == height-1) {
print('X', height); // METHOD CALL!
} else {
print('X', 1); // METHOD CALL!
print(' ', height-2); // METHOD CALL!
print('X', 1); // METHOD CALL!
}
// Print enter
System.out.println();
}
}