Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!

Similar presentations


Presentation on theme: "School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!"— Presentation transcript:

1 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!

2 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 2 CMT1000: Introduction to Programming Ed Currie Lecture 8: Methods

3 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 3 An Overview Methods –declaration –returning values –parameters –composition –calls –duration and scope –overloading Program development and top down design Going for a song

4 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 4 Some methods we’ve already seen showMessageDialog(null, “Hello!”) showInputDialog(“Enter a thingy”) main(String args[]) spling() Pizza base recipe (Not Java!) –Which classes do each of these belong to? –What type of arguments (‘actual’ parameters) can we give to them? –How do we use them?

5 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 5 Answers showMessageDialog(null, “Hello!”) –Class: JOptionPane –Arguments: null and a String –Action: Writes String on screen showInputDialog(“Enter a thingy”) –Class: JOptionPane –Argument: A String –Action: Writes String on screen; gets string from user main(String args[]) –Class: Defined by us –Argument: An array of strings –Action: Used to implement any algorithm we choose

6 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 6 Answers spling() –Class: Defined by us –Argument: None –Action: Writes a message on the screen Pizza base recipe (Not Java!) –Class: The recipe book (collection of algorithms –Argument: List of ingredients –Action: Makes a pizza base

7 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 7 How do we use (call) these methods? main(String args[]) Called automatically when we pass the class containing it to the Java Virtual Machine - see next week! JOptionPane.showMessageDialog(null, “Hello!”) spling() We call these by using their name (with arguments, if any) as a STATEMENT as shown above

8 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 8 NOTE: When the method isn’t in the class from which it is being called, we must include its class name in the call, as with JOptionPane.showMessageDialog(null, “Hello!”)

9 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 9 Calling methods (continued) String input; input = JOptionPane.showInputDialog( “Enter a thingy”) This one is different! We call it by using its name, as with the others… … but the call is an EXPRESSION, not a STATEMENT To capture the result, we must either –assign it to a variable, as above –pass it to another method as an argument

10 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 10 What does this do? JOptionPane.showMessageDialog( null, JOptionPane.showInputDialog( "Say something") );

11 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 11 So we’ve got two types of methods: Ones that carry out an action (have a side-effect) - these have return type void - indicates that they don’t return anything e.g. public static void main(String args{}) private static void spling()

12 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 12 … and ones that return a value These have a non-void return type e.g. showInputDialog What is the return type of this method?

13 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 13 Here’s an example of each type public static void square1( int y ) { System.out.println( “Square of the argument is “ + (y * y) ); } public static int square2( int y ) { return y * y; }

14 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 14 Exercise: write a main method to invoke each of the above methods

15 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 15 Parameters: What happens when a method is called? int x = 4; square1(x); The value of the argument variable x is COPIED into the formal parameter y within square1. This mechanism is known as CALL BY VALUE y behaves as if it was an initialised local variable within square1. Any changes to y do NOT affect x

16 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 16 Parameters If the formal parameter is of a primitive type, then any expression of that type may be used as an argument. What will square1 write on the screen? square1(5); square1(2+4);

17 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 17 Composition Square2 returns a value, which may itself be used as a parameter. What is the value of the following expressions? square2( square2(3)) square2(square2(square2(2)))

18 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 18 Notes: A method with return type void must not return a value; it may have a return statement without an expression return; …but this is simply used to terminate the method. A method with a return type other than void must contain a return statement with an expression of the return type.

19 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 19 Program development Each method should perform one simple task. Each method should not normally be bigger than one screen full of code. Programs should be collections of small methods Each method can be written by a different person Each method can be separately tested, then ‘plugged in’ to the program. Makes programs easier to develop, debug and modify

20 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 20 Duration and scope of identifiers The duration of an identifier is the time during which it exists in memory. The scope of an identifier is that part of the program in which it may be referenced. Parameters and local variables declared in a method body have automatic duration; created when their declaration is reached, destroyed when the block they are in, delimited by { and }, terminates.

21 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 21 Duration and scope of identifiers (cont) A variable declared in a block (between any pair of { and }) has block scope - it can only be used between its declaration and the end of that block. Later we will learn about variables with other scopes and durations.

22 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 22 Method overloading We can have more than one method with the same name in a class, as long as each differs in the number, type and/or order of its parameters.

23 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 23 public static void main(String args[]) { JOptionPane.showMessageDialog(null, “Square of 4 is “ + square(4) + ”Square of 4.5 is “ + square(4.5) ); } private static int square( int y ) { return y * y; } private static double square( double y ) { return y * y; }

24 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 24 Example: Going for a song Old MacDonald had a farm, E-I-E-I-O And on this farm he had a pig, E-I-E-I-O With an oink oink here and an oink oink there Here an oink, there an oink, everywhere an oink oink Old MacDonald had a farm, E-I-E-I-O

25 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 25 Write a program to print the song Easy to do it with loads of strings concatenated together, but... … repetitive - every verse is the same except for the name of the animal and its sound … … the program will be very large.

26 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 26 A better way... Make the verse-writing algorithm a separate method Call it each time we want to print a verse… … but, each time, tell it which animal and which sound to print.. … by passing them as ARGUMENTS!

27 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 27 Question: What type should the parameters have?

28 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 28 The method private static String buildVerse(String creature, String sound) { return "\nOld MacDonald had a farm, E-I-E-I-O" + "\nAnd on this farm he had a " + creature + ", E-I-E-I-O" + "\nWith a " + sound + "-" + sound + " here and a " + sound + "-" + sound + " there" + "\nHere a " + sound + " there a " + sound + " everywhere a " + sound + "-" + sound + "\nOld MacDonald had a farm, E-I-E-I-O\n"; }

29 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 29 The main method public static void main(String [] args) { String lyrics = buildVerse("cow", "moo") + buildVerse("sheep", "baa") + buildVerse("duck", "quack") // plus any other verses ; JOptionPane.showMessageDialog(null, lyrics); }


Download ppt "School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!"

Similar presentations


Ads by Google