Assignments

advertisement

Assignments

Read for next week: 4.1-4.9

Fri 9/17 6:00 PM CodeWarrior Tutorial o Questions?

Fri 9/17 6:00 PM Assignment 0 o UML Diagrams

Differentiating class and instance stuff in the class: underline class stuff (opposite of object diagram name)

Representing methods in the class

Arguments

Return values o Questions?

Mon 9/20 11:58 PM CodeLab #2 o Questions?

Fri 9/24 11:58 PM CodeLab #3 o Should be able to do some; will cover next week:

Infer class declaration from sample programs and CodeWarrior

Declaring constants

Standard input o Distinction between “expression” and “statement” o Questions?

Review: 1.5

What are the five stages of the software life cycle? o Analysis, Design, Coding, Testing, Operation

When does debugging occur? o Testing and (unfortunately) Operation

1 CS 302 Lecture 6 0917

2.1 The First Java Program

Reference (object) and primitive (numerical) data types

Steps to use an object (how different from primitives?) o Declare o Create o Send messages

Declaration o Memory diagram: primitive versus reference

Creation o Declaration does not create object o May use same identifier to refer to different instances of the same class at different times o Memory diagram: primitive versus reference

Distinction clear?

Note that both always use same size for variable itself, but that the amount of memory used by the object can vary widely (even among objects of the same class; e.g. lists: different people’s favorite numbers) o Assigning one object variable to another

Does not create new object

Means having two names for the same thing

Sharing

2 CS 302 Lecture 6 0917

Message sending o Can only do after the object has been created o Calling a method

Message usually refers externally

Method usually refers internally

What types are arguments to setSize and setTitle? o New type for setVisible: boolean o setSize refers to pixels

3 CS 302 Lecture 6 0917

2.2 Program Components

Three parts to all Java programs

Comments o Computer ignores o Not necessary for execution o Documentation

Help programmers understand program

 State purpose of program

 ID/explain/summarize code blocks

Note modifications o Disable sections of code during testing

Comment marker: example code

Single-line comment o Extends from marker to end of line o Example

Javadoc comments: later

Header comment: examples o CodeWarrior Tutorial o Example program

Import: use pre-defined classes whenever possible

Packages: Java comes with many

Package hierarchy o Subpackages o Multiple dots

Import statement o Avoids use of fully qualified name o Allows use of class name by itself o Does not copy classes into your program

Star o Import all classes in a package o Conventional, but can input by name specifically

4 CS 302 Lecture 6 0917

5

Class declaration o “Class” reserved word o Example o Class members

Data values

Methods

Examples o Main class

Every program must include at least one

In CodeWarrior, designate by giving it the same name as the file

Example o Refer to whole application as “<main class> application”; Example

Method declaration o Example o Modifiers

 State which kind of method

Many o Return type states the type of the data value that the method returns

CS 302 Lecture 6 0917

Completely different programs can be created readily by using different objects

6 CS 302 Lecture 6 0917

2.3 Edit-Compile-Run Cycle

Steps to execute program

Require more specifics to actually do in particular development tool

Use <main class name>.java for file name

Compilation o May need to place source in project and compile that o Bytecode file: file containing bytecode o Compilation errors easiest type to correct

Execution o Bytecode is system independent o Occurs on interpreter

System independent

Java Virtual Machine (JVM)

Must have specialized for each system o Errors detected by interpreter

7 CS 302 Lecture 6 0917

2.4 Simple Java Standard Classes

Right now, learn to use some existing classes

Later, learn to define own

Java API o Application programming interface o Documentation for the standard classes

JoptionPane for output o Display computed result to user o Window types (in GUI environment)

General purpose frame, e.g. JFrame

Special purpose dialog

JDialog

Method showMessageDialog o First argument

Controlling frame object

Dialog positioned at center of this, or, if no arg, of screen o Creates instance of JDialog internally o Class method

 Don’t need to create object to use it

Just use <class name>.<class method> o Example: display “What’s\nup?” in JFrame jf2

8 CS 302 Lecture 6 0917

9

String o Explicit use of new optional; draw mem diagram o Substring

First argument

Beginning position

Count from 0

Is displayed

Second argument

End position

Is not displayed

Creates new string

Original string left intact

Generates error if given illogical arguments

Examples

 “together” o 0-2= “to” o 2-5= “get” o 5-8= “her”

 “michelle”: 3-7= “hell”

You think of one o Length

Return # of characters in a String

Examples o IndexOf

 Locate index position of substring within another string

Returns position of first char of substring

 Returns -1 if substring not found

Case-sensitive

Returns first occurrence if multiple exists

CS 302 Lecture 6 0917

Date o Time instance o Millisecond precision o Automatically set to time created (from OS) o toString method converts to human-readable: Sat

Apr 20 15:05:18 PDT 2002 o internally represented as time since epoch (Jan 1,

1970 00:00:00 GMT) o Use GregorianCalendar for real dates

SimpleDateFormat o Change display format of Date o Pass formatiting string when create instance of

SimpleDateFormat o Formatting string case-sensitive

SimpleDateFormat simpleDF; simpleDF = new SimpleDateFormat(“…”);

System.out.println(simpleDF.format(new

Date()));

10 CS 302 Lecture 6 0917

JoptionPane for input o showInputDialog method o Access input by assigning return result to String variable o Returns null if cancel clicked o Returns empty string if nothing entered & OK clicked

2.5 Sample Development

Get into the habit of developing according to the software life cycle stages (even though not strictly necessary for small programs)

Map out overall plan o ID classes necessary o ID implementation steps to follow o Outline program logic o Write design document

Consider design alternative o Better depends on metric o Always developing for users

Monday 9-14

CodeLab #2 Due

More Chapter 2

If finish, review Chapter 3 so far

11 CS 302 Lecture 6 0917

Download