Escolar Documentos
Profissional Documentos
Cultura Documentos
4-2 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Topics
4-3 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Relevance
4-4 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Identifying the Components of a Class
Shirt
Order
Date
Window
OrderEntry
Customer
Button
4-5 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Structuring Classes
• Class declaration
• Field declarations (class attributes are called “fields”)
– Fields may also be initialized at declaration time.
• Methods (optional)
• Comments (optional)
4-6 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Structuring Classes
declarations
// This method displays the values for an item
public void displayInformation() {
System.out.println("Shirt ID: " + shirtID);
System.out.println("Shirt description:" + description);
System.out.println("Color Code: " + colorCode);
4-7 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Symbols Used in Defining a Java Source
• Braces
• Parentheses
• Semicolons
• Commas
• Single quotation marks
• Double quotation marks
• Single-line comment
4-8 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Putting It All Together
4-9 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Quiz
• Single-line:
public int shirtID = 0; // Default ID for the shirt
public double price = 0.0; // Default price for all shirts
• Traditional:
/*******************************************
* Attribute Variable Declaration Section *
*******************************************/
• Syntax:
[modifiers] return_type method_identifier ([arguments]){
method_code_block
}
• Example:
public void displayInformation() {
Example:
class ShirtTest {
Shirt myShirt;
myShirt= new Shirt();
myShirt.displayInformation();
}
}