Download presentation
Presentation is loading. Please wait.
1
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure and output
2
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 2 By now, you should have: (Essential) Read Deitel and Deitel Chapter 1, 2.1 - 2.4 Familiarised yourself with the module web site Logged in to the Oasis module web site Downloaded and executed a program Worked through Units 1 and 2 of the module learning materials Printed out the lecture slides for all lectures so far (including this one)
3
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 3 Desirable: Read Deitel and Deitel 2.5 - 2.7 and 4.11 - 4.13 Worked through Unit 3 of the learning materials Read ‘Computing without Computers’ Chapters 1, 2 and 3 (The above is the ‘week 3’ work) Downloaded the JDK and JCreator to your home machine Downloaded from the Deitel and Deitel CD, and executed, some of the programs from D&D Chapter 2 Completed assessed short programming exercise 1
4
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 4 Input and Output To do anything useful a computer (and so a program) must be able to communicate with the outside world. It needs to be able to print messages to the screen so that the users can read them. There must also be a way of getting information from the keyboard. This is called Input and Output. Programming languages contain special commands for doing it.
5
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 5 A Simple Problem Write a program that wakes up people who are not paying attention by printing a message to the screen.
6
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 6 Solution Plan This is a description in English of what we want the program to do Output to the screen "Wake up at the back!" We must convert the instructions to their equivalent in a programming language before a computer can follow the plan. Below is what we would need to write in the language Java.
7
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 7 // Prints a message on the screen in the // command window public class Wakey1 { public static void main( String args[] ) { System.out.println( ”Wake up at the back!" ); }
8
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 8 Question Which bits of this program are instructions (statements)? Answer The only statement is: System.out.println( ”Wake up at the back!" );
9
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 9 Another solution // Prints a message on the screen in a // dialog box import javax.swing.JOptionPane; // import class JOptionPane public class Wakey2 { public static void main( String args[] ) { JOptionPane.showMessageDialog(null, ”Wake up at the back!" ); System.exit( 0 ); // terminate the program }
10
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 10 Question Which bits of this program are instructions (statements)? Answer JOptionPane.showMessageDialog(null, "Wake up at the back!" ); and System.exit( 0 );
11
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 11 Which program is best? It depends - they both do the job just fine You may find it easier, for the assessed coursework, to use command window output (i.e. Wakey1 style). This makes it easier to print text in columns etc.
12
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 12 Comments All programs should contain comments. –Remarks, insights, wisdom in code without affecting the program The compiler ignores comments A comment should be placed at the start of the program –give origin, purpose and an outline plan Preceded by // and run until the end of the line eg //It does so by printing a message //to the screen
13
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 13 Why Comment Programs? Comments make it easier to make changes They help humans understand the program –What is happening –not how it’s done in detail Pitfalls to look out for when using it –or changing it Too few leave guessing Too many scattered through program obscure the program itself.
14
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 14 Importing things import javax.swing.JOptionPane; is an import statement. Java has a library of predefined program chunks... … Java Applications Programming Interface, or API) We can use them within our own programs... …as we used the pizza base-making algorithm in our pizza recipe.
15
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 15 Importing things These program chunks are called classes Related classes are grouped into packages javax.swing is a package, which contains classes for providing graphical user interfaces for programs JOptionPane is a class within that package, which allows us to display a message dialog box to use any of these API classes within our program, we must include them in an import statement
16
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 16 Classes Our simple programs consists of a single class, defined as follows: public class { } Every Java program will contain at least one class defined by the programmer. public class are Java keywords
17
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 17 Classes Wakey1 or Wakey2 is the name of the class - starts with a capital letter, by convention. For the time being, think of the class as something which gives a name to your program, and encloses your algorithms (methods) between { and }. The name of the file containing your program must be the same as that of the class i.e. the Wakey1 class must be in a file called Wakey1.java.
18
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 18 Methods The methods which implement each algorithm in your program are placed within the class definition. The programs above have only one method, called main. public static void main( String args[] ) { } every Java program must contain a method called main. This represents the main algorithm of the program.
19
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 19 Methods Execution of the program will begin with the first statement in the body of main public static void - keywords ( String args[] ) is a parameter; it allows us to pass information to the program on the command line.
20
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 20 The Method Body Every method has a body (including the main method) The body is where the instructions are for that method The body is enclosed in curly brackets - They always come in pairs! public static void main( String args[] ) { the instructions go here } In Wakey1, the main method contains one instruction; in Wakey2, it has two instructions
21
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 21 Statements Instructions are also called statements Each statement ends with a semicolon – System.exit( 0 ); It is a bit like a full stop in English Its the only way the compiler can tell when one statement finishes and the next starts It is very important –but easily forgotten.
22
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 22 Statements The statement System.exit( 0 ); calls a method exit, which is found in a class called System, which is part of a package of classes called java.lang System does not have to be imported, as java.lang is automatically imported into every Java program. This statement is needed to terminate the program properly, when the program uses a graphical user interface.
23
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 23 Output statements (Command window) System.out is called the standard output object. It has methods for displaying output in the command window. Method System.out.println is such a method –displays a line of text –places the output cursor at the beginning of the next line
24
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 24 System.out.print What does this do? // Prints a message on the screen in the // command window public class Wakey1 { public static void main( String args[] ) { System.out.print( ”Wake up" ); System.out.println( ” at the back!" ); }
25
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 25 Newline character What does this do? // Prints a message on the screen in the // command window public class Wakey1 { public static void main( String args[] ) { System.out.println( ”Wake\nup at\nthe back!" ); }
26
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 26 Output Statements (Dialog boxes) JOptionPane.showMessageDialog(null, ”Wake up at the back!" ); This statement invokes the method showMessageDialog from class JOptionPane, which we imported into our program. This method has two parameters. - null, which is a reference value - The string of text which we wish to be displayed in the dialog box When the program runs, the dialog box displays the string; box disappears when one clicks its OK button.
27
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 27 // Prints greeting on the screen in 3 dialog boxes import javax.swing.JOptionPane; // import class JOptionPane public class Hello2 { public static void main( String args[] ) { JOptionPane.showMessageDialog(null, "Hello!" ); JOptionPane.showMessageDialog(null, "How nice to see you!" ); JOptionPane.showMessageDialog(null, "How are you?" ); System.exit( 0 ); // terminate the program }
28
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 28 Newlines with dialog boxes // Prints greeting on the screen on three lines // in a single dialog box import javax.swing.JOptionPane; // import class JOptionPane public class Hello3 { public static void main( String args[] ) { JOptionPane.showMessageDialog(null, "Hello!\nHow nice to see you!\nHow are you?" ); System.exit( 0 ); // terminate the program }
29
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 29 Subprograms To write a bigger program, we can add more statements to main - the first algorithm executed when we run the program. However, remember our pizza recipe. -Make the base according to the recipe on page 15. We isolated the base-making instructions into a separate algorithm, which could be invoked from any pizza recipe. In Java, we can implement algorithms as named methods, and invoke them by using their names as instructions.
30
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 30 A method declaration It has a similar structure to the main method A line giving its name A body enclosed in curly brackets When execution of a method finishes (i.e. the } is reached), control returns to the original method
31
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 31 // Prints greetings from two separate methods // on the screen import javax.swing.JOptionPane; // import class JOptionPane public class Hello4 { public static void splong() { System.out.println ( "Hello from method splong!"); } public static void spling() { System.out.println ( "Hello from method spling!"); } public static void main( String args[] ) { spling(); splong(); System.exit( 0 ); // terminate the program }
32
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 32 Calling a method spling (); This line says execute the spling method. A method is called by giving as a command its name followed by parentheses () Do not forget the ; on the end!
33
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 33 import javax.swing.JOptionPane; // import class JOptionPane public class Hello5 { public static void splong() { System.out.println (, "Hello from method splong!"); } public static void spling() { System.out.println (, "Hello from method spling!"); splong(); } public static void main( String args[] ) { spling(); splong(); System.exit( 0 ); // terminate the program }
34
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 34 Question What will be the output from this program? Answer Successive lines with the messages - Hello from method spling! (called from main) -Hello from method splong! (called from spling) -Hello from method splong! (called from main) There are two ways of finding the answer to this question. -Run the program, and see what the output is. -(quicker) dry-run the program. You act as the computer.
35
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 35 Subprograms Stepwise refinement... … versus object-orientation
36
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 36 An Outline Program import javax.swing.JOptionPane; // import class JOptionPane if using dialog boxes public class CLASSNAME // also filename { public static void METHODX() { STATEMENTS FOR METHOD X } MORE METHODS IF NEEDED public static void main( String args[] ) { STATEMENTS FOR MAIN ALGORITHM System.exit( 0 ); // terminate the program } Replace stuff in capitals as necessary
37
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 37 Program Layout Blank lines and indentation are very important –they make a program easier to read –like paragraphs in English Blank lines should be used to separate distinct parts of the program Bad layout is a style error –it may work –but will be hard to change –as it will be hard to understand it
38
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 38 Indentation The curly brackets round the body of a method should be –aligned with method heading, and –on a separate line The statements in the body itself should be indented This helps make the body stand out Makes program MUCH easier to read
39
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 39 Exercise: Which of the following (identical) methods is easiest to read? public static void splong() { JOptionPane.showMessageDialog(null, "Hello from method splong!"); } public static void splong() { JOptionPane.showMessageDialog(null, "Hello from method splong! ") }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.