Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
www.site.uottawa.ca/~elsaddik CSI 1102 Introduction to Software Design Prof. Dr.-Ing. Abdulmotaleb El Saddik University of Ottawa (SITE 5-037) (613) 562-5800 x 6277 elsaddik @ site.uottawa.ca abed @ mcrlab.uottawa.ca http://www.site.uottawa.ca/~elsaddik/ 1 (c) elsaddik www.site.uottawa.ca/~elsaddik Learning objectives 2 (c) elsaddik • Brief review of the basic computer processing concepts • Understand what problem solving entails • Understand why problem solving skills are so important • Describe the various levels of programming languages • Understand a first Java program and its basic structure • Source: Sections 1.3-1.5, Chapter 1 (L&L) A brief review of Networking Read through Sections 1.0-1.2 to ensure you are “up to date” www.site.uottawa.ca/~elsaddik PC Penetration Worldwide 1400 1200 Millions 1000 800 PC's Population 600 400 200 4 (c) elsaddik Ch in a ia In d an y Ge rm Fr an ce UK US A 0 Sources: IDC, UN www.site.uottawa.ca/~elsaddik Hardware and Software Hardware • the physical, tangible parts of a computer • keyboard, monitor, disks, wires, chips, etc. Software • programs and data • a program is a series of instructions A computer requires both hardware and software Each is essentially useless without the other 5 (c) elsaddik www.site.uottawa.ca/~elsaddik Software Categories Operating System • controls all machine activities • provides the user interface to the computer • manages resources such as the CPU and memory • Windows XP, Windows 2000, Unix, Linux, Mac OS Application program • generic term for any other kind of software • word processors, missile control systems, games Most operating systems and application programs have a graphical user interface (GUI) 6 (c) elsaddik www.site.uottawa.ca/~elsaddik Networks A network is two or more computers that are connected so that data and resources can be shared Most computers are connected to some kind of network Each computer has its own network address, which uniquely identifies it among the others A file server is a network computer dedicated to storing programs and data that are shared among network users 7 (c) elsaddik www.site.uottawa.ca/~elsaddik Network Connections Each computer in a network could be directly connected to every other computer in the network These are called point-to-point connections Adding a computer requires a new communication line for each computer already in the network This technique is not practical for more than a few close machines 8 (c) elsaddik www.site.uottawa.ca/~elsaddik Network Connections Most networks share a single communication line Adding a new computer to the network is relatively easy Network traffic must take turns using the line, which introduces delays 9 (c) elsaddik Often information is broken down in parts, called packets, which are sent to the receiving machine and then reassembled www.site.uottawa.ca/~elsaddik Local-Area Networks A Local-Area Network (LAN) covers a small distance and a small number of computers LAN A LAN often connects the machines in a single room or building 10 (c) elsaddik www.site.uottawa.ca/~elsaddik Wide-Area Networks A Wide-Area Network (WAN) connects two or more LANs, often over long distances LAN LAN 11 (c) elsaddik A LAN usually is owned by one organization, but a WAN often connects groups in different countries www.site.uottawa.ca/~elsaddik The Internet The Internet is a WAN which spans the entire planet The word Internet comes from the term internetworking, which implies communication among networks It started as a United States government project, sponsored by the Advanced Research Projects Agency (ARPA) - originally it was called the ARPANET The Internet grew quickly throughout the 1980s and 90s 12 (c) elsaddik Less than 600 computers were connected to the Internet in 1983; by the year 2000 there were over 10 million www.site.uottawa.ca/~elsaddik The World Wide Web The World Wide Web is just ONE application of the Internet • allows many different types of information to be accessed using a common interface A browser is a program which accesses and presents information • text, graphics, video, sound, audio, executable programs A Web document usually contains links to other Web documents, creating a hypermedia environment The term Web comes from the fact that information is not organized in a linear fashion Other Internet applications: 13 (c) elsaddik • E-mail, Telnet, FTP, etc… www.site.uottawa.ca/~elsaddik The World Wide Web Web documents are often defined using the HyperText Markup Language (HTML) Information on the Web is found using a Uniform Resource Locator (URL): http://www.uottawa.ca http://www.site.uottawa.ca/index.html ftp://java.sun.com/applets/animation.zip A URL indicates a protocol (http), a domain, and possibly specific documents 14 (c) elsaddik 700 600 Millions of Users www.site.uottawa.ca/~elsaddik People on the Internet (Worldwide) 500 400 Outside US North America 300 200 100 03 20 02 20 01 20 00 20 99 19 98 19 97 19 96 19 19 95 0 Source: IDC 15 (c) elsaddik 16 (c) elsaddik www.site.uottawa.ca/~elsaddik Programming & Programming Languages Source: Sections 1.3-1.5, Chapter 1 (L&L) www.site.uottawa.ca/~elsaddik So what is Problem Solving? 18 (c) elsaddik The purpose of writing a program is to solve a problem The general steps in problem solving are: • • • • • • Understand the problem Dissect the problem into manageable pieces Design a solution Consider alternatives to the solution and refine it Implement the solution Test the solution and fix any problems that exist www.site.uottawa.ca/~elsaddik Problem Solving: “Divide and Conquer” Many software projects fail because the developer didn't really understand the problem to be solved We must avoid assumptions and clarify ambiguities As problems and their solutions become larger, we must organize our development into manageable pieces: “Divide and Conquer” This technique is fundamental to software development 19 (c) elsaddik www.site.uottawa.ca/~elsaddik Object-oriented approach to Problem Solving 20 (c) elsaddik We will dissect our solutions into pieces called classes and objects, taking an object-oriented approach “If you want to eat an elephant, take one bite at a time”. www.site.uottawa.ca/~elsaddik Problem solving through a programming language So suppose we have a problem to be solved We choose to design and implement a computer program to solve the problem We use a programming language to do the job for us A programming language • specifies the words and symbols that we can use to write a program • employs a set of rules that dictate how the words and symbols can be put together to form valid program statements Examples of programming languages: • Fortran, Cobol, C++, C, Delphi, Pascal, Smalltalk and JAVA 21 (c) elsaddik www.site.uottawa.ca/~elsaddik Different Programming Language Levels There are four programming language levels: • machine language • assembly language • high-level language • fourth-generation language Each type of CPU has its own specific machine language The other levels were created to make it easier for a human being to read and write programs 22 (c) elsaddik www.site.uottawa.ca/~elsaddik Basic Program Development Edit and save program errors errors Compile program Execute program and evaluate results 23 (c) elsaddik www.site.uottawa.ca/~elsaddik Problem solving using JAVA The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since It is an object-oriented language 24 (c) elsaddik www.site.uottawa.ca/~elsaddik Java Program Structure In the Java programming language: • A program is made up of one or more classes • A class contains one or more methods • A method contains program statements These terms will be explored in detail throughout the course, starting from next week A Java application always contains a method called main See Lincoln.java (page 30) 25 (c) elsaddik www.site.uottawa.ca/~elsaddik Java Program Structure // comments about the class public class MyProgram { class header class body Comments can be added almost anywhere } 26 (c) elsaddik www.site.uottawa.ca/~elsaddik Java Program Structure // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { method body } } 27 (c) elsaddik method header www.site.uottawa.ca/~elsaddik Lincoln.java //*********************************************************** *** // Lincoln.java Author: Lewis and Loftus // // Demonstrates the basic structure of a Java application. //*********************************************************** *** public class Lincoln { //-------------------------------------------------------// Prints a presidential quote. //-------------------------------------------------------public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one."); } 28 (c) elsaddik } www.site.uottawa.ca/~elsaddik About programming language: They all have the following in common Basic Components • Comments • Identifiers • (Reserved Words) • Symbols (e.g. <, >, =, …) • White Spaces Syntax and Semantics Translation Errors 29 (c) elsaddik www.site.uottawa.ca/~elsaddik What are Comments? Comments in a program are called inline documentation They should be included to explain the purpose of the program and describe processing steps They do not affect how a program works Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks /** this is a javadoc comment 30 (c) elsaddik */ */ www.site.uottawa.ca/~elsaddik The Importance of Comments Describe WHAT, WHY, HOW and by WHOM Very important for further use • To understand years from now • Developer’s turnover high Do not add them at the end!!!! Without comments is very difficult to maintain code 31 (c) elsaddik www.site.uottawa.ca/~elsaddik Lincoln.java //*********************************************************** // Lincoln.java Author: Lewis and Loftus // // Demonstrates the basic structure of a Java application. //*********************************************************** Comments public class Lincoln { //-------------------------------------------------------// Prints a presidential quote. //-------------------------------------------------------public static void main (String[] args) { System.out.println ("A quote by Abraham Lincoln:"); System.out.println ("Whatever you are, be a good one."); } } 32 (c) elsaddik www.site.uottawa.ca/~elsaddik Identifiers Identifiers are the words a programmer uses in a program An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign Identifiers cannot begin with a digit Java is case sensitive - Total, total, and TOTAL are different identifiers By convention, Java programmers use different case styles for different types of identifiers, such as • title case for class names - Lincoln 33 (c) elsaddik • upper case for constants - MAXIMUM www.site.uottawa.ca/~elsaddik Identifiers Sometimes we choose identifiers ourselves when writing a program (such as Lincoln) Sometimes we are using another programmer's code, so we use the identifiers that they chose (such as println) Often we use special identifiers called reserved words that already have a predefined meaning in the language • A reserved word cannot be used in any other way • E.g. IF (temp < 30) THEN 34 (c) elsaddik www.site.uottawa.ca/~elsaddik Reserved Words 35 (c) elsaddik The Java reserved words: abstract boolean break byte case catch char class const continue default do double else extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while www.site.uottawa.ca/~elsaddik About Syntax and Semantics The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program E.g. if (height > tallest) then { tallest = height; } The semantics of a program statement define what that statement means (its purpose or role in a program) E.g. We want to determine the tallest person. 36 (c) elsaddik www.site.uottawa.ca/~elsaddik About Syntax and Semantics A program that is syntactically correct is not necessarily logically (semantically) correct A program will always do what we tell it to do, not what we meant to tell it to do !!! Typical error incorrect boundaries E.g. (height > tallest) instead of (height >= tallest) 37 (c) elsaddik www.site.uottawa.ca/~elsaddik What are White Spaces? Spaces, blank lines, and tabs are called white space White space is used to separate words and symbols in a program Extra white space is ignored A valid Java program can be formatted in many ways Programs should be formatted to enhance readability, using consistent indentation • See Lincoln2.java (page 37) • See Lincoln3.java (page 38) 38 (c) elsaddik www.site.uottawa.ca/~elsaddik A BADLY structured Java example //*************************************************** // Lincoln2.java Author: Lewis and Loftus // // Demonstrates a poorly formatted, though valid, program. //********************************************************* public class Lincoln2{public static void main(String[]args){ System.out.println("A quote by Abraham Lincoln:"); System.out.println("Whatever you are, be a good one.");}} 39 (c) elsaddik www.site.uottawa.ca/~elsaddik Lincoln3.java //******************************************************************** // Lincoln3.java Author: Lewis and Loftus // // Demonstrates another valid program that is poorly formatted. //******************************************************************** public Lincoln3 class { public static void main ( String [] args 40 (c) elsaddik ) { System.out.println ( "A quote by Abraham Lincoln:" ) ; System.out.println ( "Whatever you are, be a good one." ) ; } } www.site.uottawa.ca/~elsaddik Translation of Programming Languages A program must be translated into machine language before it can be executed on a particular type of CPU This can be accomplished in several ways A compiler is a software tool which translates source code into a specific target language Often, that target language is the machine language for a particular CPU type The Java approach is somewhat different 41 (c) elsaddik www.site.uottawa.ca/~elsaddik Translation in Java Java source code Java compiler Java bytecode Java interpreter Bytecode compiler Machine code 42 (c) elsaddik www.site.uottawa.ca/~elsaddik Java Compiling: •javac <Identifier.java> • javac Lincoln.java •You will get: Lincoln.class Executing: •java <Identifier> • java Lincoln 43 (c) elsaddik www.site.uottawa.ca/~elsaddik Java Translation The Java compiler translates Java source code into a special representation called bytecode Java bytecode is not the machine language for any traditional CPU Another software tool, called an interpreter, translates bytecode into machine language and executes it Therefore the Java compiler is not tied to any particular machine Java is considered to be architecture-neutral 44 (c) elsaddik www.site.uottawa.ca/~elsaddik About Errors A program can have three types of errors • The compiler will find syntax errors and other basic problems (compile-time errors) • If compile-time errors exist, an executable version of the program is not created • A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) • A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors) 45 (c) elsaddik www.site.uottawa.ca/~elsaddik Creating your program: Java Development Environments • • • • • • • • Sun Java Development Kit (JDK) Sun Forte for Java Borland JBuilder MetroWerks CodeWarrior Microsoft Visual J++ Symantec Café Monash BlueJ RealJ (www.realj.com) Used in Lab B02 Though the details of these environments differ, the basic compilation and execution process is essentially the same 46 (c) elsaddik www.site.uottawa.ca/~elsaddik Introduction to Graphics The last one or two sections of each chapter of the textbook focus on graphical issues Most computer programs have graphical components A picture or drawing must be digitized for storage on a computer A picture consists of pixels, and each pixel is stored separately 47 (c) elsaddik www.site.uottawa.ca/~elsaddik Representing Color A black and white picture can be stored using one bit per pixel (0 = white and 1 = black) A colored picture requires more information; there are several techniques for representing colors For example, every color can be represented as a mixture of the three additive primary colors Red, Green, and Blue In Java, each color is represented by three numbers between 0 and 255 that collectively are called an RGB value 48 (c) elsaddik www.site.uottawa.ca/~elsaddik Coordinate Systems Each pixel can be identified using a twodimensional coordinate system When referring to a pixel in a Java program, we use a coordinate system with the origin in the top-left corner (0, 0) 112 40 (112, 40) Y 49 (c) elsaddik X www.site.uottawa.ca/~elsaddik Summary of Lecture Lecture has focused on programming and programming languages Students should now Understand what problem solving entails Understand why problem solving skills are so important Describe the various levels of programming languages Understand a first Java program and its basic structure 50 (c) elsaddik 51 (c) elsaddik www.site.uottawa.ca/~elsaddik