24F_W01_C01_v1.2.pdf
Document Details
Uploaded by AttentiveProsperity
Full Transcript
Chapter 1 Introduction to Computers, Programs, and Java Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved....
Chapter 1 Introduction to Computers, Programs, and Java Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 1 Objectives Introduction Computer Systems: Hardware and Software Why Program? Programming Languages What Is a Program Made Of? basic syntax of a Java program Errors Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 2 Introduction This course teaches programming using Java – Java is a powerful language that runs on practically every type of computer. – Java can be used to create large applications or small programs, known as applets, that are part of a Web site Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. What is a Computer? A computer consists of a CPU, memory, hard disk, monitor, printer, and communication devices. Bus Storage Communication Input Output Devices Memory CPU Devices Devices Devices e.g., Disk, CD, e.g., Modem, e.g., Keyboard, e.g., Monitor, and Tape and NIC Mouse Printer Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 4 What is a Computer? What is the primary difference between memory and a storage device? Memory is volatile, because information is lost when the power is turned off. Programs and data are permanently stored on storage devices and are moved, when the computer actually uses them, to memory, which is much faster than storage devices. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 5 What is a Computer? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 6 How Data is Stored? Memory address Memory content Data of various kinds,.. such as numbers,.. characters, and strings,.. are encoded as a series of 2000 01001010 Encoding for character ‘J’ bits (zeros and ones). 2001 01100001 Encoding for character ‘a’ 2002 01110110 Encoding for character ‘v’ 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 7 Programs and Programming Languages Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 8 Programs Computer programs are set of instructions that tell computers what to do. Without programs, a computer is an empty machine. Programs are written using programming languages. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 9 Programming Languages A programming language is a special language used to write computer programs. Programming languages were invented to ease the task of programming Use words instead of numbers. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 10 Programming Languages What language does the CPU understand? The machine language is a set of primitive instructions built into every computer. This is the language understood by a computer and executed by a computer. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 11 Popular Programing Languages Language Description BASIC Beginner’s All-purpose Symbolic Instruction Code. It was designed to be learned and used easily by beginners. C Developed at Bell Laboratories. C combines the power of an assembly language with the ease of use and portability of a high-level language. C++ C++ is an object-oriented language, based on C. C# Pronounced “C Sharp.” It is a hybrid of Java and C++ and was developed by Microsoft. COBOL COmmon Business Oriented Language. Used for business applications. FORTRAN FORmula TRANslation. Popular for scientific and mathematical applications. Java Developed by Sun Microsystems, now part of Oracle. It is widely used for developing platform- independent Internet applications. Pascal Named for Blaise Pascal, who pioneered calculating machines in the seventeenth century. It is a simple, structured, general-purpose language primarily for teaching programming. Python A simple general-purpose scripting language good for writing short programs. Visual Visual Basic was developed by Microsoft and it enables the programmers to rapidly develop Basic graphical user interfaces. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 12 high-level programming language What is a high-level programming language? The high-level languages are English-like and easy to learn and program. What is a source program? The program written in a programming language is called a source program. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 13 Compiling Source Code A program written in a programing language is called a source program or source code. A source program must be translated into machine code for execution. The translation can be done using another programming tool called a compiler. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 14 Compiling Source Code A compiler translates the entire source code into a machine-code file, and the machine-code file is then executed. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 15 Java Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 16 Why Java? The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future of computing is being profoundly influenced by the Internet, and Java promises to remain a big part of that future. Java is the Internet programming language. Java is a general purpose programming language. Java is the Internet programming language. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 17 Java, Web, and Beyond Java can be used to develop standalone applications. Java can be used to develop applications running from a browser. Java can also be used to develop applications for hand-held devices. Java can be used to develop applications for Web servers. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 18 The Java Development Cycle When you finish designing a program and writing the Java code that implements your design, you must compile and execute your program. This is called the Java development cycle, Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 19 JDK and JRE What does JDK stand for? What does JRE stand for? JDK stands for Java Development Toolkit. JRE stands for Java Runtime Environment. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 20 Popular Java IDEs NetBeans Eclipse IntelliJ IDE Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 21 Integrated Development Environments (IDE) Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Integrated Development Environments (IDE) Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. A Simple Java Program Listing 1.1 // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 24 Anatomy of a Java Program Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 25 Anatomy of a Java Program Class name Main method Statements Statement terminator Reserved words Comments Blocks Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 26 Class Name Every Java program must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 27 Main Method In order to run a class, the class must contain a method named main. The program is executed from the main method. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 28 Statement A statement represents an action or a sequence of actions. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 29 Statement Terminator Every statement in Java ends with a semicolon (;). // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 30 Reserved words Reserved words or keywords are words that have a specific meaning to the compiler. They cannot be used for other purposes in the program ― For example, when the compiler sees the word class, it understands that the word after class is the name for the class. // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 31 Java Key words You cannot use any of the following as identifiers in your programs 32 Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Java Key words Is Java case sensitive? What is the case for Java keywords? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 33 Java Key words Is Java case sensitive? What is the case for Java keywords? – Java source code is case sensitive. – Java keywords are always in lowercase. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 34 Blocks A pair of braces in a program forms a block that groups components of a program. public class Test { public static void main(String[] args) { Class block System.out.println("Welcome to Java!"); Method block } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 35 Special Symbols Character Name Description {} Opening and closing Denotes a block to enclose statements. braces () Opening and closing Used with methods. parentheses [] Opening and closing Denotes an array. brackets // Double slashes Precedes a comment line. " " Opening and closing Enclosing a string (i.e., sequence of characters). quotation marks ; Semicolon Marks the end of a statement. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 36 Special Symbols // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Character Name Description {} Opening and closing Denotes a block to enclose statements. braces () Opening and closing Used with methods. parentheses [] Opening and closing Denotes an array. brackets // Double slashes Precedes a comment line. 37 " " Opening and closing Enclosing a string (i.e., sequence of characters). quotation marks ; Semicolon Marks the end of a statement. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. Output? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 38 Output? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 39 Programming Style and Documentation Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 40 Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 41 Appropriate Comments Include a summary at the beginning of the program to explain what the program does. Include your name, class section, instructor, date, and a brief description at the beginning of the program. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 42 Naming Conventions Choose meaningful and descriptive names. More on next chapter. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 43 Proper Indentation and Spacing Indentation – Indent two spaces. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 44 Proper Indentation and Spacing Indentation – Indent two spaces. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 45 Proper Indentation and Spacing Spacing – Use blank line to separate segments of the code. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 46 Block Styles Use end-of-line style for braces. Next-line public class Test style { public static void main(String[] args) { System.out.println("Block Styles"); } } End-of-line style public class Test { public static void main(String[] args) { System.out.println("Block Styles"); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 47 Programming Errors Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 48 Programming Errors Syntax Errors – Detected by the compiler Runtime Errors – Causes the program to abort Logic Errors – Produces incorrect result Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 49 Syntax Errors public class ShowSyntaxErrors { public static void main(String[] args) { System.out.println("Welcome to Java); } } Any idea? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 50 Syntax Errors public class ShowSyntaxErrors { public static void main(String[] args) { System.out.println("Welcome to Java); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 51 Syntax Errors Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 52 Syntax Errors Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 53 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { System.out.println(1 / 0); } } Any idea about this error? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 54 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { System.out.println(1 / 0); } } Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 55 Logic Errors public class ShowLogicErrors { public static void main(String[] args) { System.out.println("Celsius 35 is Fahrenheit degree "); System.out.println((9 / 5) * 35 + 32); } } Is it the formula? Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 56 Logic Errors public class ShowLogicErrors { public static void main(String[] args) { System.out.println("Celsius 35 is Fahrenheit degree "); System.out.println((9 / 5) * 35 + 32); } } Is it the formula? well.. partially Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved. 57