Bài giảng Chapter 2: Getting Started with Java

Tài liệu Bài giảng Chapter 2: Getting Started with Java: Chapter 2Getting Started with Java©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Chapter 2 ObjectivesAfter you have read and studied this chapter, you should be able to Identify the basic components of Java programs.Write simple Java programs.Describe the difference between object declaration and object creation.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Chapter 2 Objectives, cont.After you have read and studied this chapter, you should be able toDescribe the process of creating and running Java programs.Use the Date, SimpleDateFormat, String, and JOptionPane classes from the standard Java packages.Develop Java programs using the incremental development approach.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramThis first program displays a window on the screen.The size of the window is 300 x 200 pixels, and the default title is My First Java Program.The fundame...

ppt52 trang | Chia sẻ: honghanh66 | Ngày: 19/03/2018 | Lượt xem: 58 | Lượt tải: 0download
Bạn đang xem trước 20 trang mẫu tài liệu Bài giảng Chapter 2: Getting Started with Java, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
Chapter 2Getting Started with Java©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Chapter 2 ObjectivesAfter you have read and studied this chapter, you should be able to Identify the basic components of Java programs.Write simple Java programs.Describe the difference between object declaration and object creation.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Chapter 2 Objectives, cont.After you have read and studied this chapter, you should be able toDescribe the process of creating and running Java programs.Use the Date, SimpleDateFormat, String, and JOptionPane classes from the standard Java packages.Develop Java programs using the incremental development approach.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramThis first program displays a window on the screen.The size of the window is 300 x 200 pixels, and the default title is My First Java Program.The fundamental OOP concept illustrated by the program: An object-oriented program uses objects.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java Program/* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample1.java*/import javax.swing.*;class Ch2Sample1 { public static void main(String[ ] args){ JFrame myWindow; myWindow = new JFrame(); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.1Result of running the Ch2Sample1 program. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.2The program diagram for the Ch2Sample1 program.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.3The program diagram for the Ch2Sample1 program that shows the dependency relationship.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramTo use an object in a program, first we declare and create and object. Then we send messages to it.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramWhen we declare an object, we must give it a name. A Java identifier is a sequence of letters, digits, underscores, and dollar signs used to name a class, object, method, etc.The first character in a Java identifier must be a letter.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramUppercase and lowercase letters are distinguished.myWindow, mywindow, MYWINDOWNo spaces are allowed in an identifier.Java naming conventions dictate the use of an uppercase letter for the first letter of class names, and a lowercase letter for the first letter of object names.class Accountobject account©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramNo objects are created by the declaration. The declaration only assigns an identifier to the object.We create an object by invoking the new operation: = new ();myWindow = new JFrame ( );©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.4Distinction between object declaration and object creation.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.5Relationship between the state-of-memory diagram and the program diagram notation.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.6The state after two new commands are executed.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.1 The First Java ProgramThe object that receives a message must possess a corresponding method.The expressions “sending a message” and “calling a method” are synonymous.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.7Correspondence between message sending as represented in the program diagram and in the actual Java statement.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.8How the beginning and ending comment markers are matched.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsBelow are the program components for the program Ch2Sample1:CommentsImport StatementClass DeclarationMethod Declaration©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsPrograms contain comments in which we state the purpose of the program, explain the meaning of code, and provide other descriptions to help programmers use and understand the code.A comment is any sequence of text that begins with the marker /* and ends with the marker */.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsIn Java, classes are grouped into packages, and the Java system comes with numerous packages.To use a class from a package, we refer to the class in the program by using the following syntax:.This notation is called dot notation.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsUsing the fully qualified name of a class can be cumbersome. Using the import statement solves this problem.import javax.swing.*;©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsA Java program is composed of one or more classes. The syntax for declaring a class isclass { }A class member is either a data value or a method.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsOne of the classes in the program must be designated as the main class. If we designate a class as a main class, we must define a method called main, because when a Java program is executed, the main method of a main class is executed first.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program ComponentsA sample method declaration and its components.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.9A program template for simple Java applications.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.2 Program Components/*Chapter 2 Sample Program: Freehand DrawingFile: Ch2FunTime.javaThe program will allow you to draw a picture by dragging a mouse (move the mouse while holding the left mouse button down; hold the button on Mac). To erase the picture and start over, click the right mouse button (command-click on Mac).*/import javabook.*;class Ch2FunTime { public static void main (String[ ] args) { SketchPad doodleBoard; doodleBoard = new SketchPad( ); doodleBoard.setVisible( true ); }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.10The window that appears on the screen when the program starts running.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.11The same window after a picture is drawn.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.12The program diagram for the FunTime program.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.13A MiniBrowser object displaying a sample web page.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.3 Edit-Compile-Run CycleStep One: Edit the program.Type in the program, using a text editor, and save the program to a file. Use the name of the main class and the suffix .java for the filename. This file is called a source file.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.3 Edit-Compile-Run CycleStep 2: Compile the source file.The process of compiling the source file creates the bytecode file.The name of the compiler-generated bytecode file will have the suffix .class while its prefix is the same as the source file’s.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.3 Edit-Compile-Run CycleA sample source file and its bytecode file.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.3 Edit-Compile-Run CycleStep 3: Execute the bytecode file.A java interpreter will go through the bytecode file and execute the instructions in it. If your program is error-free, a window will appear on the screen.If an error occurs while running the program, the interpreter will catch it and stop its execution.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.3 Edit-Compile-Run CycleThe result after the interpreter executes the instructions in the bytecode file.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.4 Sample Java Standard ClassesUsing existing Java classes is an important step in becoming a successful and proficient programmer.We will introduce four standard classes here: JOptionPane StringDateSimpleDateFormat.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.4 Sample Java Standard ClassesUsing the JOptionPane class is a simple way to display a result of a computation to the user.JOptionPane.showMessageDialog(null, “I Love Java”);©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.14A simple “message” dialog created by the showMessageDialog method of the JOptionPane class.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.15A dialog with multiple lines of text.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.4 Sample Java Standard ClassesThe textual values passed to the showMessageDialog method are instances of the String class.A sequence of characters separated by double quotes is String constants.String text;text = “Espresso”; JOptionPane.showMessageDialog(text.substring(2, 7));©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.16A dialog showing the substring of “espresso” from index position 2 to 6. ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.17Individual characters in a string are numbered from 0.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.18The effect of the substring method is shown. The original string remains intact.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.4 Sample Java Standard ClassesDate and SimpleDateFormatThe Date class is used to represent a time instance to a millisecond. This class is in the java.util package.SimpleDateFormat can be used to provide an alternative format to the Date class. This class is in the java.text package.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.19JOptionPane can also be used for input: Below is the result of calling the showInputDialog method of the JOptionPane class with “What is your name?” as the method’s second argument.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.5 Sample Development: Printing the InitialsProblem statement:Write an application that asks for the user’s first, middle, and last names and replies with their initials.Overall plan:Get the user’s first, middle, and last names.Extract the initials to form the monogram.Output the monogram.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.20The program diagram for Ch2Monogram.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.5 Sample Development: Printing the Initials/*Chapter 2 Sample Program: Displays the MonogramFile: Step1/Ch2Monogram.java*/import javax.swing.*;class Ch2Monogram { public static void main (String [ ] args) { String name; name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):"); JOptionPane.showMessageDialog(null, name); }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.5 Sample Development: Printing the Initials/*Chapter 2 Sample Program: Displays the MonogramFile: Step 2/Ch2MonogramStep2.java*/import javax.swing.*;class Ch2Monogram { public static void main (String [ ] args) { String name, first, middle, last, space, monogram; space = " "; //Input the full name name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):");©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.2.5 Sample Development: Printing the Initials //Extract first, middle, and last names first = name.substring(0, name.indexOf(space)); name = name.substring(name.indexOf(space)+1, name.length()); middle = name.substring(0, name.indexOf(space)); last = name.substring(name.indexOf(space)+1, name.length()); //Compute the monogram monogram = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0,1); //Output the result JOptionPane.showMessageDialog(null, "Your monogram is " + monogram); }}©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.Fig. 2.21 Apply the two sequences of indexOf and substring methods to extract three substrings from a given string.©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.

Các file đính kèm theo tài liệu này:

  • pptchap_2_8847.ppt
Tài liệu liên quan