Procedural programming in Java Procedural programming in Java

  • Slides: 18
Download presentation
Procedural programming in Java

Procedural programming in Java

Procedural programming in Java A simple programming problem Problem definition A program is required

Procedural programming in Java A simple programming problem Problem definition A program is required which will take a string as a command line argument of any length and determine if the string represents a palindrome - i. e. a word which reads the same backwards as forwards. If the word is a palindrome, a message : <command line argument> is a palindrome should be displayed. If not, the message should be : <command line argument> is not a palindrome If the number of command line arguments is not equal to 1 an error message should be displayed.

Procedural programming in Java A simple programming problem Program design word : string *

Procedural programming in Java A simple programming problem Program design word : string * represents command line argument length : integer *represents length of word if there is one command line argument then set word = command line argument set length = length of word for all characters at pos in the first half of the word if char at pos not equal to char at length-pos then exit for end if end for if premature exit from loop then display not a palindrome else display palindrome end if else display error message end if

Procedural programming in Java A simple programming problem Program code public class Palindrome {

Procedural programming in Java A simple programming problem Program code public class Palindrome { public static void main(String [] args) { String word; int length, pos = 0; if ( args. length == 1 ) { word = args[0]; length = word. length(); while ( pos <= length / 2) { if ( word. char. At(pos) != word. char. At(length - 1 - pos)) break; pos++; } //end while if ( pos > length / 2 ) System. out. println(word + “ is a palindrome”); else System. out. println(word + “ is not a palindrome”); } else System. err. println(“One command line argument only”); } //end main } //end class Palindrome

Procedural Programming in Java The square root algorithm The square root of a number

Procedural Programming in Java The square root algorithm The square root of a number can be calculated by taking a series of estimates. Each successive estimate is derived from the previous using the following formula : new estimate = average of (old estimate + value/old estimate) This can be expressed as pseudo-code as follows : value : integer *value who’s square root is required e 1 : real *first estimate e 2 : real *next estimate e 2 = value / 2 *a crude way to start off with a first guess do set e 1 to e 2 set e 2 to (e 1 + value/e 1)/2 while positive difference between e 1 and e 2 > accuracy required Note : In order to repeat the loop, the new estimate must become the old one next time round. Hence the need for - set e 1 to e 2

Procedural Programming in Java Implementation of square-root program public class Square. Root { public

Procedural Programming in Java Implementation of square-root program public class Square. Root { public static void main(String [] args) { int value; double e 1, e 2; value = Integer. parse. Int(args[0]); if (value > 0){ //now we can calculate square root e 2 = value / 2; do { e 1 = e 2; e 2 = (e 1 + value / e 1) / 2; } while (Math. abs(e 1 - e 2) > 0. 0001); System. out. println("Square root = " + e 2); } else System. err. println("Only positive arguments allowed"); } //end main } //end class Square. Root

Procedural Programming in Java Sub-programs in Java It is not desirable or practicable to

Procedural Programming in Java Sub-programs in Java It is not desirable or practicable to place all code in one main program. As programs become larger and perhaps more people are working on them simultaneously, it is necessary to subdivide the code into sub-programs. These are called - subroutines (FORTRAN), procedures (PASCAL), functions (C) or methods (Java), but all refer to sub-programs. Use of methods allow you to : Subdivide a large program into smaller pieces - easier to understand. Allow several people to work on different parts of the program at once. Factor out commonly occurring pieces of code and write only once. Avoid repetition and hence changes only made in one place. Create sub-programs which can be re-used in other applications. Minimise the amount of re-compilation when changes are made.

Procedural Programming in Java Parameterless methods public class Rabbit { public static void main(String

Procedural Programming in Java Parameterless methods public class Rabbit { public static void main(String [] args) { chatter(); //all methods must have a parameter chatter(); //list () even if it is empty chatter(); } //end main public static void chatter(){ System. out. println(“Hello and welcome to Java”); } //end chatter } //end class Rabbit Here, the method chatter() needs no additional information to do its job. Hence, nothing is passed to it as parameters and nothing is returned.

Procedural Programming in Java Method with parameters In this version of chatter(), the method

Procedural Programming in Java Method with parameters In this version of chatter(), the method needs to be told how often to repeat the message - the int repeat in the method header is a formal parameter, the 2 * x and the 5 in the method calls are actual parameters. public class Rabbit { public static void main(String [] args) { int x = 10; chatter(2 * x); //here an actual value (parameter) is passed chatter(5); //to tell the method how often to loop } //end main public static void chatter(int repeat){ for (int i = 0; i < repeat; i++) System. out. println(“Hello and welcome to Java”); } //end chatter } //end class Rabbit

Procedural Programming in Java Method with parameters Here, chatter() needs to be told what

Procedural Programming in Java Method with parameters Here, chatter() needs to be told what to say and how often. Formal parameters consist of lists of data type followed by name - int repeat, String message Actual parameters consist of a corresponding set of values - 5 , args[0] public class Rabbit { public static void main(String [] args) { int x = 10; chatter(x % 3, “Hello there”); //must send both repeat chatter(5, args[0]); //amount and message string } //end main public static void chatter(int repeat, String message){ for (int i = 0; i < repeat; i++) System. out. println(message); } //end chatter } //end class Rabbit

Procedural Programming in Java Method which return results Here, chatter() needs to be told

Procedural Programming in Java Method which return results Here, chatter() needs to be told what to say and how often. Formal parameters consist of lists of data type followed by name - int repeat, String message Actual parameters consist of a corresponding set of values - 5 , args[0] public class Rabbit { public static void main(String [] args) { int val = Integer. parse. Int(args[0]); System. out. println(“Square root = “ + square. Root(val)); } //end main public static double square. Root(int value) { double e 1, e 2 = value / 2; //local variables known only to method do { e 1 = e 2; e 2 = ( e 1 + value / e 1 ) / 2; } while (Math. abs(e 1 - e 2) > 1. 0 e-5); //use of scientific notation return e 2; //result returned to calling program } //end square. Root } //end class Rabbit

Important concept #1 • Divide and Conquer: Break large programs into a series of

Important concept #1 • Divide and Conquer: Break large programs into a series of smaller modules – Helps manage complexity – Makes it easier to build large programs – Makes it easier to debug programs

Important concept #2 • Abstraction: Most of the time, you need to know what

Important concept #2 • Abstraction: Most of the time, you need to know what a method does, but not how it actually does it. – Also helps manage complexity – You use other people’s code without knowing how it does it’s job.

Methods Declarations • Methods – Allow programmers to modularize programs • Makes program development

Methods Declarations • Methods – Allow programmers to modularize programs • Makes program development more manageable • Software reusability • Avoid repeating code – Local variables • Declared in method declaration – Parameters • Communicates information between methods via method calls 2003 Prentice Hall, Inc. All rights reserved.

Method Declarations (cont. ) • General format of method declaration: modifiers return-value-type method-name( parameter

Method Declarations (cont. ) • General format of method declaration: modifiers return-value-type method-name( parameter 1, …, parameter. N ) { declarations and statements } • Method can also return values: return expression; 2003 Prentice Hall, Inc. All rights reserved.

16 Naming your methods • As with variables naming methods is important • You

16 Naming your methods • As with variables naming methods is important • You should give your methods names which clearly describe what the function is doing – helps debugging – helps others read your code • Same rules as naming variables – E. g. public static float calculate. Tax( int sale ) • When you write about a method in an explanation use the parenthesis to indicate you are referencing a method (as opposed to a regular variable): – E. g. //call square. Integer() to calculate the square 2003 Prentice Hall, Inc. All rights reserved.

17 Good programming with methods • A method should do one and only one

17 Good programming with methods • A method should do one and only one useful action – If you see names for your method that suggest multiple actions then it’s time to break it up into separate functions; for example, calculate. Tax. And. Print. Return. And. Save. File(); -ugh • If you do something more than once in a program, you should write a method for that action. 2003 Prentice Hall, Inc. All rights reserved.

18 More Good Programming • If you have written a method to do something

18 More Good Programming • If you have written a method to do something in one project, and you need to do the same action in another project, you should reuse the method. – In Java this is usually accomplished by using classes which we will not cover this semester. 2003 Prentice Hall, Inc. All rights reserved.