Getting Started with Java - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Getting Started with Java

Description:

The Date class in Java represents time to the nearest millisecond ... method of the Date class returns a String representing the complete time and date ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 31
Provided by: amyh99
Learn more at: http://cs.boisestate.edu
Category:
Tags: and | date | getting | java | started | time

less

Transcript and Presenter's Notes

Title: Getting Started with Java


1
Chapter 2
  • Getting Started with Java
  • Part B

2
Topics
  • Components of a Java Program
  • classes
  • methods
  • comments
  • import statements
  • Declaring and creating objects
  • Java identifiers
  • Standard Java classes
  • Development Example

3
Warm-up Exercises
  • First homework assignment (on the web)
  • Chapter 1 problems 2, 4, 5
  • Chapter 2 problems 1, 9 (parts a, c, g, j) , 12
  • Practice
  • Chapter 1 3 CD class
  • Chapter 1 14 Inheritance hierarchy for contact
    manager (Person, Professional contact, Friend,
    Student)

4
UML Class Diagrams
  • An example of a UML diagram for the CD class
  • Diagram has only instance data and methods
  • Class data and methods would be underlined

5
Inheritance Hierarchy
6
Template for Java Program
7
Edit-Compile-Run Cycle
  • Edit
  • vi MyClass.java
  • Compile
  • javac MyClass.java
  • Run
  • java MyClass

8
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

9
Standard Classes
  • We will introduce four standard classes here
  • String
  • JOptionPane
  • Date
  • SimpleDateFormat

10
String
  • A string is basically some text
  • a sequence of characters
  • We've seen 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)

11
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)

12
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"

13
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

14
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

15
Using the substring method
16
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 \

17
GUI Windows
  • Windows come in 2 forms in a GUI interface
  • application windows which are generally
    long-lived
  • JFrame objects in Java
  • 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

18
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

19
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)

20
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

21
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 hoursminutesseconds timezone
    year
  • Date is in java.util

22
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

23
SimpleDateFormat Codes
Y year number yyyy-gt2004
M month text or number MM-gt10 MMM-gt Oct
d day in month number dd-gt20
h hour in (am/pm) number hh-gt09
a am/pm text a-gtam
m minutes in hour number mm-gt35
s seconds in minute number ss-gt54
E day in week text E-gt Sat.
24
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

25
Sample Development Printing Initials
  • Problem statement
  • Write an application that asks for the users
    first, middle, and last names and replies with
    their initials.
  • Overall plan
  • Get the users first, middle, and last names.
  • Extract the initials to form the monogram.
  • Output the monogram.

26
What classes do we need?
27
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

28
UML Diagram for Ch2Monogram
29
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

30
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)
Write a Comment
User Comments (0)
About PowerShow.com