Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes...

30
Chapter 2 Getting Started with Java Part B
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    232
  • download

    3

Transcript of Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes...

Chapter 2

Getting Started with Java

Part B

Topics

• Components of a Java Program– classes– methods– comments– import statements

• Declaring and creating objects– Java identifiers

• Standard Java classes• Development Example

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)

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

Inheritance Hierarchy

Template for Java Program

Edit-Compile-Run Cycle

• Editvi MyClass.java

• Compilejavac MyClass.java

• Runjava MyClass

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

Standard Classes

• We will introduce four standard classes here: – String– JOptionPane – Date– SimpleDateFormat

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)

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 StringString 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);

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"

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

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

Using the substring method

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 \

GUI Windows

• Windows come in 2 forms in a GUI interface1. 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

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

Message Dialogs

• Used when you need to be sure the user has been informed about something– results– error messages and warningsJOptionPane.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)

Input Dialogs

• Another common form of dialog is one used to ask for a small amount of inputJOptionPane.showInputDialog( String prompt)

– displays a box for the user to enter something– returns the String that the user types in

Using Dates in Java

• The Date class in Java represents time to the nearest millisecond

• Create a Date with the current time usingDate today = new Date();

• The toString method of the Date class returns a String representing the complete time and dateWeekday Month day hours:minutes:seconds timezone year

• Date is in java.util

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

fieldsSimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

• SimpleDateFormat is in the java.text package

SimpleDateFormat Codes

Y year number yyyy->2004

M month text or number

MM->10

MMM-> Oct

d day in month number dd->20

h hour in (am/pm) number hh->09

a am/pm text a->am

m minutes in hour number mm->35

s seconds in minute number ss->54

E day in week text E-> Sat.

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

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.

What classes do we need?

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

UML Diagram for Ch2Monogram

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

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)