Download presentation
Presentation is loading. Please wait.
Published bySharon Borton Modified over 9 years ago
1
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Lecture #1 By Dr. Basheer M. Nasef
2
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Agenda Introduction to Java Programming Examples 2
3
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Learning Strategies A programming course is quite different from other courses. In a programming course, you learn from examples, from practice, and from mistakes. You need to devote a lot of time to writing programs, testing them, and fixing errors. 3
4
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef JDK & JRE ? JRE (Java Runtime) is needed for running Java programs. JDK (Java Development Kit), which includes JRE plus the development tools (such as compiler and debugger), is need for writing as well as running Java programs. To write Java Programs, you should install JDK, which includes JRE. 4
5
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef JDK Versions JDK Alpha and Beta (1995): Sun announced Java in September 23, 1995. JDK 1.0 (January 23, 1996): Originally called Oak (named after the oak tree outside James Gosling's office). Renamed to Java 1 in JDK 1.0.2. JDK 1.1 (February 19, 1997): Introduced AWT event model, inner class, JavaBean, JDBC, and RMI. J2SE 1.2 (codename Playground) (December 8, 1998): Re- branded as "Java 2" and renamed JDK to J2SE (Java 2 Standard Edition). Also released J2EE (Java 2 Enterprise Edition) and J2ME (Java 2 Micro Edition). Included JFC (Java Foundation Classes - Swing, Accessibility API, Java 2D, Pluggable Look and Feel and Drag and Drop). Introduced Collection Framework and JIT compiler. J2SE 1.3 (codename Kestrel) (May 8, 2000): Introduced Hotspot JVM. 5
6
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef JDK Versions J2SE 1.4 (codename Merlin) (February 6, 2002): Introduced assert, non-blocking IO (nio), logging API, image IO, Java webstart, regular expression support. J2SE 5.0 (codename Tiger) (September 30, 2004): Officially called 5.0 instead of 1.5. Introduced generics, autoboxing/unboxing, annotation, enum, varargs, for-each loop, static import. Java SE 6 (codename Mustang) (December 11, 2006): Renamed J2SE to Java SE (Java Standard Edition). Java SE 7 (codename Dolphin) (July 28, 2011): First version after Oracle purchased Sun (called Oracle JDK). Java SE 8 (March 18, 2014): included support for Lambda expressions, default methods, and JavaScript runtime. 6
7
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef JDK Editions Java Standard Edition (J2SE) J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such as cell phones. This Course (CSC210) uses J2SE to introduce Java programming. 7
8
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Popular Java IDEs (Integrated development environment ) NetBeans Open Source by Sun Eclipse Open Source by IBM Borland JBuilder 2007 (Based on Eclipse) JCreator. 8
9
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Java Program 9 Java programs consist of pieces called classes. Classes include pieces called methods that perform tasks and return information when they complete them. Programmers can create each piece they need to form Java programs. However, most Java programmers take advantage of the rich collections of existing classes in the Java class libraries, which are also known as the Java APIs (Application Programming Interfaces).
10
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Java Program 10 Thus, there are really two aspects to learning the Java "world." The first is the Java language itself, so that you can program your own classes. The second is the classes in the extensive Java class libraries. Throughout this course, we discuss many library classes.
11
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Creating, Compiling, and Executing a Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } 11
12
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Creating and Editing Using NotePad To use NotePad, type notepad Welcome.java from the DOS prompt. 12
13
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Creating, Compiling, and Running Programs 13
14
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Trace a Program Execution 14 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Enter main method
15
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Trace a Program Execution 15 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } Execute statement
16
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Trace a Program Execution 16 //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
17
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef 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/intro7e 17
18
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Compiling and Running Java from the Command Window Set path to JDK bin directory set path=c:\Program Files\java\jdk1.8.0_25\bin Set classpath to include the current directory set classpath=. Compile javac Welcome.java Run java Welcome 18
19
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Compiling and Running Java from JCreator 19
20
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Anatomy of a Java Program Comments Package Reserved words Modifiers Statements Blocks Classes Methods The main method 20
21
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Comments I. Line comment: A line comment is preceded by two slashes (//) in a line. II. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines. 21 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. Three types of comments in Java.
22
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Package The second line in the program (package chapter1;) specifies a package name, chapter1, for the class Welcome. Forte compiles the source code in Welcome.java, generates Welcome.class, and stores Welcome.class in the chapter1 folder. 22
23
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Reserved Words Reserved words or 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 above program are public, static, and void. Their use will be introduced later. 23
24
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef 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 are public and static. Other modifiers are private, final, abstract, and protected. 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.” 24
25
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Statements A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;). 25
26
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Blocks 26 A pair of braces in a program forms a block that groups components of a program.
27
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Classes The class is the 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. For now, though, understand that a program is defined by using one or more classes. 27
28
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Methods What is System.out.println? It is a method: a collection of statements that performs a sequence of operations to display a message on the console. 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. 28
29
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef 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; } 29
30
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef 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.” WelcomeInMessageDialogBox 30 Run IMPORTANT NOTE: To enable the buttons, you must store the entire slide file Link_Files files into a directory (e.g., e:\Link_Files_CSC210).
31
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Display Message", JOptionPane.INFORMATION_MESSAGE); 31
32
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef Two Ways to Invoke the Method There are several ways to use the showMessageDialog method. For the time being, all you need to know are two ways to invoke it. 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. The other is to use a statement like this: JOptionPane.showMessageDialog(null, x); where x is a string for the text to be displayed. 32
33
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef PROGRAMMING EXERCISES (Displaying a pattern) Write a program that displays the following pattern: 33
34
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef PROGRAMMING EXERCISES (Displaying a pattern) Write a program that displays the following pattern: 34
35
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef PROGRAMMING EXERCISES (Displaying a pattern) Write a program that displays the following pattern: 35
36
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef PROGRAMMING EXERCISES 36
37
L1-2:CSC210 2014-2015 ©Dr. Basheer M. Nasef PROGRAMMING EXERCISES 37 Write a program that asks the user to enter two numbers, then prints the sum, product, and difference of the two numbers.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.