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
Programming Fundamentals I (COSC1336), Lecture 1 (prepared after Chapter 1 of Liang’s 2011 textbook) Stefan Andrei 5/3/2017 COSC-1336, Lecture 1 1 Programming Fundamentals I COSC-1336-01 is a 3 credit points module: Assessments/Tutorials: 10% Assignments: 25% First Lesson exam: 5% (open book) Mid-term exam: 20% (open book) Written final exam: 40% (open book) Lectures: 11:15am-12:05pm, room 107, on MWF New schedule: 11:05am-12:05pm – do you agree with this new schedule? Module homepage: http://galaxy.lamar.edu/~sandrei/cosc-1336-01 5/3/2017 COSC-1336, Lecture 1 2 Course Etiquette Code of conduct no copying for details, see webpage You are encouraged to attend to all lectures, tutorials, and so on. You are encouraged to ask questions. You are encouraged to offer answers. 5/3/2017 COSC-1336, Lecture 1 3 Contact Information and Textbooks Associate Professor: Lectures based on the book: Dr. Stefan Andrei, [email protected] Office hours: 10:00am-11:00am, 1:30pm-2:30pm on MWF, and anytime by email appointment Liang: Introduction to Java programming: Eight Edition, 2011 Pearson Education, 0132130807 Other recommended books: John Lewis, Peter DePasquale, and Joe Chase: JAVA Foundations, Addison Wesley, 2008, ISBN: 0321429729 5/3/2017 COSC-1336, Lecture 1 4 Overview of This Course 1. Introduction to Computers, Programs, and Java (Chapter 1) 2. Elementary Programming (Chapter 2) 3. Selections (Chapter 3) 4. Loops (Chapter 4) 5. Methods (Chapter 5) 6. Arrays (Chapter 6) 7. Multidimensional Arrays (Chapter 7) 8. Objects and Classes (Chapter 8) 9. Strings and Text I/O (Chapter 9) 10. Thinking in Objects (Chapter 10) 5/3/2017 COSC-1336, Lecture 1 5 Course Objectives This is an introductory course for fundamental concepts of programming in terms of JAVA. Topics include introduction to the JAVA programming language, data and expressions, classes and objects, conditionals, loops and arrays. This course will work together with a laboratory section COSC 1173. 5/3/2017 COSC-1336, Lecture 1 6 Lecture Structure Reminder of last lecture Overview Content (new notions + examples) Summary Reading suggestions Coming up next 5/3/2017 COSC-1336, Lecture 1 7 Few things about LU (instead of reminder of last lecture) Lamar University, a member of The Texas State University System, is accredited by the Commission on Colleges of the Southern Association of Colleges and Schools (SACS) to award associate, baccalaureate, masters, and doctorate degrees (www.lamar.edu) Computer Science Department, part of College of Arts and Sciences, is accredited by Accreditation Board for Engineering and Technology (ABET) (cs.lamar.edu) LU ACM (Association of Computing Machinery) acm.lamar.edu 5/3/2017 COSC-1336, Lecture 1 8 2008 Beaumont Enterprise article • The ACM Regional Programming Contest (October 17-18, 2008). • Programming language: Java or C++. • The ‘Birds of Prey’ team: 19th out of 68. • James Carnley • Ricky Guidry • Jacob Wallace • The ‘Cardinals’ team: 55th out of 68. • Ran Qi • Suman Rajbhandari • Vishal Patel • Coach: Dr. Stefan Andrei. • Join us to represent LU at this contest. 5/3/2017 COSC-1336, Lecture 1 9 Lamar University in 2010 1. 2. According to the Local Programming Contest, we selected two teams: ‘The Exceptions’ team: Caleb Fenton, Zebulun Barnett, Yogesh Sangi. ‘Cardinals’ team: Ethan Hasson, Judson Ward, Rishi Dhanju. 5/3/2017 Coach: Dr. Stefan Andrei. Faculty Trainers: Dr. Stefan Andrei and Prof. Myers Foreman Training sessions: September and October for 4-6 hours per week. We selected one of the best textbook for programming contest training [Skiena, 2008]. COSC-1336, Lecture 1 10 The results were very good ‘The Exceptions’ got 16th out of 71 teams (solving 5 exercises out of 8). ‘Cardinals’ got 52nd (solving 3 exercises out of 8). ‘The Exceptions’ team outperformed: 5/3/2017 All teams competing at the LSU site. Many teams from Oklahoma and Texas, including all teams representing Texas Christian University, Southern Methodist University and the University of Houston. COSC-1336, Lecture 1 11 The ‘Top at Site’ Award 5/3/2017 COSC-1336, Lecture 1 12 Lamar University news 5/3/2017 COSC-1336, Lecture 1 13 Computer Science is not only about … Word processors Spreadsheets Web browsers Beaubouef, T., Mason, J.: Why the high attrition rate for Computer Science students: some thoughts and observations. In ACM SIGCSE Bulletin 37 (2), pp. 103-106, 2005 5/3/2017 COSC-1336, Lecture 1 14 Overview of This Lecture To review computer basics, programs, and operating systems (§§1.2-1.4). To explore the relationship between Java and the World Wide Web (§1.5). To distinguish the terms API, IDE, and JDK (§1.6). To write a simple Java program (§1.7). To display output on the console (§1.7). To explain the basic syntax of a Java program (§1.7). To create, compile, and run Java programs (§1.8). (GUI) To display output using the JOptionPane output dialog boxes (§1.9). 5/3/2017 COSC-1336, Lecture 1 15 What is a Computer? A computer consists of a Central Processing Unit (CPU), memory, hard disk, floppy disk, monitor, printer, and communication devices. Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 16 CPU The CPU is the “brain” of a computer. It retrieves instructions from memory and executes them. The CPU speed is measured in megahertz (MHz), and 1 megahertz means 1 million pulses per second. The speed of the CPU has been improved continuously. If you buy a PC now, you can get an Intel Pentium 4 Processor at 3 gigahertz (1 gigahertz is 1000 megahertz). Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 17 Memory The memory stores data and program instructions to be send to CPU for execution. A memory unit is an ordered sequence of bytes, each holds eight bits. A program and its data must be brought to memory before they can be executed. The current content of a memory byte is lost whenever new information is placed in it. Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 18 How Data is Stored? Data of various kinds, such as numbers, characters, and strings, are encoded as a series of bits (zeros and ones). Computers use zeros and ones because digital devices have two stable states, which are referred to as zero and one by convention. Memory address Memory content . . . . . . 2000 01001010 Encoding for character ‘J’ 2001 01100001 Encoding for character ‘a’ 2002 01110110 Encoding for character ‘v’ 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 The programmers need not to be concerned about the encoding and decoding of data, which is performed automatically by the system based on the encoding scheme. 5/3/2017 COSC-1336, Lecture 1 19 How Data is Stored? (cont) The encoding scheme varies. Example: character ‘J’ is represented by 01001010 in one byte. A small number such as 3 can be stored in a single byte. If computer needs to store a large number that cannot fit into a single byte, it uses a number of adjacent bytes. No two data can share or split the same byte. A byte is the minimum storage unit. 5/3/2017 COSC-1336, Lecture 1 20 Storage Devices Memory is volatile, because information is lost when the power is off. Programs and data are permanently stored on storage devices and are moved to memory when the computer actually uses them. There are three main types of storage devices: Disk drives (hard disks and floppy disks), CD drives (CD-R and CD-RW), and Tape drives. Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 21 Output Devices: Monitor The monitor displays information (text and graphics). The resolution and dot pitch determine the quality of the display. Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 22 Monitor Resolution resolution The resolution specifies the number of pixels per square inch. Pixels (short for “picture elements”) are tiny dots that form an image on the screen. The resolution can be set manually. The higher the resolution, the sharper and clearer the image is. However, the image may be very small if you set high resolution on a small screen monitor. 5/3/2017 COSC-1336, Lecture 1 23 Monitor Resolution and Dot Pitch resolution PC monitors are usually 15-inch, 17-inch, 19-inch, or 21-inch. For a 15-inch monitor, a comfortable resolution setting would be 640480 (307,200 pixels). dot pitch The dot pitch is the amount of space between pixels. The smaller the dot pitch, the better the display. 5/3/2017 COSC-1336, Lecture 1 24 Communication Devices A regular modem uses a phone line and can transfer data in a speed up to 56,000 bps (bits per second). A DSL (digital subscriber line) also uses a phone line and can transfer data in a speed 20 times faster than a regular modem. A cable modem uses the TV cable line maintained by the cable company. A cable modem is as fast as a DSL. Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 25 Communication Devices A Network Interface Card (NIC) is a device to connect a computer to a Local Area Network (LAN). The LAN is commonly used in business, universities, and government organizations. A typical type of NIC, called 10BaseT, can transfer data at 10 mbps (million bits per second). Bus Storage Devices e.g., Disk, CD, and Tape 5/3/2017 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer COSC-1336, Lecture 1 26 Input Devices An input device is any peripheral (piece of computer hardware equipment) used to provide data and control signals to a computer. Examples (traditional devices): Keyboard, Mouse, Scanner. Examples (pointing devices, that is, any human interface device that allows a user to input spatial data to a computer): 5/3/2017 Touch-screen, 3D mouse, Joystick, and so on. COSC-1336, Lecture 1 27 Programs Computer programs, known as software, are instructions to the computer. You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them. Programs are written using programming languages. 5/3/2017 COSC-1336, Lecture 1 28 Programming Languages Machine Language Assembly Language High-Level Language Machine language is a set of primitive instructions built into every computer. The instructions are in the form of binary code, so you have to enter binary codes for various instructions. Programming with native machine language is a tedious process. 5/3/2017 COSC-1336, Lecture 1 29 Example: Machine language Moreover the programs are highly difficult to read and modify. For example, to add two numbers, you might write an instruction in binary like this: 5/3/2017 1101101010011010 COSC-1336, Lecture 1 30 Programming Languages Machine Language Assembly Language High-Level Language Assembly languages were developed to make programming easy. Since the computer cannot understand assembly language, however, a program called assembler is used to convert assembly language programs into machine code. 5/3/2017 COSC-1336, Lecture 1 31 Example: assembly language For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3 Assembly Source File … ADDF3 R1, R2, R3 … 5/3/2017 Machine Code File Assembler COSC-1336, Lecture 1 … 1101101010011010 … 32 Programming Languages Machine Language Assembly Language High-Level Language The high-level languages are English-like and easy to learn and program. Example 1: x = y + z; Example 2: the following is a high-level language statement that computes the (approximate) area of a circle with radius 5: area = 5 * 5 * 3.1415; 5/3/2017 COSC-1336, Lecture 1 33 Popular High-Level Languages COBOL (COmmon Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) C (whose developer designed B first) Visual Basic (Basic-like visual language developed by Microsoft) Delphi (Pascal-like visual language developed by Borland) C++ (an object-oriented language, based on C) C# (a Java-like language developed by Microsoft) Java (We use it in these lectures) 5/3/2017 COSC-1336, Lecture 1 34 Compiling Source Code A program written in a high-level language is called a source program (a.k.a., source file or source code). Since a computer cannot understand a source program, a program called a compiler is used to translate the source program into a machine language program called an object program. Source File Compiler Machine-language File Linker Executable File Library Code 5/3/2017 COSC-1336, Lecture 1 35 Compiling Source Code (cont.) The object program is often then linked with other supporting library code before the object can be executed on the machine. Source File Compiler Machine-language File Linker Executable File Library Code 5/3/2017 COSC-1336, Lecture 1 36 Operating Systems The Operating System (OS) is a program that manages and controls a computer’s activities. The most common operating system is Windows. Here are some recent versions: Windows 95, 98, NT, 2000, XP, 7. 5/3/2017 COSC-1336, Lecture 1 User Application Programs Operating System Hardware 37 Operating Systems (cont.) Windows is currently the most popular PC operating system. Application programs such as an Internet browser and a word processor cannot run without an operating system. 5/3/2017 COSC-1336, Lecture 1 User Application Programs Operating System Hardware 38 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. Java is a general purpose programming language. 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. 5/3/2017 COSC-1336, Lecture 1 39 Java, Web, and Beyond Java can be used to develop client-server Web applications, such as Java Applets and Java Servlets. Java can also be used to develop applications for hand-held devices such as Palm and cell phones. 5/3/2017 COSC-1336, Lecture 1 40 Examples of Java’s Versatility (Applets) 5/3/2017 COSC-1336, Lecture 1 41 PDA and Cell Phone 5/3/2017 COSC-1336, Lecture 1 42 Java’s History James Gosling and Sun Microsystems Oak Java, May 20, 1995, Sun World HotJava was the first browser to support Java applets, and was Sun's demonstration platform for the then new technology. Early History Website: http://java.sun.com/features/1998/05/birthday.html 5/3/2017 COSC-1336, Lecture 1 43 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is Architecture-Neutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic www.cs.armstrong.edu/liang/intro8e/JavaCharacteristics.pdf 5/3/2017 COSC-1336, Lecture 1 44 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Java is partially modeled on C++, but greatly simplified and improved. Some people refer to Java as "C++--" because it is like C++ but with more functionality and fewer negative aspects. COSC-1336, Lecture 1 45 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Java is inherently object-oriented. Although many object-oriented languages began strictly as procedural languages, Java was designed from the start to be object-oriented. Objectoriented programming (OOP) is a popular programming approach that is replacing traditional procedural programming techniques. One of the central issues in software development is how to reuse code. Object-oriented programming provides great flexibility, modularity, clarity, and reusability through encapsulation, inheritance, and polymorphism. COSC-1336, Lecture 1 46 Object-Oriented Programming Java is an object-oriented programming language. As the term implies, an object is a fundamental entity in a Java program. Objects can be used effectively to represent realworld entities. For instance, an object might represent a particular employee in a company. Each employee object handles the processing and data management related to that employee. 5/3/2017 COSC-1336, Lecture 1 47 Difference between traditional and object-oriented approaches The conventional approach tends to consider data and behavior separate. The object-oriented approach considers data and behavior together in a concept called class. Example: 5/3/2017 Conventional approach: 1 large room with scanners and printers. Object-oriented approach: 2 smaller rooms, one with scanners and the other one with printers. COSC-1336, Lecture 1 48 Difference between traditional and object-oriented approaches (cont) Another example: suppose you have a fruit basket with apples, peaches, and pineapples, and two knives to peal off these fruits: 5/3/2017 Conventional approach: one large basket with all fruits and one drawer with two knives. Object-oriented approach: three smaller baskets, one with apples and their knife, another with peaches with no knife, and the last one with pineapples and the larger knife. COSC-1336, Lecture 1 49 Traditional approach → Java public class FruitBasket { static String nameOfFruit = ""; static int typeKnife = 0; public static void main(String args[]) { nameOfFruit = "apple"; typeKnife = 1; System.out.println("We eat " + nameOfFruit + " with " + typeKnife); nameOfFruit = "Pineapple"; typeKnife = 2; System.out.println("We eat " + nameOfFruit + " with " + typeKnife); // ... The operations ... } } 5/3/2017 COSC-1336, Lecture 1 50 Object-oriented approach → Java class Fruit { String nameOfFruit; int typeKnife; Fruit(String nF, int tK) { nameOfFruit = nF; typeKnife = tK; } // ... The operations ... } public class FruitBasket { public static void main(String args[]) { Fruit obj1 = new Fruit("apple", 1); System.out.println("We eat " + obj1); Fruit obj2 = new Fruit("pineapple", 2); System.out.println("We eat " + obj2); } } 5/3/2017 COSC-1336, Lecture 1 51 Difference between traditional and object-oriented approaches (cont) Another example: manual gear versus automatic gear vehicle: 5/3/2017 Conventional approach: manual gear, hence the responsibility of changing the gear belongs to the driver. Object-oriented approach: automatic gear, hence the operation of changing the gear while driving is embedded into the car specification. The driver does not need to change any gear! COSC-1336, Lecture 1 52 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Distributed computing involves several computers working together on a network. Java is designed to make distributed computing easy. Since networking capability is inherently integrated into Java, writing network programs is like sending and receiving data to and from a file. COSC-1336, Lecture 1 53 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 You need an interpreter to run Java programs. The programs are compiled into the Java Virtual Machine (JVM) code called bytecode. The bytecode is machine-independent and can run on any machine that has a Java interpreter, which is part of the Java Virtual Machine. COSC-1336, Lecture 1 54 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Java compilers can detect many problems that would first show up at execution time in other languages. Java has eliminated certain types of error-prone programming constructs found in other languages. Java has a runtime exception-handling feature to provide programming support for robustness. COSC-1336, Lecture 1 55 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Java implements several security mechanisms to protect your system against harm caused by stray programs. COSC-1336, Lecture 1 56 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Write once, run anywhere With a Java Virtual Machine (JVM), you can write one program that will run on any platform. COSC-1336, Lecture 1 57 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Because Java is architecture neutral, Java programs are portable. They can be run on any platform without being recompiled. COSC-1336, Lecture 1 58 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Java's Performance Java Is Multithreaded Java Is Dynamic 5/3/2017 Java’s performance is sometimes criticized. The execution of the byte-code is never as fast as it would be with a compiled language, such as C++. Because Java is interpreted, the byte-code is not directly executed by the system, but is run through the interpreter. The new JVM uses the technology known as just-in-time compilation. It compiles byte-code into native machine code, stores the native code, and re-invokes the native code when its bytecode is executed. Sun recently developed the Java HotSpot Performance Engine, which includes a compiler for optimizing the frequently used code. The HotSpot Performance Engine can be plugged into a JVM to dramatically boost its performance. COSC-1336, Lecture 1 59 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureNeutral Java Is Portable Multithread programming is smoothly integrated in Java, whereas in other Java's Performance languages you have to call procedures Java Is Multithreaded specific to the operating system to enable multithreading. Java Is Dynamic 5/3/2017 COSC-1336, Lecture 1 60 Characteristics of Java Java Is Simple Java Is Object-Oriented Java Is Distributed Java Is Interpreted Java Is Robust Java Is Secure Java Is ArchitectureJava was designed to adapt to an evolving Neutral environment. New code can be loaded on Java Is Portable the fly without recompilation. There is no need for developers to create, and for users Java's Performance to install, major new software versions. Java Is Multithreaded New features can be incorporated transparently as needed. Java Is Dynamic 5/3/2017 COSC-1336, Lecture 1 61 JDK Versions JDK 1.02 (1995) JDK 1.1 (1996) JDK 1.2 (1998) JDK 1.3 (2000) JDK 1.4 (2002) JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 JDK 1.7 (2010) a. k. a. JDK 7 or Java 7 Sun Microsystems merged Oracle (April 2010) – hence Java can be found at www.oracle.com/technetwork/java/index.html instead of java.sun.com 5/3/2017 COSC-1336, Lecture 1 62 JDK Editions Java Standard Edition (J2SE) Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java Micro Edition (J2ME). J2SE can be used to develop client-side standalone applications or applets. J2ME can be used to develop applications for mobile devices such as cell phones. Liang’s book uses J2SE to introduce Java programming. 5/3/2017 COSC-1336, Lecture 1 63 Popular Java Integrated Development Environments (IDEs) NetBeans: Open Source by Sun (http://netbeans.org/) Eclipse: Open Source by IBM (http://www.eclipse.org/platform/) 5/3/2017 COSC-1336, Lecture 1 64 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!"); } } 5/3/2017 COSC-1336, Lecture 1 65 Creating and Editing Using NotePad To use NotePad, type notepad Welcome.java from the DOS prompt. 5/3/2017 COSC-1336, Lecture 1 66 Creating and Editing Using WordPad To use WordPad, type write Welcome.java from the DOS prompt. 5/3/2017 COSC-1336, Lecture 1 67 Creating, Compiling, and Running Programs Create/Modify Source Code Source code (developed by the programmer) Saved on the disk public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } Byte code (generated by the compiler for JVM to read and interpret, not for you to understand) … Method Welcome() 0 aload_0 … Method void main(java.lang.String[]) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return Source Code Compile Source Code i.e., javac Welcome.java If compilation errors stored on the disk Bytecode Run Byteode i.e., java Welcome Result 5/3/2017 COSC-1336, Lecture 1 If runtime errors or incorrect result 68 Compiling Java Source Code You can port a source program to any machine with appropriate compilers. The source program must be recompiled (if you didn’t send the bytecode program, too), however, because the object program can only run on a specific machine. Nowadays computers are networked to work together. 5/3/2017 COSC-1336, Lecture 1 Java Bytecode Java Virtual Machine Any Computer 69 Compiling Java Source Code Java was designed to run object programs on any platform. With Java, you write the program once, and compile the source program into a special type of object code, known as bytecode. The bytecode can then run on any computer with a Java Virtual Machine, as shown below. Java Virtual Machine is a software that interprets Java bytecode. 5/3/2017 COSC-1336, Lecture 1 Java Bytecode Java Virtual Machine Any Computer 70 Trace a Program Execution Enter main() method //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 5/3/2017 COSC-1336, Lecture 1 71 Trace a Program Execution Execute statement //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } 5/3/2017 COSC-1336, Lecture 1 72 Trace a Program Execution //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } } print a message to the console 5/3/2017 COSC-1336, Lecture 1 73 Supplements on the Companion Website See Supplement I.B for installing and configuring JDK. See Supplement I.C for compiling and running Java from the command window for details. www.cs.armstrong.edu/liang/intro8e 5/3/2017 COSC-1336, Lecture 1 74 Review Question Answers 5/3/2017 COSC-1336, Lecture 1 75 Compiling and Running Java from the Command Window Set path to JDK bin directory Set classpath to include the current directory set classpath=. Compile set path=c:\Program Files\java\jdk1.6.0\bin javac Welcome.java Run java Welcome 5/3/2017 COSC-1336, Lecture 1 76 Compiling and Running Java from TextPad See Supplement II.A on the Website for details. 5/3/2017 COSC-1336, Lecture 1 77 Anatomy of a Java Program Comments Reserved words Modifiers Statements Blocks Classes Methods The main() method 5/3/2017 COSC-1336, Lecture 1 78 Comments Three types of comments in Java: 1. Line comment: A line comment is preceded by two slashes (//) in a line. 2. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines. 3. javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command. 5/3/2017 COSC-1336, Lecture 1 79 Reserved Words Reserved words (a.k.a., keywords) are words that have a specific meaning to the compiler and 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. Other reserved words in Listing 1.1 are public, static, and void. Their use will be introduced later in the lessons. 5/3/2017 COSC-1336, Lecture 1 80 Modifiers Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers: public, static, private, final, abstract, protected, and so on. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs. Modifiers are discussed in Chapter 6, “Objects and Classes.” 5/3/2017 COSC-1336, Lecture 1 81 Statements In Java, a direct instruction that tells the computer to do something is called a statement. A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!"); in the program in Listing 1.1 is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;). In addition, the above statement is a method call, too. 5/3/2017 COSC-1336, Lecture 1 82 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) { System.out.println("Welcome to Java!"); } } 5/3/2017 COSC-1336, Lecture 1 Class block Method block 83 Classes The class is the most essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. The mystery of the class will continue to be unveiled throughout the lessons. For now, though, understand that a program is defined by using one or more classes. 5/3/2017 COSC-1336, Lecture 1 84 Methods What is System.out.println()? It is a method call: a collection of statements that performs a sequence of operations to display a message on the console. It can be used even without fully understanding the details of how it works. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println() method with a different argument to print a different message. 5/3/2017 COSC-1336, Lecture 1 85 The main() method The main() method provides the control of program flow. The Java interpreter executes the application by invoking the main() method. The main() method looks like this: public static void main(String[] args) { // Statements; } 5/3/2017 COSC-1336, Lecture 1 86 Displaying Text in a Message Dialog Box You can use the showMessageDialog() method in the JOptionPane class. JOptionPane is one of the many predefined classes in the Java system, which can be reused rather than “reinventing the wheel.” 5/3/2017 COSC-1336, Lecture 1 87 WelcomeInMessageDialogBox.java /** This application program displays Welcome to Java! * in a message dialog box. */ import javax.swing.JOptionPane; public class WelcomeInMessageDialogBox { public static void main(String[] args) { // Display Welcome to Java! in a message dialog box JOptionPane.showMessageDialog(null, "Welcome to Java!"); } } http://blogs.oracle.com/thejavatutorials/entry/why_is_swing_called_swing 5/3/2017 COSC-1336, Lecture 1 88 5/3/2017 COSC-1336, Lecture 1 89 Compiling and Running the program 5/3/2017 COSC-1336, Lecture 1 90 The showMessageDialog() Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display Message", JOptionPane.INFORMATION_MESSAGE); 5/3/2017 COSC-1336, Lecture 1 91 Two Ways to Invoke the Method There are several ways (at least two) to use the showMessageDialog() method: 1. One is to use a statement as shown in the example: JOptionPane.showMessageDialog(null, x, y, JOptionPane.INFORMATION_MESSAGE); where x is a String for the text to be displayed, and y is a String for the title of the message dialog box. 2. The other is to use a statement like this: JOptionPane.showMessageDialog(null, x); where x is a String for the text to be displayed. 5/3/2017 COSC-1336, Lecture 1 92 A general Java class syntax // comments about the class public class MyProgram { class header class body } 5/3/2017 Comments can be placed almost anywhere COSC-1336, Lecture 1 93 The main() method syntax (test driver) // comments about the class public class MyProgram { // comments about the method public static void main (String[] args) { method body method header } } 5/3/2017 COSC-1336, Lecture 1 94 Summary To review computer basics, programs, and operating systems (§§1.2-1.4). To explore the relationship between Java and the World Wide Web (§1.5). To distinguish the terms API, IDE, and JDK (§1.6). To write a simple Java program (§1.7). To display output on the console (§1.7). To explain the basic syntax of a Java program (§1.7). To create, compile, and run Java programs (§1.8). (GUI) To display output using the JOptionPane output dialog boxes (§1.9). 5/3/2017 COSC-1336, Lecture 1 95 Reading suggestions From [Liang: Introduction to Java programming: Eight Edition, 2011 Pearson Education, 0132130807] Chapter 1 (Introduction) 5/3/2017 COSC-1336, Lecture 1 96 Coming up next From [Liang: Introduction to Java programming: Eight Edition, 2011 Pearson Education, 0132130807] 5/3/2017 Chapter 2 (Elementary Programming) COSC-1336, Lecture 1 97 Thank you for your attention! Questions? 5/3/2017 COSC-1336, Lecture 1 98