SC300 Unit one Prof. Julie Shertzer [email protected] AIM: JulieShertzer.
IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir...
-
Upload
roy-maxwell -
Category
Documents
-
view
220 -
download
2
Transcript of IT258 Foundation of Programming Using Java Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir...
IT258 Foundation of Programming Using Java
Unit 2 Seminar :(Chapter 1 )
Instructor : Vladimir Gubanov, PhDEmail : [email protected]
A reminder:
1. Our Seminars :
they will be each Thursday , from 9 PM to 10 PM EST
2. My Office Hours :
Mondays, 7PM to 8PM
Saturdays, 9AM to 10 AM
3. My email :
Java Programming, Fifth Edition
Chapter One:Creating Your First Java Classes
Objectives
• Object-oriented programming concepts
• Learn about Java• Analyze a Java application that uses
console output• Save, compile, run, and modify a
Java application• Create a Java application using GUI
outputJava Programming, Fifth Edition
4
Java Programming, Fifth Edition
5
Object-Oriented Programming Concepts
• Procedural programming – Sets of operations executed in sequence– Variables hold values– Procedures group operations in logical units
• Object-oriented programs – Create classes first– Create objects from classes when a
functionality of the class needs to be used– Object-oriented programming includes:
– Encapsulation– Inheritance– Polymorphism
Java Programming, Fifth Edition
6
Understanding Objects and Classes• Objects
– Made up of attributes, methods, events
• Attributes– Characteristics that define object– Value of attributes is object’s state
• Class – Describes objects with common properties– Definition – template for objects– Instance of a class – an object to use
OBJECT
• Represents something you would find in the “real-world”
• Objects can be described by their attributes(properties) and behaviors(methods)
• In Java, an object is represented by a software module, which contains a collection of related data and procedures
Object: Car
PROPERTY (or field)
• Attribute which describes the object:
• In Java, a property is represented by a variable associated with the object
Property: Color Make Year VIN
Method: Start Accelerate Turn Left Stop
METHOD
• Behavior of the object or what the object can do
• In Java, a method is represented by a public subroutine or function related to the object
State
Object: CarProperty: GasolineLevel OilLevel Speed
STATE
• The collection of an object’s property values determine its state
• The object’s state can affect its behavior
VIN: 323422344
VIN: 323422345
VIN: 323422346
Class: Car
Objects:
CLASS
• A category of objects – “abstraction” • Template for the object, also called “factory”• Allows multiple objects to have common
methods and properties • An object is an “instance” of a class
Object: CarProperties: Color GasolineLevel
Make OilLevel Model Speed
YearMethods: Stop Turn Left Start Turn Right Accelerate
Interface
INTERFACE• The set of public methods and properties
which are available to access or manipulate the object
• An object may have many interfaces
window door
roomA blueprint is the specification of aHouse. This blueprint specifies a house with room, door, and window.
CLASS : elaborate
class
object
A class is the specification from which a developer generates objects
A blueprint is the specification from which a builder generates houses
ENCAPSULATION
• Hides the internal workings of an object from the rest of the application.
• You may change Implementation while interface remains constant
• In Java properties and methods are used to get information and perform actions in an application that are hidden within classes:
you do not know how they are implemented , but can use then as “black box”
INHERITANCE: Very Important
• Ability to reuse functionality in a class.• A base class defines the common
functionality: Subclasses inherit that functionality
and may add new functionality– i.e.: A general class of Sport Car is used to
create sub-classes of Camaro and FireBird. They have many similar (re-used) features but are different cars, because they have other properties (methods) different.
Java Programming, Fifth Edition
17
Learning About Java
• Java– Developed by Sun Microsystems – Object-oriented language – Advantages :
• Simple, Distributed , Interpreted, Secure• Architecture neutral, Portable, Multithreaded
– Can be run on wide variety of computers – Runs on hypothetical computer known as
Java virtual machine (JVM)
Java Programming, Fifth Edition
18
Learning About Java (continued)
• Java (continued) – Can be run on wide variety of computers – Does not execute instructions on
computer directly– Runs on hypothetical computer known
as Java virtual machine (JVM)
• Source code – Programming statements written in high-
level programming language
The Java Virtual Machine – special “basic computer”
Java is platform independent. What this means is that a program written inJava should be executable in any computer with any chipset.
This is done by means of the Java Virtual Machine (JVM). The JVM interacts witha computer through bytecode.
Computer JVM Program
Java Programming, Fifth Edition
20
Java Program Types
• Applets– Programs embedded in Web page
• Java applications– Called Java stand-alone programs– Console applications
• Support character output
– GUI based applications• Menus
• Toolbars
• Dialog boxes
Java Programming Process:
• Write a specification for the program (what the program is supposed to do)
• Design the program • Choose algorithms and decide how data will be
stored , develop pseudocode and/or flowchart• Write the program (Java language, indeed!)• Compile the program (to lower-level language)• Execute the program• Debug the program• Maintenan the program in a workable condition
Java Programming, Fifth Edition
22
Analyzing a Java Application That Uses Console Output
• Even simplest Java application – Involves fair amount of confusing syntax
• Goal of the first application :– Print “First Java application” on screen
Java Programming, Fifth Edition
23
Analyzing a Java Application That Uses Console Output :
Java Programming, Fifth Edition
24
Understanding the First Class
• Everything used within Java program must be part of a class
• Define Java class using any name or identifier
• Requirements for identifiers– Must begin with:
• Letter of English alphabet• Or non-English letter (such as α or π)
– Cannot begin with digit
Java Programming, Fifth Edition
25
Understanding the First Class
• Requirements for identifiers– Can only contain:
• Letters
• Digits
• Underscores
• Dollar signs
– Cannot be Java reserved keyword– Cannot be true, false, or null
• Access modifier– Defines how class can be accessed
Java Programming, Fifth Edition
26
Understanding the First Class
Understanding the StatementThat Prints the Output
Java Programming, Fifth Edition
27
Some Illigal in Java Class names
Java Programming, Fifth Edition
28
Understanding the First Class (continued)
Java Programming, Fifth Edition
29
Java Programming, Fifth Edition
30
Understanding the main() Method• static
– Means method accessible and usable • Even though no objects of class exist
• void – Use in main() method header– Indicates main() method does not return
value when called– Doesn’t mean main() doesn’t produce
output
Another Java Program: Example.java/*/* This is a simple Java program.This is a simple Java program.
Call this file Example.javaCall this file Example.java*/*/class Example class Example {{ //A Java program begins with a call to main().//A Java program begins with a call to main().
public static void main(String[] args) public static void main(String[] args) {{System.out.println("Java drives the Web."); System.out.println("Java drives the Web."); // System.out : console output// System.out : console output} // println() displays the string} // println() displays the string
} // passed to it } // passed to it
Here: Java is case sensitive language!!
• a source file : the same name as class with .java extension
• comments(ignored by the compiler): single line and multiline
• a subroutine is called a method; main() method is the entry point
• public: access specifier ; static – can be called before an object is
created from a class ;
void - does not return a value
Change .println to Println and compile
Shell Code – create and use as a template for any class
Java Programming, Fifth Edition
32
Do not forget to same in the file AnyClassName.java
Java Programming, Fifth Edition
33
Adding Comments to a Java Class• Types of Java comments
– Line comments • Start with two forward slashes (//) • Continue to end of current line • Do not require ending symbol
– Block comments • Start with forward slash and asterisk (/*)• End with asterisk and forward slash (*/)
Java Programming, Fifth Edition
34
Saving, Compiling, and Runningand Modifying a Java Application• Saving a Java class
– Save class in file with exactly same name and.java extension
• For public classes• Class name and filename must match exactly
• Compiling a Java class– Compile source code into bytecode– Translate bytecode into executable statements
• Using Java interpreter– Type javac First.java
Java Programming, Fifth Edition
35
Running a Java Application
• Run application from command line– Type java First
• Shows application’s output in command window
• Class stored in folder named Java on C drive
Java Programming, Fifth Edition
36
Running a Java Application
In order javac and java programs be able to find First.java and First.class files - set up Environmental Path variable
Modifying a Java Class
• Modify text file that contains existing class
• Save file with changes – Using same filename
• Compile class with javac command• Interpret class bytecode and execute
class using java command
Java Programming, Fifth Edition
37
Java Programming, Fifth Edition
38
Creating a Java ApplicationUsing GUI Output• JOptionPane
– Produce dialog boxes
• Dialog box – GUI object resembling window– Messages placed for display
• Package– Group of classes
• import statement– Use to access built-in Java class
Java Programming, Fifth Edition
39
Creating a Java ApplicationUsing GUI Output
Java Programming, Fifth Edition
40
Correcting Errors
• First line of error message displays:– Name of file where error found– Line number– Nature of error
• Next lines identify:– Symbol – Location
• Compile-time error– Compiler detects violation of language rules – Refuses to translate class to machine code
Do not expect miracles : an error message will only approximately indicate where an error is and what it could be – no guarantee that these are 100% correct
Java Programming, Fifth Edition
41
Correcting Errors andFinding Help (continued)• Parsing
– Process compiler uses to divide source code into meaningful portions
• Logic error– Syntax correct but produces incorrect
results when executed– Usually more difficult to find and resolve
• Java API – Also called the Java class library– Prewritten Java classes
Java Programming, Fifth Edition
42
You Do It
• Your first application• Adding comments to a class• Modifying a class• Creating a dialog box
Don’t Do It
• File’s name must match name of class• Don’t confuse names parentheses, braces,
brackets, curly braces, square brackets, and angle brackets
• Don’t forget to end a block comment• Don’t forget that Java is case sensitive• End every statement with semicolon
– Do not end class or method headers with semicolon
• Recompile when making changes
Java Programming, Fifth Edition
43
Java Programming, Fifth Edition
44
Summary
• Computer program – Set of instructions that tells a computer what to
do• Object-oriented programs
– Classes– Objects
• Java virtual machine (JVM)– Standardized hypothetical computer
• Everything in a Java program must be part of a class
Java Programming, Fifth Edition
45
Summary (continued)
• Access modifier– Word that defines circumstances under which
class can be accessed• All Java applications must have method named main()
• Program comments – Nonexecuting statements – Add to file for documentation
• javac– Compile command
Java Programming, Fifth Edition
46
Summary (continued)
• java– Execute command
• JOptionPane– GUI– Provides methods for creating dialogs
Quiz• True or False: Object-oriented programming is a style of programming
in which sets of operations are executed one after another in sequence.
• Answer: False
• Writing ____ programs involves creating classes, creating objects from those classes, and creating applications.
• Answer: object-oriented Answer: False
• A(n) ____ of a class is an existing object of a class.
• Answer: instance
• Programming statements written in a high-level programming language are called ____.
• Answer: source code47
Quiz (cont.)
• Stand-alone programs are called Java ____.
• Answer: applications
• Answer: True
• True or False: Not all classes have a main() method.
• Answer: True
• Line comments start with ____.
• Answer: two forward slashes (//)
48
Quiz (cont.)
• True or False: In Java, if a class is public (that is, if you use the public access modifier before the class name), you must save the class in a file with exactly the same name and a .class extension.
• Answer: False and *=
• To compile a file named First.java, you type ____ First.java and then press Enter.
• Answer: javac
• To run the First application from the command line, you type ____ First.
• Answer: java49
Quiz (cont.)• A(n) ____ is a GUI object resembling a window in
which you can place messages you want to display.
• Answer: dialog box
50
This is the end of our Unit 2 Seminar
•Any questions ?
51