Introductory Programming In Java Comp6700/Comp2140 ! Henry Gardner/ Alexei Khorev/ Charles Martin Many of the students in this course seem to be up and running with the lecture material. However, we are concerned that some of you could be getting left behind and we are thinking of spending some of Week 5 as a review week. What we are thinking is that we would not set new material in the labs in week 5 but that people could attend labs if they want and work on some basic programming -- we will give you some ideas. We would spend the Wednesday lecture slot of that week reviewing material, especially the basic programming and object-oriented material. The Friday lecture of Week 5 would present new material. Week 5 has a holiday on the Monday so there will be no lab on that day. Because it is a review week, we will suggest that those students who feel that they want to meet us in the labs come along to the Tuesday or Wednesday labs. There are two other public holidays on Mondays this semester. We will discuss arrangements for those labs in due course. How are you coping……? 2 Aside: Blocks Methods Today 3 Java “blocks” live inside { } brackets Blocks define scope. Aside -- Blocks 4 5 (Loosely) also called “functions”, “subroutines”, “procedures” etc… Methods are class-based or object-based functions in Java. You need to use a class name or an object name to call methods. They have return types to return data. Data can be passed in to them. They often define local variables. They are very important for structuring and reusing code. (Algorithms live in methods.) Methods 6 A method is a separate piece of code which can be called from a main program (“main method”) or another method. Each method must be defined within a class and each class can have several methods. Copies of the data from the calling program are passed down to a method in its argument list. In Java, each method returns a single value (which could be an array or an object) to the calling method (or “program”). ! As well as calculating a return value, methods can do all sorts of other things, such as writing to files or databases. Often methods do not return any value at all — in this case the return type is given the special designation void. If the return type is not void, then a method must contain one or more return statements. Method - basics 7 Methods – formal definition 8 • Modifiers of methods (just like modifiers of variables) determine their characteristics in terms of relationship with other parts of the program: can they be used by other classes (private, protected, public), can they change implementation (final), can they be called without creating an object of the class in which they are defined (static). • The argument list is a comma separated list of parameter declarations surrounded by a pair of matching parentheses • The type of the expression in the return statement must match the type specified just before the method name. Example 9 Formal arguments are the names given to the method parameters in their method definition. The actual arguments are the data or variables passed down to the methods when they are called. In Java, the data values are copied before passing to the methods. This is “call by value”. But funny things happen when you copy the “value” of an object “reference” and pass it to a method For all intents and purposes. It is best to think that object parameters are passed by reference. This can be dangerous and lead to side-effects that you did not intend. Formal and actual arguments 10 11 12 13 Have a particular, well-defined task in mind. Carefully consider assumptions you make on the values of the input parameters (the “preconditions”). Ensure that your methods guarantee to return the correct results for all input parameters. Strive to avoid “side effects”; i.e. affecting object states in the calling program through object references. Note that functional languages are free of side effects by design (“pure”) Designing methods 14 Often we want to write methods which cope with being passed different arguments Some arguments could have default values You might want to deal with different data types which store essentially the same information It is possible to have several methods with the same name and same return type so long that their argument lists are different. This is very flexible and convenient. Java will automatically choose the correct method to match the actual arguments that are passed to it. Method overloading 15 This is called recursion Some algorithms are very neatly implemented using recursion. Classic example: factorial Need to have a termination condition. Methods can call themselves 16 17 18 These are stored in an area of memory called the JVM “stack” This storage is particular to each method; not to the whole program Stacks are “first in first out” data structures. When recursion is used the most recent versions of the local variables sit on top of the stack. You can adjust the amount of stack memory available to the JVM when you run your program Note that Java objects are stored in another area of memory called the “heap”. You can adjust the amount of heap memory available to the JVM. Local variables in methods 19 There are some youTube videos: EG (Guru99): https://www.youtube.com/watch?v=450maTzSIvA ! You might see the following error message if you run out of heap space: ! ! ! The –Xms option on the java command can vary the heap size https://docs.oracle.com/javase/8/docs/technotes/tools/windows/ java.html Heap and Stack memory 20 Play with TestBlocks.java and BlockDemo.java Understand how scoping of variables works. Have lots of nested blocks Experiment with Blocks for IF and For statements Understand how the scope of variables works for these statements Play with SquareInt.java Rename the function to be “halveInt”; make the return type “double” and make the effect of the function to halve the value of the input argument (Note: be careful of the printf format statement in this case.) Ideas for exercises 21 Look at the pass by value and pass-objects-by-value examples. If you have some of your own classes which define objects, then try passing these objects to methods and see that you can change their values in the calling program. Look at the factorial example. Change the return type of the factorial function to be “long”. Change it to be Float. Read the Javadoc documentation on BigInteger and understand how the BigInteger version works. More ideas for exercises.. 22