Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 1 Chapter 2 Getting Started with Java Program development.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 1 Chapter 2 Getting Started with Java Program development."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development process Structure of a Java program Declaring and creating objects Standard Java classes

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Development Process Design the program Use a text editor (vim) to write a java file vim MyClass.java Compile the java code to create a class file javac MyClass.java Run the program java MyClass Create the documentation javadoc MyClass.java

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Template for Simple Java Programs import java.package.ClassName; /** Describe the class here */ public class MyClass { public static void main(String[ ] args) { /* main method is used to start the program declare and create objects then call methods */ } Import Statements Class Name javadoc Comment javadoc Comment main Method

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The Simplest Program /** The simplest class */ public class Hello { public static void main(String[ ] args) { System.out.println( "Hello, World"); } No import statements are needed The main method has a single statement

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter An object-oriented program uses objects. The Hello class uses two kinds of objects –System.out System is a class that provides access to standard input and output facilities out is an object in the System class (class data). It belongs to the PrintStream class –"Hello, World!" Double quotes are used to automatically create a String object. Both these classes belong to the java. lang package which is automatically imported into every class

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Components A Java program is composed of one or more classes –Each class corresponds to a file Each class contains –import statements –comments - ignored by the compiler –class declaration

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Import Statement Syntax and Semantics We use an import statement to tell the compiler how to find a class we want to use in our program. ; e.g. dorm. Resident; Class Name The name of the class we want to import. Use asterisks to import all classes. Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name Name of the package that contains the classes we want to use. Package Name Name of the package that contains the classes we want to use.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Import Statement import javax.swing.*; /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ public class Ch2Sample1 { public static void main(String[ ] args) { JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Import Statement

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter import javax.swing.*; /** Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ public class Ch2Sample1 { public static void main(String[ ] args) { JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Program Component: Comments Comment

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Three Types of Comments /* This is a comment with three lines of text. */ Multiline Comment Single line Comments // This is a comment // This is another comment // This is a third comment /** * This class provides basic clock functions. In addition * to reading the current time and today’s date, you can * use this class for stopwatch functions. */ javadoc Comments

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The First Java Program The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. This program displays a window on the screen. –the window is an object The size of the window is set to 300 pixels wide and 200 pixels high. Its title is set to My First Java Program. –the title is a String, another object

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Class Declaration import javax.swing.*; /** Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ public class Ch2Sample1 { public static void main(String[ ] args) { JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Class Declaration

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Method Declaration import javax.swing.*; /** Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */ public class Ch2Sample1 { public static void main(String[ ] args) { JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Method Declaration

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Method Declaration Elements public static void main( String[ ] args ){ JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Method Body Modifier Return Type Method Name Parameter

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Ch2Sample1 import javax.swing.*; public class Ch2Sample1 { public static void main(String[ ] args) { JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Declare a name Create an object Use an object

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Diagram for Ch2Sample1 myWindow : JFrame Ch2Sample1 setSize(300, 200) setTitle(“My First Java Program”) setVisible(true) Ch2Sample1 is not an object In the main method –create object myWindow –send messages to myWindow

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object Declaration Every object we use in a program needs a name Use a declaration statement to tell the compiler –what names we are going to use –what type of object the name will refer to JFrame myWindow; Object Name One object is declared here. Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated. Class Name This class must be defined before this declaration can be stated.

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Names in Java We need names for classes, objects and methods in a Java program Rules for names –Start with a letter or underscore (_) –followed by letters, digits, underscore –case sensitive Naming conventions –class names start with a capital letter –method and object names start with a lower case letter –constants are all capitals

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object Creation Instantiation is the process of creating an object –use the new operator in front of a constructor to create an object If we want to use the object more than once, we have to assign it a name –use the assignment operator (=) to assign it a name myWindow = new JFrame ( ) ; Object Name Name of the object we are creating here. Object Name Name of the object we are creating here. Class Name An instance of this class is created. Class Name An instance of this class is created. Argument No arguments are used here. Argument No arguments are used here.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter UML for Declaration and Creation Customer customer; customer = new Customer( ); 1. The identifier customer is declared and space is allocated in memory. 2. A Customer object is created and the identifier customer is set to refer to it. 1 2 customer 2 : Customer customer 1

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Name vs. Objects Customer customer; customer = new Customer( ); customer : Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost.

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sending a Message myWindow. setVisible ( true ) ; Object Name Name of the object to which we are sending a message. Object Name Name of the object to which we are sending a message. Method Name The name of the message we are sending. Method Name The name of the message we are sending. Argument The argument we are passing with the message. Argument The argument we are passing with the message.

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Standard Classes Java comes with a large collection of existing classes –Don’t reinvent the wheel. When there are existing objects that satisfy our needs, use them. –Before we can learn how to define our own classes, we need to learn how to use existing classes We will introduce three standard classes here: –String –Date –SimpleDateFormat.

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String The String class is used to represent text There are close to 50 methods defined in the String class. For example. –length - find out how many characters a String has –indexOf - find the position of a particular sequence of characters in a String –substring - get a String containing a specified part of the original strings Java provides a special operation called concatenation for Strings

25 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Literal Strings A sequence of characters separated by double quotes is a String constant, also called a String literal. Putting double-quoted text into your program tells the compiler to create a String object for you. –The literal string does not have a name unless you give it one. String myString = "this is my string"; –If you want to use the same constant string in several places in your program, you can either give it a name or just use the double- quoted form wherever you need it.

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter name Naming a Literal String 1. The identifier name is declared and space is allocated in memory. 2. The identifier name is set to refer to the literal String String name; name = “Jon Java”; : String Jon Java name 2

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter name A String is an Object 1. The identifier name is declared and space is allocated in memory. 2. A String object is created and the identifier name is set to refer to it String name; name = new String(“Jon Java”); : String Jon Java name 2

28 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String method: length Assume str is a String object and properly initialized to a string. str.length( ) will return the number of characters in str. If str is “programming”, then str.length( ) will return 11 because there are 11 characters in it.

29 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Method: indexOf Assume str and substr are String objects and properly initialized. str.indexOf( substr ) will return the first position substr occurs in str. If str is “programming” and substr is “gram”, then str.indexOf(substr ) will return 3 because the position of the first character of substr in str is 3. If substr does not occur in str, then –1 is returned. The search is case-sensitive.

30 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Indexing The position, or index, of the first character is 0.

31 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Method: substring Assume str is a String object and properly initialized to a string. str.substring( i, j ) will return a new string by extracting characters of str from position i to j-1 where 0  i  length of str, 0  j  length of str, and i  j. If str is “programming”, then str.substring(3, 7) will create a new String whose value is “gram” because g is at position 3 and m is at position 6. The original string str remains unchanged.

32 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Method: concatenation Assume str1 and str2 are String objects and properly initialized. str1 + str2 will return a new string that is a concatenation of two strings. If str1 is “pro” and str2 is “gram”, then str1 + str2 will return “program”. Notice that this is an operator and not a method of the String class. The strings str1 and str2 remains the same.

33 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Date The Date class from the java.util package is used to represent a date. When a Date object is created, it is set to today (the current date set in the computer) The class has toString method that converts the internal format to a string. Date today; today = new Date( ); today.toString( ); “Fri Oct 31 10:05:18 PST 2003”

34 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter SimpleDateFormat The SimpleDateFormat class allows the Date information to be displayed with various format. Table 2.1 page 64 shows the formatting options. Date today = new Date( ); SimpleDateFormat sdf1, sdf2; sdf1 = new SimpleDateFormat( “MM/dd/yy” ); sdf2 = new SimpleDateFormat( “MMMM dd, yyyy” ); sdf1.format(today); sdf2.format(today); “10/31/03” “October 31, 2003”


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 1 Chapter 2 Getting Started with Java Program development."

Similar presentations


Ads by Google