School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure.

Slides:



Advertisements
Similar presentations
IT151: Introduction to Programming
Advertisements

University of Palestine software engineering department Introduction to data structures Introduction to java application instructor: Tasneem Darwish.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Your First Java Program: HelloWorld.java
Chapter 1: Introduction
Chapter 2 - Introduction to Java Applications
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
How to Create a Java program CS115 Fall George Koutsogiannakis.
Excerpts from Introduction to Java Programming, 4E Author: Y. Daniel Liang (Copyright by Prentice Hall)
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Introduction to Java Programming, 4E
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 2: 1 CMT1000: Introduction to Programming Ed Currie Lecture 2B: Programming.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 4: Storing and.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Chapter 2 - Introduction to Java Applications
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
Comments are for people Header comments supply basic information about the artifact.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Programming With C.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Introduction to Java Programming with Forte Y. Daniel Liang.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Chapter 2: Java Fundamentals
Comments in Java. When you create a New Project in NetBeans, you'll notice that some text is greyed out, with lots of slashes and asterisks:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to programming in the Java programming language.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Chapter 3 Introduction To Java. OBJECTIVES Packages & Libraries Statements Comments Bytecode, compiler, interpreter Outputting print() & println() Formatting.
Creating a Java Application and Applet
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
CS 177 Recitation Week 1 – Intro to Java. Questions?
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Introduction to Java Programming, 4E Y. Daniel Liang.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
Programming what is C++
String Output ICS 111: Introduction to Computer Science I
Chapter 2 - Introduction to Java Applications
MSIS 655 Advanced Business Applications Programming
IFS410 Advanced Analysis and Design
JOptionPane class.
Computer Programming-1 CSC 111
Chapter 2: Java Fundamentals cont’d
How to Run a Java Program
Presentation transcript:

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 1 CMT1000: Introduction to Programming Ed Currie Lecture 3: Program structure and output

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 2 By now, you should have: (Essential) Read Deitel and Deitel Chapter 1, 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)

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 3 Desirable: Read Deitel and Deitel and 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

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.

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.

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.

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!" ); }

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!" );

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 }

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 );

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.

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

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.

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.

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

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

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.

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.

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.

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

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.

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.

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

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!" ); }

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!" ); }

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.

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 }

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 }

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.

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

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 }

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!

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 }

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.

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 3: 35 Subprograms Stepwise refinement... … versus object-orientation

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

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

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

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! ") }