2007 lawrenceville press slide 1 chapter 3 classes and objects 1. how is a class different from an...

10
© 2007 Lawrenceville Press Slide 1 Chapter 3 Chapter 3 Classes and Objects Classes and Objects 1. How is a class different from an object?

Upload: shannon-chase

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

© 2007 Lawrenceville Press Slide 3 2. Write the class declaration for a class named Farewell with a method named sayGoodbye with a statement that prints the word “bye” with a comment that contains your name

TRANSCRIPT

Page 1: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 1

Chapter 3Chapter 3Classes and ObjectsClasses and Objects

1. How is a class different from an object?

Page 2: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 2

Chapter 3Chapter 3A Java ApplicationA Java Application

/**

* The Greeting class displays a greeting

*/

public class Greeting {

public static void main(String[] args) {

System.out.println("Hello,world!");

}

}

commentcomment

class declarationclass declaration

methodmethodstatementstatement

Page 3: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 3

2. Write the class declaration 2. Write the class declaration for a class named Farewell for a class named Farewell with a method named with a method named sayGoodbye with a sayGoodbye with a statement that prints the statement that prints the word “bye” with a comment word “bye” with a comment that contains your namethat contains your name

Page 4: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 4

Chapter 3Chapter 3Executing a Java ApplicationExecuting a Java Application

public class Greeting public static void System.out.printl }}

source codesource code03 3b 84 01 ff f9 68 05 1a

bytecodbytecodee

...c cilbup Hello,world!...48 b3 30

compilercompiler JVMJVM

Page 5: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 5

Chapter 3Chapter 3Escape SequencesEscape Sequences

An escape sequence is a backslash (\) followed by a symbol that together represent a character.

Commonly used escape sequences:\n newline\t tab (8 spaces)\\ backslash\" double quotation mark

Page 6: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 6

Chapter 3Chapter 3The format() MethodThe format() Method

A method in the System class Used to control the way output is displayed Requires a format string and an argument list The format string specifier takes the form:

%[alignment][width]s For exampleSystem.out.format("%-6s %4s", "Test1", "90");displays:

Test1 90

Page 7: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 7

Chapter 3Chapter 3Code ConventionsCode Conventions

An introductory comment should begin a program.

Package names should begin with a lowercase letter and then an uppercase letter should begin each word within the name.

Class names should be nouns and begin with an uppercase letter and an uppercase letter should begin each word within the name.

A comment block should be included before each class.

Page 8: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 8

Chapter 3Chapter 3Code Conventions Code Conventions ((con'tcon't))

Comments should not reiterate what is clear from the code.

Statements in a method should be indented. An open curly brace ({) should be placed on the

same line as the class or method declaration, and the closing curly brace (}) should be on a separate line and aligned with the class or method declaration.

Page 9: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 9

Chapter 3Chapter 3Flowchart SymbolsFlowchart Symbols

input/outputinput/outputstart/endstart/end

Page 10: 2007 Lawrenceville Press Slide 1 Chapter 3 Classes and Objects 1. How is a class different from an object?

© 2007 Lawrenceville PressSlide 10

Chapter 3Chapter 3The Triangle FlowchartThe Triangle Flowchart