Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components.

Similar presentations


Presentation on theme: "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components."— Presentation transcript:

1 COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components of a Java Program classes methods comments import statements *Declaring and creating objects Java identifiers *Standard Java classes

2 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The First Java Program *This first program displays a window on the screen. The size of the window is 300 x 200 pixels The title is My First Java Program. *The fundamental OOP concept illustrated by the program: An object-oriented program uses objects.

3 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. 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){ JFramemyWindow; myWindow = new JFrame(); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); }

4 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. UML diagram for Ch2Sample1 *This diagram shows the messages sent to myWindow

5 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Class diagram for Ch2Sample1 *This diagram shows the dependency relationship between the classes in the program. The Ch2Sample1 class uses a JFrame object. *To use an object in a program, first we declare and create and object. Then we send messages to it.

6 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Declaring Objects *When we declare an object, we must give it a name and say what class it belongs to. syntax: the name must be a valid Java identifier the class must be defined and available if multiple objects are declared, the names are separated by commas

7 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Java Identifiers *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. Uppercase and lowercase letters are distinguished. myWindow, mywindow, MYWINDOW No spaces are allowed in an identifier.

8 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Java Naming Conventions *Use of an uppercase letter for the first letter of class names class Account *Use a lowercase letter for the first letter of object names. object account *Use all upper case letters for constants final int WINDOW_HEIGHT

9 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Instantiating Objects *No object is created by the declaration. *We create an object by invoking the new operation: Example

10 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Object Declaration and Object Creation *declaration assigns a name to a memory address that will store the location of an object *instantiation sets aside memory for the objects data stores the address of the objects data in the named memory location

11 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Diagram to show memory state *program diagram (or object diagram) *state-of-memory diagram uses similar notation to UML but includes extra symbols

12 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Another instantiation example *The state after two new commands are executed. allocating a new object means the address of the original is forgotten an object with no references to it is garbage

13 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Methods and Messages *Syntax for sending messages The expressions “sending a message” and “calling a method” are synonymous. The object that receives a message must possess a corresponding method.

14 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Program Components *Comments *Import Statement *Class Declaration *Method Declaration

15 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Comments *Comments are used to state the purpose of the program, explain the meaning of code, and provide other descriptions to help programmers use and understand the code. A single-line comment is preceeded by // A comment is any sequence of text that begins with the marker /* and ends with the marker */. A javadoc comment begins with /** and ends with */.

16 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using Classes from Java Library *In 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.

17 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. import statements *Using the fully qualified name of a class can be cumbersome. *Using the import statement solves this problem. import javax.swing.*; *Now we can just use the name of the class

18 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Class definition *A Java program is composed of one or more classes. *The syntax for declaring a class is A class member is either a data value or a method. Most classes will contain some of each

19 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. The main class *One 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. *The main class is sometimes called a driver class

20 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Method Definition *Syntax *Example:

21 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Template for Java Program

22 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Edit-Compile-Run Cycle (Linux version) *Edit vi MyClass.java *Compile javac MyClass.java *Run java MyClass

23 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Editing *Step 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.

24 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Compiling *Step 2: Compile the source file. The process of compiling the source file creates the bytecode file. the bytecode file is binary The name of the compiler-generated bytecode file will have the suffix.class while its prefix is the same as the source file’s. The class file will not be created if there are syntax errors in your program You'll get a list of error messages

25 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Running the Program *Step 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.

26 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Java Standard Classes *Java comes with an extensive set of classes that you can use. *Using existing Java classes is an important step in becoming a successful and proficient programmer. *The standard classes are organized into packages *Use import statements to tell the compiler where to find the classes you want to use

27 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Standard Classes *We will introduce four standard classes here: String JOptionPane Date SimpleDateFormat

28 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. String *A string is basically some text a sequence of characters *We've seen some String objects in our programs already "this is a String" These are constant or literal String objects They have no name They can't be changed *String class is in java.lang (no import statement required)

29 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Creating Strings *Putting a literal string in your program creates a String automatically *You can declare a String object and assign it to the literal String String fixedString = "some text"; - the String class is the only one you can do this with *You can use new to create a new String from an existing one String newString = new String( "more text"); String newString2 = new String( newString);

30 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. String Methods *The String class has many methods *We'll look at a few length() returns the number of characters in the String String text = "Espresso"; text.length() returns 8 String concat( String toAdd) creates a new String out of the original String followed by the argument String Can use + as a shortcut for this method (another unique property of the String class) "acro" + "bat" is "acrobat"

31 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. indexOf method *The indexOf method can be used to locate one String in another indexOf( String toFind) returns the position of toFind in the String Positions are numbered starting from 0 the index of "press" in "Espresso" is 2 if the toFind String isn't present, the result is -1

32 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. substring method *substring returns a new String which is part of a larger String substring( start, justAfter) start is the desired starting index justAfter is the index of the first character past the desired substring

33 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using the substring method

34 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Strings (review) *Literal strings are enclosed in double quotes “literal string” *Concatenate strings with + operator "acro" + "bat" is "acrobat” *Methods length() gets number of characters indexOf gets position of a substring substring picks out a smaller string

35 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Special Characters *What if you need a string that has several lines of text in it? *You can't spread a literal string over multiple lines *You need to put a character into the string that represents a carriage return "\n" is a special character that represents a newline *the \ is an escape character that tells the compiler to interpret the next character differently from its usual meaning *We'll see other special characters that start with a \

36 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. GUI Windows *Windows come in 2 forms in a GUI interface 1.application windows which are generally long-lived JFrame objects in Java 2.dialog boxes are short-lived JDialog objects in Java allow the user to interact with the program come in many forms - the JDialog class has to be quite complex to support this variety some common forms messages confirmation (yes/no/cancel) input

37 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. JOptionPane *Allows you to easily create common types of JDialog objects *All methods are class methods you never create a JOptionPane object call the methods using JOptionPane.methodName() JOptionPane is in the javax.swing package

38 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Message Dialogs *Used when you need to be sure the user has been informed about something results error messages and warnings JOptionPane.showMessageDialog( parent, message) parent is the name of the JFrame the JDialog is attached to (or null if it is not attached to a JFrame) message is the text to be displayed (a String)

39 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Input Dialogs *Another common form of dialog is one used to ask for a small amount of input JOptionPane.showInputDialog( String prompt) displays a box for the user to enter something returns the String that the user types in

40 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Using Dates in Java *The Date class in Java represents time to the nearest millisecond *Create a Date with the current time using Date today = new Date(); *The toString method of the Date class returns a String representing the complete time and date Weekday Month day hours:minutes:seconds timezone year *Date is in java.util

41 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Other Date Formats *Use the SimpleDateFormat class *Create a SimpleDateFormat object with a "picture" of what you want it to look like picture is a string with codes for the various fields SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); *SimpleDateFormat is in the java.text package

42 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. SimpleDateFormat Codes Yyearnumberyyyy->2004 Mmonthtext or number MM->10 MMM-> Oct dday in monthnumberdd->20 hhour in (am/pm)numberhh->09 aam/pmtexta->am mminutes in hournumbermm->35 sseconds in minutes numberss->54 Eday in weektextE-> Sat.

43 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Software Lifecycle *The sequence of stages from conception to operation of a program is called software life cycle. *Five stages are Analysis Design Coding Testing Operation and Maintenance

44 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Sample Development: Printing Initials *Problem 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.

45 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. What classes do we need?

46 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Necessary classes *Name, initials and monogram need String *Need JOptionPane for input and output To input the name, we need an input dialog To output the monogram, we need a message dialog

47 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. UML Diagram for Ch2Monogram

48 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Phase 1 *Input the user's name first middle last *Choices one dialog for each part easy to program tedious for user single dialog for the entire name harder to program need to specify format in this case nicer for user

49 COMPSCI 125 Spring 2005 ©The McGraw-Hill Companies,Inc. Permission required for reproduction or display. Phase 2 *Compute and display monogram need to display result to know whether computation is correct *How to separate name into 3 parts? use indexOf to find space use substring to separate at space repeat for second space *How to get initial from each part? substring(0,1)


Download ppt "COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components."

Similar presentations


Ads by Google