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 Structure of.

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 Structure of."— Presentation transcript:

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

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 2 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 2 - 3 The Simplest Program /** The simplest class */ public class Hello { public static void main(String[ ] args) { System.out.println( "Hello, World"); } The class has a single method, the main method The main method has a single statement

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 4 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.

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

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 6 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 2 - 7 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

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 8 Import Statement Syntax and Semantics We often use classes that already exist in our programs 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.

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 9 Program that creates and uses an object import javax.swing.JFrame; /** Chapter 2 Sample Program: Displaying a Window */ 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); }

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 10 Objects Used 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

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 11 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

12 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 12 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 for the object –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.

13 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 13 Names in Java We need names for classes, objects and methods in a Java program Rules for names –Start with a letter or underscore (_) –remaining characters can be 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

14 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 14 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 give it a name –use the assignment operator (=) to assign 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.

15 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 15 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

16 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 16 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.

17 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 17 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. We send a message to an object by calling one of its methods When we send a message, we need to specify the object it should be sent to

18 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 18 Method Definition 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

19 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 19 Standard Classes Java comes with a large collection of existing classes –Don’t reinvent the wheel. When there are existing classes 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.

20 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 20 String The String class is used to represent text There are close to 50 methods defined in the String class. For example. –length - tells how many characters a String has –indexOf - gives the position of a particular sequence of characters in a String –substring - get another String containing some part of the original string Java provides a special operation called concatenation to combine Strings –We can concatenate two strings using +

21 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 21 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.

22 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 22 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. 1 2 1 String name; name = “Jon Java”; : String Jon Java name 2

23 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 23 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. 1 2 1 String name; name = new String(“Jon Java”); : String Jon Java name 2 : String Jon Java

24 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 24 String method: length Assume str is a properly initialized String object. str.length( ) will return the number of characters in str. If str is “programming”, then str.length( ) will return 11. –all characters, including spaces, are counted in the length

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

26 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 26 String Method: indexOf Assume str and substr are properly initialized String objects. 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.

27 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 27 String Method: substring Assume str is a properly initialized String object. str.substring( i, j ) will return a new string by extracting characters of str from position i through position j-1 where 0  i  str.length, 0  j  str.length, 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.

28 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 28 String Method: concatenation Assume str1 and str2 are properly initialized String objects. 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 + is an operator and not a method of the String class. The strings str1 and str2 remain the same.

29 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 29 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 the current date (based on the computers settings) 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”

30 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 30 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”

31 Tools for UML Diagrams dia - a linux tool that allows you to draw many different kinds of diagrams –from the prompt type dia violet - a java program to create simple UML diagrams –copy violet.jar from ~tole/teaching/cs125/demo to a directory of your own –run the program using java -jar violet.jar


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 Structure of."

Similar presentations


Ads by Google