Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.

Similar presentations


Presentation on theme: "Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If."— Presentation transcript:

1 Methods and You

2 Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If this is the case, then methods can be defined as the verbs of an English sentence. Methods are definitions of behavior of a computer program, usually for specific tasks that are to be repeated many times in the same program.

3 You have been working with methods ALL year! Examples: System.out.println() – a method that prints to the screen and moves to the next line myScanner.nextInt() – a method that grabs an integer value from the keyboard (if the Scanner is initialized as System.in) You will now be defining your own methods. Let’s say you wanted to create a method that would calculate the sum of two numbers. It would be declared as…

4 public static int addTwo(int num1, int num2) { return num1+num2; } It’s a relatively short method, but there’s a lot going on that you need to be aware of.

5 public static int addTwo(int num1, int num2) { return num1+num2; } The first item defining a method must define the method’s visibility. In Java, there are many types of visibility, of which you need to know two: public – this method is visible is any class which uses this program (think of it as any file) private – this method can only be used in THIS file and NO WHERE else.

6 public static int addTwo(int num1, int num2) { return num1+num2; } A static method means that there is only one instance of this method in all of existence. Most of the time, you can leave it out totally. However, we need to declare all of our methods static because we want to use them in the main method of our programs. Because main is a static method, all methods that are to be used in it must also be declared as static. Do not worry about this – just declare them all as static.

7 public static int addTwo(int num1, int num2) { return num1+num2; } ALL methods must have a return type, even if they return NO value. If a method returns no value, it must be declared as void. If a method does return a value, it must be declared as the data type to be returned. Since the addTwo method returns an integer value, it is declared as int.

8 public static int addTwo(int num1, int num2) { return num1+num2; } All methods must have names, and these follow the same naming rules as all identifiers and file names (no spaces, doesn’t start with a number, etc.).

9 public static int addTwo(int num1, int num2) { return num1+num2; } Parameters are the data that the method must be aware of in order for it to perform its task. Since addTwo adds two numbers together, it needs to know what those two numbers are. So, the two parameters that are passed in are the two numbers to be added. These parameters must be declared in the parentheses as if they were being declared in the body of a program. However, they should NOT be given values, i.e. declare but do NOT define. This is bad – public static int addTwo(int num1 = 9, int num2).

10 Some methods do no have any parameters. For example: public static void printFiveStars() { System.out.println(“*****”); } This method does not take in any parameters, since it just prints five stars to the screen, regardless of anything else going on in the main program. This method also does not return a value, since it just prints to the Console, so its return type is void.

11 So, now that you’ve defined the method, you need to USE (call/invoke) it! Here is a main method that uses our addTwo method: public static void main(String[] args) { int theSum = addTwo(1,2); System.out.println(theSum); } This program will print out 3.

12 Let’s take a look at the main program… public static void main(String[] args) { int theSum = addTwo(1,2); System.out.println(theSum); } When addTwo is called, you must pass in two numbers. WHY!? …because it was defined as taking those numbers in! …but what does the computer see!? …

13 public static void main(String[] args) { int theSum = addTwo(1,2); System.out.println(theSum); } addTwo(1,2) turns into the integer value 3. It is TOTALLY REPLACED by that value. The computer does NOT see addTwo(1,2)…it sees: int theSum = 3; Like in algebra, a method that returns a value can be seen as a variable itself, standing for the value that is returned from the method. So, they must often be caught in variables, especially if you want to use them later in the program. However, they can be just printed to the screen also! System.out.println(addTwo(1,2)); will print the number 3 to the Console.

14 Since void methods do not return values, they do not need to be caught in a variable; they can just be called from the main program. Consider our printFiveStars from earlier: public static void main(String[] args) { printFiveStars(); } This will print the following to the Console: *****

15 Methods are one of the benchmarks of logical thinking. When you are presented with a problem, it is most logical to break down a complicated task into smaller problems. In this way, if there is an error in your code somewhere, it would be easier to find. Imagine a gigantic database program, like the one Farrell uses to keep track of student averages, attendance, detentions, etc…

16 Some methods could be: public static double calculateAverage(Student theStudent); public static int getNumberOfAbsences(Student theStudent) public static int getNumberOfDetentions(Student theStudent) public static void printClassGrades(Student theStudent) …and so on!


Download ppt "Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If."

Similar presentations


Ads by Google