Creating a Program - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Creating a Program

Description:

A First Program Using Java. Programming ... All Java applications require a main() method. The main() method always executes first ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 19
Provided by: Staf205
Category:
Tags: creating | java | program

less

Transcript and Presenter's Notes

Title: Creating a Program


1
Creating a Program
  • Chapter 1
  • Dr. James Jiang

A First Program Using Java
2
Programming
  • Program set of instructions that tell a
    computer what to do
  • High-level programming language
  • Syntax rules of the language
  • Logic produce desired results
  • Compiler/interpreter/assembler translates
    high-level language to machine code

3
Programming Approaches
Procedural programming
Object-oriented programming
4
Procedural Programming
  • Create memory locations ? variables
  • Write operations to manipulate variable values
  • Operations are grouped into logical units ?
    procedures
  • Procedures are called or invoked

5
Object-Oriented Programming
  • An extension of procedural programming
  • Program components are objects
  • OOP objects are manipulated
  • DOS vs Windows
  • Objects are made up of states and methods
  • States describe characteristics (attributes) of
    an object

6
Class
Inheritance
  • A collection of attributes methods
  • An object derived from an existing class is an
    instance of the class
  • Goldie, the retriever, instance of class dogs
  • Inheritance ability of an object to take on
    characteristics of base class
  • Apply general knowledge of class to individual
    members of the class

7
Methods
  • Code segments used to perform a task
  • Attributes and methods are encapsulated into
    objects

Encapsulation
8
Encapsulation
  • Act of assembling attributes, methods, other
    elements into a single self-contained unit
  • Encapsulated objects hide low-level details (how
    attributes are set how methods are executed)
  • Attributes and methods are executed through an
    interface

9
Java
  • Sun Microsystems
  • General-purpose business applications
  • Interactive WWW-based programs
  • Runs on any platform (architecturally neutral)
  • Bytecode compiled form of Java that can run on
    any platform with a Java interpreter
  • Interpreter program that translates code
  • Java Virtual Machine (JVM) the Java language
    interpreter
  • Java eliminates difficult features found in C

10
First Java Program
  • public class First
  • public static void main(String args)
  • System.out.println(First Java program)

11
Syntax
  • All statements must end with a semicolon ( )
  • Literal strings must appear between double
    quotation marks
  • Arguments to methods must appear in parentheses
  • Argument information that methods require
  • println() method
  • prints a line of output on the screen
  • positions the cursor on the next line

12
Syntax
  • out is an object that represents the screen
  • print() method does not advance cursor
  • System is a class (case sensitive)
  • Periods ( . ) separate class, object, method
  • System.out.println()

Everything is an object and every object is a
member of a class

13
Creating a Class
  • public class First
  • Defines a class named First
  • Creating class names
  • Must begin with a letter of the alphabet
  • Only digits, letters, underscores,
  • No reserved words
  • Not true, false, null
  • Begin class names with uppercase letters

14
Syntax
  • public keyword is an access modifier
  • Defines how a class may be accessed
  • Enclose contents of all classes within curly
    brackets
  • Methods require a set of curly brackets
  • Brackets must be used in pairs
  • Opening bracket ?
  • Closing bracket ?

15
The main() Method Header
  • First line of a method
  • All Java applications require a main() method
  • The main() method always executes first
  • Example (explanation p. 11)
  • public static void main(String args)

16
Shell Output Program
  • public class First
  • public static void main(String args)
  • /insert code here/

17
Writing a First Program
  • Class name must be the same as the classs
    filename
  • Java is case sensitive
  • Program comments
  • Line comments ? //
  • Block comments ? / /

18
Running a Program
  • Compile the program (source code) into bytecode
  • javac Hello.java
  • ( see Appendix A)
  • Results
  • Bad command or file name
  • Program language error messages
  • No messages

Java is case-sensitive
Write a Comment
User Comments (0)
About PowerShow.com