comments are for people header comments supply basic information about the artifact

29
Start of every class Start up DrJava Open class web site with browser Read entire class entry Download indicted files Try to figure out why the picture and title for the day are appropriate

Upload: blaze-garrett

Post on 29-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Start of every class Start up DrJava

Open class web site with browser

Read entire class entry

Download indicted files

Try to figure out why the picture and title for the day are appropriate

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Comments are for people

Header comments supply basic information about the artifact

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Comments also used to describe class elements

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Whitespace is also for people

Blank lines make elements stand out

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Indentation is a form of whitespace

Indentation is used to indicate that what follows is a part of what proceeded

Indentation is a form of whitespace

Indentation is used to indicate that what follows is a part of what proceeded

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Keywords are reserved lowercase words that have special meaning

public – what follows is shareable

class – program or object definition

static – not part of an object definition

void – method does not return a value

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Name of the class

A Java name must be an identifier

Java has syntax rules for stating what is a valid name; e.g., identifiers begin with letter and can be followed letters and numerals

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

To encourage program understanding style rules give name understanding

Convention to capitalize the first letter in a class name

General convention to capitalize successive words in a name

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

PrintQuote, main, String, args, System, out, and println are all names

Convention that identifiers not indicating a class begin with a lower case letter

Braces delimit a class definition

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Braces delimit a block of code

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

A program begins executing the code in method main()

Method main() must be public, static, and void

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Every method has a parameter list following its name.

The list is contained with a pair of parentheses.

For some methods, the list is empty

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Method main() takes a single parameter of type String[]

Everybody who is anybody calls that parameter, args

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Following the parameter list is the body of the method

The method body lists the statements (i.e., actions) it is to perform when invoked (started up)

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

The statements are performed from top to bottom

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

The statements are performed from top to bottom

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Because statements are allowed in general to span multiple lines an indicator is needed for separating statements from one another

Java statements are terminated with a semicolon

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

The dot is the selection operator

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

Sometimes multiple selecting is needed to get a hold of the desired component

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

System is the name of a class library whose elements are automatically available to a program

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

The System library has an element named out

out represents an object configured to be associated with your display

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

out has a println() behavior

println() is a method

println() displays text to its display

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

println() displays the value of its argument

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

This use of println() says to print the literal string ”I’m never going to the Louvre again.” to its display

A literal string is a sequence of characters within a pair of the double-quote characters

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

This use of println() says to print the literal string ”-- Joanne Cohoon” to its display

// author: J. P. Cohoon

// email id: jpc

// purpose: print text

public class PrintQuote {

// method main(): program starting point

public static void main( String[] args ) {

System.out.println( "I’m never going to the Louvre again." );

System.out.println( "-- Joanne Cohoon." );

}

}

A program terminates when all of the statements in method main() are completed