You are on page 1of 30

Introducing the Java Language

Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Objectives
After completing this lesson, you should be able to:
• Define a class
• Identify the components of a class
• Explain the term object
• Describe the purpose of a variable
• Discuss methods and describe how to use a main method
• Describe the elements that comprise a Java class, such as
declarations, return values, and the proper use of brackets
and braces
• Identify keywords and describe their purpose
• Test and execute a simple program
• Describe some common causes
of syntax errors
• Describe the purpose and features of an IDE debugger

4-2 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Topics

• Define a class, identify class components, and use


variables
• Discuss methods and the use of a main method
• Identify keywords
• Test and execute a simple Java program
• Describe some common causes of syntax errors
• Describe the purpose and features of an IDE debugger

4-3 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Relevance

How do you test something that you have built, such as a


house, a piece of furniture, or a program?

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

public class Shirt { Class declaration


public int shirtID = 0; // Default ID for the shirt
public String description = "-description required-"; // default
// The color codes are R=Red, B=Blue, G=Green, U=Unset
public char colorCode = 'U';
public double price = 0.0; // Default price for all shirts
Field public int quantityInStock = 0;

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

Method System.out.println("Shirt price: " + price);


System.out.println("Quantity in stock: " + quantityInStock);

} // end of display method


} // end of class

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

• Syntax for declaring a class:


[modifiers] class class_identifier
• Class example:
public class Shirt{
public double price;
Open and
closing
public void setPrice(double priceArg){braces for
price = priceArg; the Shirt
class
}
}

4-9 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Quiz

Select the class declaration that adheres to the class-naming


guidelines.
a. class Shirt
b. public Class 501Pants
c. public Shirt
d. public Class Pants

4 - 11 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Field Declarations and Assignments

public int shirtID = 0;


public String description = "-description required-";
public char colorCode = 'U';
public double price = 0.0;
public int quantityInStock = 0;

4 - 12 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Comments

• Single-line:
public int shirtID = 0; // Default ID for the shirt
public double price = 0.0; // Default price for all shirts

// The color codes are R=Red, B=Blue, G=Green

• Traditional:
/*******************************************
* Attribute Variable Declaration Section *
*******************************************/

4 - 13 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Topics

• Define a class, identify class components, and use


variables
• Discuss methods and the use of a main method
• Identify keywords
• Test and execute a simple Java program
• Describe some common causes of syntax errors
• Describe the purpose and features of an IDE debugger

4 - 15 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Methods

• Syntax:
[modifiers] return_type method_identifier ([arguments]){
method_code_block
}

• Example:
public void displayInformation() {

System.out.println("Shirt ID: " + shirtID);


System.out.println("Shirt description:" + description);
System.out.println("Color Code: " + colorCode);
System.out.println("Shirt price: " + price);
System.out.println("Quantity in stock: " + quantityInStock);

} // end of display method

4 - 16 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Topics

• Define a class, identify class components, and use


variables
• Discuss methods and the use of a main method
• Identify keywords
• Test and execute a simple Java program
• Describe some common causes of syntax errors
• Describe the purpose and features of an IDE debugger

4 - 18 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Keywords

abstract default for package synchronized

assert do if private this

boolean double implements protected throw

break else import public throws

byte enum instanceof return transient

case extends int short true

catch false interface static try

char final long strictfp void

class finally native super volatile

continue float new switch while

4 - 19 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Topics

• Define a class, identify class components, and use


variables
• Discuss methods and the use of a main method
• Identify keywords
• Test and execute a simple Java program
• Describe some common causes of syntax errors
• Describe the purpose and features of an IDE debugger

4 - 20 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Creating and Using a Test Class

Example:

class ShirtTest {

public static void main (String[] args) {

Shirt myShirt;
myShirt= new Shirt();

myShirt.displayInformation();

}
}

4 - 21 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


main Method

• A special method that the JVM recognizes as the starting


point for every Java technology program that runs from a
command line
• Syntax:
public static void main (String[] args)

4 - 22 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Compiling a Program

1. Go to the directory where the source code files are stored.


2. Enter the following command for each .java file you want
to compile.
• Syntax:
javac filename
• Example:
javac Shirt.java

4 - 23 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Executing (Testing) a Program

1. Go to the directory where the class files are stored.


2. Enter the following for the class file that contains the main
method:
• Syntax:
java classname
• Example:
java ShirtTest
• Output:
Shirt ID: 0
Shirt description:-description required-
Color Code: U
Shirt price: 0.0
Quantity in stock: 0

4 - 24 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Compiling and Running a Program
by Using an IDE

Save is equivalent to javac.

Run is equivalent to java.

4 - 25 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Topics

• Define a class, identify class components, and use


variables
• Discuss methods and the use of a main method
• Identify keywords
• Test and execute a simple Java program
• Describe some common causes of syntax errors
• Describe the purpose and features of an IDE debugger

4 - 26 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Avoiding Syntax Problems

4 - 27 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Topics

• Define a class, identify class components, and use


variables
• Discuss methods and the use of a main method
• Identify keywords
• Test and execute a simple Java program
• Describe some common causes of syntax errors
• Describe the purpose and features of an IDE debugger

4 - 28 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Working with an IDE Debugger

4 - 29 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Summary
In this lesson, you should have learned how to:
• Define a class
• Identify the components of a class
• Explain the term object
• Describe the purpose of a variable
• Discuss methods and describe how to use a main method
• Describe the elements that comprise a Java class, such as
declarations, return values, and the proper use of brackets
and braces
• Identify keywords and describe their purpose
• Test and execute a simple program
• Describe some common causes of
syntax errors
• Describe the purpose and features of an IDE debugger

4 - 31 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Practice 4-1 Overview: Viewing and Adding Code
to an Existing Java Program
In this practice, you are given a completed Java program.
During the practice, you will:
• Open the Java program
• Examine the lines of code
• Modify the program
• Compile the program
• Test it by executing the program

4 - 32 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Practice 4-2 Overview:
Creating and Compiling a Java Class
In this practice, you create a Java class and compile it. You
also create another Java class to test the previous class.

4 - 33 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.


Practice 4-3 Overview:
Exploring the Debugger
In this practice, you debug the ShirtTest program by using
the NetBeans debugger. During the practice, you will:
• Set a breakpoint
• Examine and modify field values
• Use a step through

4 - 34 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.

You might also like