Lesson 3 introducing the java language

Page 1

Lesson 3 Introducing the Java Language

4-1


Objectives • After completing this lesson, you should be able to: – – – – – –

– – –

4-2

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


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


Relevance • How do you test something that you have built, such as a house, a piece of furniture, or a program?

4-4


Identifying the Components of a Class Shirt Order Date

Window

OrderEntry Customer

Button

4-5


Structuring Classes – Class declaration – Field de larations lass attri utes are alled fields • Fields may also be initialized at declaration time.

– Methods (optional) – Comments (optional)

4-6


Structuring Classes public class Shirt {

Field declarations

Method

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 public int quantityInStock = 0; // 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); System.out.println("Shirt price: " + price); System.out.println("Quantity in stock: " + quantityInStock); } // end of display method } // end of class

4-7

Class declaration


Symbols Used in Defining a Java Source

– – – – – – –

4-8

Braces Parentheses Semicolons Commas Single quotation marks Double quotation marks Single-line comment


Putting It All Together – Syntax for declaring a class: [modifiers] class class_identifier

– Class example:

public class Shirt{ public double price; public void setPrice(double priceArg){ price = priceArg; }

} Open and closing braces for the Shirt class

4-9


Quiz • Select the class declaration that adheres to the class-naming guidelines. a. b. c. d.

4 - 10

class Shirt public Class 501Pants public Shirt public Class Pants


Field Declarations and Assignments • • • • •

4 - 11

public public public public public

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


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: • • •

4 - 12

/******************************************* * Attribute Variable Declaration Section * *******************************************/


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 - 13


Methods • • •

• • • • • • • •

4 - 14

– 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


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


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 - 16


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 - 17


Creating and Using a Test Class • Example: class ShirtTest { public static void main (String[] args) { Shirt myShirt; myShirt= new Shirt(); myShirt.displayInformation();

} }

4 - 18


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 - 19


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

javac Shirt.java

4 - 20

– Example:


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: •

• • • • •

4 - 21

java ShirtTest

• Output: Shirt ID: 0 Shirt description:-description requiredColor Code: U Shirt price: 0.0 Quantity in stock: 0


Compiling and Running a Program by Using an IDE

Save is equivalent to javac. Run is equivalent to java.

4 - 22


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 - 23


Avoiding Syntax Problems

4 - 24


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 - 25


Working with an IDE Debugger

4 - 26


Summary • In this lesson, you should have learned how to: – – – – – – – – – –

4 - 27

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


Practice for Lesson 3 Overview: • 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

• In this practice, you create a Java class and compile it. You also create another Java class to test the previous class. • 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 - 28


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.