Title: CITA 140 Introduction To Computer Programming
1CITA 140 Introduction To Computer Programming
- Instructor Dr. Douglas Dune, Ph.D.
2Java Programming, Third Edition
- Chapter One
- Creating Your First Java Classes
3Objectives
- Learn about programming
- Be introduced to object-oriented programming
concepts - Learn about Java
- Analyze a Java application that uses console
output - Add comments to a Java class
4Objectives (continued)
- Save, compile, and run a Java application
- Modify a Java class
- Create a Java application using GUI output
- Correct errors and find help
5Learning About Programming
- Program set of instructions directing computer
action - Machine language based on circuit switching
- High-level programming languages
- Languages approximating natural language syntax
- Example terms read, write, and add
- Syntax grammatical rules of programming language
6Learning About Programming (continued)
- Compiler translates high-level code into object
program - Interpreter converts and executes statements
line by line - Debugging locating and repairing errors
- Program logic structural integrity
7Introducing Object-Oriented Programming Concepts
- Procedure set of operations forming program unit
- Variable named computer memory location
- Procedural programming
- Procedure acts on separate data to perform
task(s) - Procedure may call or invoke other procedures
- Object-oriented programming
- Views application as a set of interacting objects
- Objects contain data and methods
8Introducing Object-oriented Programming Concepts
(continued)
- Three OOP language requirements
- Encapsulation, inheritance, polymorphism
- Class object template encapsulating methods and
data - Object components
- Attributes (states) defining characteristics
- Methods self-contained blocks of program code
- Instance term for an existing object of a class
9Introducing Object Oriented Programming Concepts
(continued)
- Inheritance class deriving methods/data from
another - Example Mustang derives features from generic
Ford - Polymorphism method operation specified by
context - close ( ) interpreted differently by printer and
browser
10Learning About Java
- Java was developed by Sun Microsystems
- Object-oriented language modeled after C
- Designed for interactive Web-based applications
- Also suitable for general purpose applications
- Java is architecturally neutral
- Java is compiled and interpreted
- Source code form of Java (and all high-level)
programs
11Learning About Java (continued)
- Java programs run on Java Virtual Machine (JVM)
- Three step process from source code to executable
- Source code written in text editor
- Java compiler translates into bytecode (binary
program) - Java interpreter executes line by line in JVM
12(No Transcript)
13Java Program Types
- Java applets programs embedded in a Web page
- Java applications stand-alone programs
- Console support character output to DOS window
- Windowed create GUIs (graphical user interfaces)
14Analyzing A Java Application That Uses Console
Output
- Console applications relatively easy to write
- The First class is written on seven lines
- First's sole task output "First Java
Application"
15(No Transcript)
16Understanding the Statement That Prints the
Output
- Literal string character sequence enclosed by "
" - Argument data passed to a method
- Parsing System.out.println("First Java
Application") - out is a System object
- println ( ) is a method encapsulated in out
- Dot operator separates class, object, method
- println ( ) takes literal string argument
- println( ) outputs string, places cursor on next
line
17Understanding the First Class
- All coding takes place within classes
- Class defined by using name or identifier
- Identifiers must meet four requirements
- Contain letters, digits, underscores, or dollar
signs - Begin with a letter, underscore, or dollar sign
- May not be a Java reserved keyword, such as class
- May not be primitive value true, false, or null
- Convention begin class name with uppercase letter
18(No Transcript)
19(No Transcript)
20(No Transcript)
21(No Transcript)
22Understanding the First Class (continued)
- First line public class First
- Keyword class identifies First as a class
- Public access modifier defining permissions
- Contents enclosed within curly braces
- Includes main ( ) method
- main ( ) method encloses another method, println(
)
23Understanding the Main Method
- Parsing public static void main(String args)
- public defines access level (least restrictive)
- static indicates method independent of object
- void indicates method does not return a value
- String indicates argument type (string class)
- The name args is a placeholder for argument
- class First may be used as a shell (or template)
- Replace println( ) with block comments
24(No Transcript)
25Adding Comments To A Class
- Comments non-executing lines documenting program
- Statements "commented out" ignored by compiler
- Three ways to comment
- Line comments //
- Block comments / /
- javadoc comments / /
26(No Transcript)
27Saving a Java Class
- Java class stored in secondary memory (disk)
- Save public class with .java extension
- Example First.java
- Class name and file name must match
- Java is case-sensitive
28Compiling a Java Class
- Two-step translation process
- Source code compiled into bytecode
- Bytecode interpreted into executable statements
- Compiling at the command line
- Source code file should be in path
- Type javac followed by name of file
- Example javac First.java
- Syntax errors require debugging in text editor
29Running a Java Program
- Running application
- Type java followed by class name
- Example java First
- Application output appears in command window
30(No Transcript)
31Modifying a Java Class
- To produce new output, modify text file
- Open text file that contains existing program
- Change original literal string
- Add an additional literal string with new println
( ) - Save file with changes (under same name)
- Recompile class with javac command
- Interpret bytecode and execute using java
command
32(No Transcript)
33Creating A Java Application Using GUI Output
- JOptionPane provides dialog boxes for GUI output
- Implementation
- Import class package containing JOptionPane
- Example import javax.swing.JOptionPane
- Use showMessageDialog( )
- Like println ( ), but with two arguments
- Pass null and new literal string
- Must use exit ( ) method to return control to OS
34(No Transcript)
35(No Transcript)
36Correcting Errors and Finding Help
- Syntax errors
- Concern violation of language rules
- Captured and highlighted by compiler
- Logic error
- Syntactically correct, but semantically invalid
- Captured at run-time by operating system (crash)
- Example class ErrorTest
- Comments in second line not properly closed
37(No Transcript)
38(No Transcript)
39Correcting Errors and Finding Help (continued)
- After debugging error, program recompiled
- Java resources at http//java.sun.com
- Documentation
- Java API (class library)
- FAQs (Frequently Asked Questions)
- SDK (Java Software Development Kit)
- The Java Tutorial
40Summary
- OO features encapsulation, inheritance,
polymorphism - OO programs consist of interacting objects
- Object is an instance of a class
- Objects encapsulate methods and attributes
- Java object-oriented programming language
41Summary (continued)
- All Java coding in the context of classes
- Java architecturally neutral
- Java source code compiled into bytecode
- Java interpreter executes bytecode within JVM
- Java program types applets and applications
42Summary (continued)
- Syntax grammatical rules of language
- Logic program semantics
- Comments non-executable documenting statements
- Compilation command javac fileName.java
- Run command java fileName