Download presentation
Presentation is loading. Please wait.
1
Your First Java Application
Chapter 2 Your First Java Application
2
Objectives understand how computer programs model the world
understand the difference between a class and an object define fields and state explain what methods are understand how objects create relationships define the terms association, composition and specialization …continued
3
Objectives explain the difference between syntax and semantics
write, compile and run a Java applet with an HTML container write, compile and run a Java application using a dialog box*
4
Installing Java follow the instructions on the projection screen
install Java Development Kit (JDK) from the Sun website Install Java Runtime Environment (JRE)
5
Program Concepts Modern object-oriented programs help us build models to manage the complexity found in a problem domain. The “problem domain” describes real-world objects and concepts that a computer program is trying to solve.
6
Models A model is a simplification of a complex system. A good model:
Helps to identify the most important aspects of a problem. Helps a programmer to focus on the problem he or she is trying to solve instead of on the complexity of the problem’s data.
7
What is a metaphor? A metaphor is a word or phrase used in place of another word or phrase to denote a likeness. Computer programs use “metaphors” to represent real objects.
8
Objects Objects represent real-world things: Objects have: Ship Rudder
Wheel Instruments Objects have: properties (characteristics) methods (behaviors)*
9
Classes and Objects A class is a definition of a type:
Like a template, a class defines the characteristics and behaviors of the type. An object is an instance of a class: Can be instantiated and manipulated An object’s characteristics are defined by the class that was used to create the object. (continued…)
10
Classes and Objects an object is an “instance” of a class
the dog Bowser is an instance of the class dog the act of creating an object is called “instantiation”
11
Fields Fields define the properties of a class.
Can be intrinsic or built-in “types” (int, boolean…) Can be user-defined objects fields, instance variables and properties are interchangeable State is the current value of a field in an object.*
12
Methods Methods describe the capabilities of the class.
The capabilities are implemented by a method. example: a Dog can bark( ), a spaceship can fire( ) Every method must be called from another method. The only exception is main(), which is called by the OS. Methods can accept parameters. Parameters are enclosed in the parentheses*
13
Object Relationships Association Composition
A method of one object calls the method of another object. Composition Some objects are composed of other objects.*
14
Specialization Hierarchies of classes move from a generalized class to a more specific class.
15
Topic Check Complete Questions 1 – 3, p. 43*
16
Creating Programs Syntax and Semantics
A language’s exact keywords, punctuation, and order of terms are called its syntax. Semantics refers to the meaning of one’s instructions; what the program is trying to accomplish.*
17
Writing a Java Program Writing a Java program requires five steps:
Analyze Design Write Compile Test*
18
Compiling the Code Programs must be compiled in order for the CPU to understand and execute the instructions. Java byte code is run through the Java Virtual Machine (JVM), which translates the code for the CPU. .java files are compiled by the javac (java compiler) program into .class files.*
19
Types of Java Programs Applet Application
Requires an .HTML file with the <applet> tag to reference the .class file Can also be run using the appletviewer.exe program file Application Run by the java.exe program file Run without a browser interface, often using the console window*
20
HelloWorld.java Analysis
The keyword import allows existing class libraries to be reused. The keyword public signifies that a class is visible to other classes. Braces {} indicate the beginning and end of a block of code. Braces must always be “balanced” Align opening and closing braces*
21
More HelloWorld.java Analysis
The keyword void signifies that a method does not return a value. Identifiers are the names given to classes, fields, and methods. Identifiers are case sensitive. Identifiers often use camel case notation.*
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.