0% found this document useful (0 votes)
68 views51 pages

Farrell - JavaP - 10e - ch01 - PowerPoint Java Java

This document help beginners to learn how to program using Java and will help how to start and learn to program.

Uploaded by

qkjrm9kf28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views51 pages

Farrell - JavaP - 10e - ch01 - PowerPoint Java Java

This document help beginners to learn how to program using Java and will help how to start and learn to program.

Uploaded by

qkjrm9kf28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Java Programming,

10e
Chapter 01: Creating Java
Programs

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
1
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Icebreaker: Interview Simulation
1. The class will be broken up into pairs of students.
2. Each student will create three questions to ask their partner:
a) one True/False question;
b) one question that can be answered with a number;
c) one question that requires a short answer
3. Partners will take turns asking the questions they’ve prepared.
4. Each student will then introduce their partner to the class using the
information they’ve learned.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Chapter Objectives
By the end of this chapter, you should be able to:
01.01 Define basic programming terminology
01.02 Compare procedural and object-oriented programming
01.03 Describe the features of the Java programming language
01.04 Analyze a Java application that produces console output
01.05 Compile a Java class and correct syntax errors
01.06 Run a Java application and correct logic errors
01.07 Add comments to a Java class
01.08 Create a Java application that produces GUI output
01.09 Identify and consult resources to help develop Java programming
skills
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.1 Learning Programming Terminology (1 of 3)
Computer program
• A set of written instructions that tells the computer what to do
Machine language
• The most basic circuitry-level language
• A low-level programming language
• All programs get translated into machine language
High-level programming language
• Allows you to use a vocabulary of reasonable terms

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.1 Learning Programming Terminology (2 of 3)
Syntax
• A specific set of rules for the language
Program statements
• Similar to English sentences
• Commands to carry out program tasks
Compiler or interpreter
• Translates language statements into machine code
Debugging
• Freeing program of all errors

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.1 Learning Programming Terminology (3 of 3)
• Syntax error
• Misuse of language rules
• A misspelled programming language word
• Logic errors
• Also called semantic errors
• Incorrect order or procedure
• The program may run but provide inaccurate output

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.1: Knowledge Check
Every language has its own specific, limited vocabulary called the language’s
________.
a. syntax
b. keywords
c. statements
d. commands

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.1: Knowledge Check Answer
Every language has its own specific, limited vocabulary, called the language’s
________.

Answer: b. keywords
When you are learning a computer programming language, you are learning
the vocabulary and syntax for the language.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.2 Comparing Procedural and Object-Oriented
Programming Concepts (1 of 5)
Procedural programming
• Sets of operations executed in sequence
• Variables
• Named computer memory locations that hold values
• Procedures
• Individual operations grouped into logical units
Object-oriented programs
• Create classes
• Blueprints for an object
• Create objects from classes
• Create applications

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.2 Comparing Procedural and Object-Oriented
Programming Concepts (2 of 5)
• Class
• Describes objects with common properties
• A definition
• An instance
• Attributes
• Characteristics that define an object
• Differentiate objects of the same class
• The value of attributes is an object’s state
• Objects
• Specific, concrete instances of a class

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.2 Comparing Procedural and Object-Oriented
Programming Concepts (3 of 5)

Figure 1-2 Dog


class definition
and some objects
created from it

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.2 Comparing Procedural and Object-Oriented
Programming Concepts (4 of 5)
• Method
• A self-contained block of program code that carries out an action
• Similar to a procedure
• Encapsulation
• Conceals internal values and methods from outside sources
• Provides security
• Keeps data and methods safe from inadvertent changes

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.2 Comparing Procedural and Object-Oriented
Programming Concepts (5 of 5)
Inheritance
• An important feature of object-oriented programs
• Classes share attributes and methods of existing classes but with more
specific features
• Helps you understand real-world objects
Polymorphism
• Means “many forms”
• Allows the same word to be interpreted correctly in different situations
based on context

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.3 Features of the Java Programming Language (1
of 4)
Java
• Object-oriented general-purpose language
• Can be run on a wide variety of computers
• Does not execute instructions on the computer directly
• Runs on a hypothetical computer known as a Java Virtual Machine
(JVM)
• Advantages:
• Security features
• Architecturally neutral

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.3 Features of the Java Programming Language (2
of 4)
Source code
• Programming statements written in high-level programming language
Development environment
• A set of tools used to write programs
Bytecode
• Statements saved in a file
• A binary program into which the Java compiler converts source code
Java interpreter
• Checks bytecode and communicates with the operating system
• Executes bytecode instructions line by line within the Java Virtual Machine

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.3 Features of the Java Programming Language (3
of 4)

Figure 1-3 The


Java environment

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.3 Features of the Java Programming Language (4
of 4)
WORA
• Write once, run anywhere
Console applications
• Support character output
Windowed applications
• Menus
• Toolbars
• Dialog boxes

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.2: Knowledge Check
The slogan, Write once, run anywhere, describes the ability of one Java
program to ________.
a. work correctly on multiple platforms
b. translate into different languages
c. perform unrelated tasks
d. create GUIs

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.2: Knowledge Check Answer
The slogan, Write once, run anywhere, describes the ability of one Java
program to ________.

Answer: a. work correctly on multiple platforms


With Java, one program version can run on Windows, Macintosh, UNIX, and
Linux.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (1 of 9)

Figure 1-5 Anatomy of a Java statement

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (2 of 9)

• Even the simplest Java application involves a fair amount of confusing


syntax.
• All Java statements end with a semicolon.
• Most Java statements can be spread across multiple lines if you place line
breaks in appropriate places.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (3 of 9)
Literal string
• Will appear in output exactly as entered
• Written between double quotation marks
Arguments
• Pieces of information passed to a method
Method
• Requires information to perform its task
System class
• Refers to the standard output device for a system

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (4 of 9)
• Everything used within a Java program must be part of a class
• Define a Java class using any name or identifier
• Upper Camel casing (Pascal casing)
• Each word of an identifier begins with uppercase letter
• UnderGradStudent
• InventoryItem
• Access specifier
• Defines how a class can be accessed

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (5 of 9)
Identifiers must begin with one of the following:
• Letter of the English alphabet
• Non-English letter (such as α or π)
• Underscore
• Dollar sign

Identifiers can only contain:


• Letters
• Digits
• Underscores
• Dollar signs

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (6 of 9)
Identifiers cannot:
• be a Java reserved keyword
• be true, false, or null
• begin with a digit

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (7 of 9)
Method header
• The first line in a method
• Contains information about how other methods can interact with it
String class
• Java class used to hold character strings

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (8 of 9)
static
• A reserved keyword
• Means the method is accessible and usable even though no objects of the
class exist
void
• Use in the main() method header
• Does not indicate the main() method is empty
• Indicates the main() method does not return a value when called
• Does not mean that main() doesn’t produce output

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.4 Analyzing a Java Application that Produces
Console Output (9 of 9)
Indent Style
• Use whitespace to organize code and improve readability
• For every opening curly brace ( { ) in a Java program, there must be a
corresponding closing curly brace
(})
• Placement of the opening and closing curly braces is not important to the
compiler
• Allman style used in text
To save a Java class
• Save the class in a file with exactly the same name and .java extension
• For public classes, class name and filename must match exactly

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.5 Compiling a Java Class and Correcting Syntax
Errors (1 of 4)
• Compiling a Java class
• Compile the source code into bytecode
• Translate the bytecode into executable statements using a Java
interpreter
• Type javac First.java
• Compilation outcomes
• javac is an unrecognized command
• Program language error messages
• No messages indicating successful completion

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.5 Compiling a Java Class and Correcting Syntax
Errors (2 of 4)

Reasons for error messages


• Misspelled the command javac
• A misspelled filename
• Not within the correct subfolder or subdirectory on the command line
• Improper installation of Java

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.5 Compiling a Java Class and Correcting Syntax
Errors (3 of 4)
The first line of the error message displays:
• The name of the file where the error was found
• The line number
• The nature of the error
Next lines identify:
• The symbol
• The location

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.5 Compiling a Java Class and Correcting Syntax
Errors (4 of 4)
Compile-time error
• The compiler detects a violation of language rules
• Refuses to translate the class to machine code
Parsing
• Compiler divides source code into meaningful portions

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.6 Running a Java Application and Correcting
Logical Errors (1 of 4)
• Run the application from the command line
• Type java First
• Shows the application’s output in the command window
• The class is stored in a folder named Java on the C drive

Figure 1-18 Output of the First application

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.6 Running a Java Application and Correcting
Logical Errors (2 of 4)

1. Modify the text file that contains the existing class


2. Save the file with changes using the same filename
3. Compile the class with the javac command
4. Interpret the class bytecode and execute the class using the java
command

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.6 Running a Java Application and Correcting
Logical Errors (3 of 4)

Figure 1-19 First class containing output modified


from the original version

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.6 Running a Java Application and Correcting
Logical Errors (4 of 4)
Logic error
• The syntax is correct but incorrect results were produced when executed
Run-time error
• Not detected until execution
• Often difficult to find and resolve

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.3: Discussion

1. How do syntax errors and logic errors differ? Discuss what defines a logic
error, list some examples, and explain why they are more difficult to detect.

2. Identify why each of the following class names are illegal in Java:
• Shoe Type
• 2023inventory
• private
• Item#

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.3: Discussion Debrief
1. A syntax error occurs when there is a misuse of language, such as a
misspelled word. A logic error occurs when the syntax of a program is
correct, but the results it produces are incorrect. Examples of logic errors
include: misspelling a word in a literal string or instructing to multiply two
numbers instead of divide. The compiler doesn’t find errors such as spelling
errors in output, or incorrectly-used mathematical operators. It is the
responsibility of the program author to test programs and find logic errors.

2. Each of the following class names are illegal in Java:


• Shoe Type: Includes a space
• 2023inventory: Starts with a number
• Private: Uses a reserved keyword
• Item#: Contains the number symbol
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.7 Adding Comments to a Java Class (1 of 3)

Program comments
• Nonexecuting statements added to a program for documentation
• Use to leave notes for yourself or others
• Include the author, date, and class’s name or function
Comment out a statement
• Turn it into a comment
• The compiler does not translate, and the JVM does not execute its
command

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.7 Adding Comments to a Java Class (2 of 3)
Line comments
• Start with two forward slashes (//)
• Continue to the end of the current line
• Do not require an ending symbol
Block comments
• Start with a forward slash and an asterisk (/*)
• End with an asterisk and a forward slash (*/)
Javadoc comments
• A special case of block comments
• Begin with a slash and two asterisks (/**)
• End with an asterisk and a forward slash (*/)
• Use to generate documentation
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.7 Adding Comments to a Java Class (3 of 3)

Figure 1-22 A program segment containing several


comments

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.8 Creating a Java Application that Produces GUI
Output (1 of 3)
Dialog box
• A GUI object resembling a window
• Messages placed for display
• JOptionPane class is used to produce
import statement
• Use to access a built-in Java class
Package
• A group of classes

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.8 Creating a Java Application that Produces GUI
Output (2 of 3)

Figure 1-23 The FirstDialog class

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.8 Creating a Java Application that Produces GUI
Output (3 of 3)

Figure 1-24 Output of the FirstDialog application

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.9 Finding Help (1 of 3)

Java API
• Also called the Java class library
• Provides prewritten information about Java classes
FAQs on the Java Web site
Java Development Kit (JDK)
• A software development kit (SDK) of programming tools
• Free to download

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.9 Finding Help (2 of 3)

Don’t forget
• The file’s name must match the class name
• To end a block comment
• Java is case sensitive
• To end every statement with a semicolon
• To recompile when making changes

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
1.9 Finding Help (3 of 3)
• Don’t confuse these terms: Parentheses, braces, brackets, curly braces,
square brackets, and angle brackets
• Do not end class or method headers with a semicolon
• Don’t panic when you see a lot of compiler error messages
• Don’t assume your program is perfect when all compiler errors are
eliminated

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.4: Case Study

You have been tasked with writing a simple program with a GUI output. You
want to follow good programming practices and may need some help along the
way.
• Should you use a procedural or object-oriented programming language?
Explain your choice.
• What types of comments should you include in her program? What is the
purpose of adding comments?
• The program needs to produce a window that presents options to users.
What should you create? What is the name of the class you should use?
• If you gets stuck, what are some of the resources you can consult? Where
can you find them?

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Activity 1.4: Case Study Debrief

• You should use an object-oriented programming language because it is


suited for GUI applications.
• You can add comments such as Line, Block, and JavaDoc. Comments are
non-executing statements that document a program for people reading the
source code.
• You should create a dialog box using the JOptionPane class.
• Some of the resources you can use include FAQs from the Java website,
and tutorials such as “The Java Tutorial,” which you can download. You also
can use a Java Development Kit to access tools.

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Self-Assessment

1. How would you differentiate between procedural and object-oriented


programming?

2. Why is it important to identify logic errors if the program can run without
fixing them? Why is it so difficult to identify logic errors?

3. Why is it important to add comments to a Java class? What types of


comments are most useful, and why?

4. What resources can you use to further develop your Java programming
skills? Where can you locate these resources?
Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary

Click the link to review the objectives for this presentation.


Link to Objectives

Joyce Farrell, Java Programming, 10th Edition. © 2023 Cengage. All Rights Reserved. May not be scanned,
copied or duplicated, or posted to a publicly accessible website, in whole or in part.

You might also like