Presentation is loading. Please wait.

Presentation is loading. Please wait.

IT258 Foundation of Programming Using Java

Similar presentations


Presentation on theme: "IT258 Foundation of Programming Using Java"— Presentation transcript:

1 IT258 Foundation of Programming Using Java
Unit 2 Seminar : (Chapter 1 ) Instructor : Vladimir Gubanov, PhD

2 they will be each Thursday , from 9 PM to 10 PM EST
A reminder: Our Seminars : they will be each Thursday , from 9 PM to 10 PM EST My Office Hours : Mondays, 7PM to 8PM Saturdays, 9AM to 10 AM My

3 Java Programming, Fifth Edition
Chapter One: Creating Your First Java Classes

4 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 output 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
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 Java Programming, Fifth Edition

7 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

8 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

9 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 Method: Start Accelerate Turn Left Stop

10 STATE The collection of an object’s property values determine its state The object’s state can affect its behavior Object: Car Property: GasolineLevel OilLevel Speed State

11 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 Objects: VIN: Class: Car VIN: VIN:

12 INTERFACE The set of public methods and properties which are available to access or manipulate the object An object may have many interfaces Object: Car Properties: Color GasolineLevel Make OilLevel Model Speed Year Methods: Stop Turn Left Start Turn Right Accelerate Interface

13 room window door CLASS : elaborate
A blueprint is the specification of a House. This blueprint specifies a house with room, door, and window. room There are several means of explaining object-oriented concepts. Weber chapter 5 (assumes syntax understanding) Farrel the first few chapters (covers the concepts from a coding perspective and not so much a conceptual one) The particular faculty member might want to use his/her own methods/analogies for explaining object-oriented concepts and forgo all or parts of this explanation. window door

14 class object A class is the specification from which a developer
A blueprint is the specification from which a builder generates houses class object A class is the specification from which a developer generates objects Analogies seem to be the easiest way to explain object-oriented principles, but be very careful that the analogy is not misinterpreted.

15 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”

16 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.

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)
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 Java Programming, Fifth Edition

19 The Java Virtual Machine – special “basic computer”
Java is platform independent. What this means is that a program written in Java should be executable in any computer with any chipset. This is done by means of the Java Virtual Machine (JVM). The JVM interacts with a computer through bytecode. Computer JVM Program

20 Java Program Types Applets Java applications
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, Fifth Edition

21 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

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
Java Programming, Fifth Edition

27 Understanding the Statement That Prints the Output
Java Programming, Fifth Edition

28 Some Illigal in Java Class names
Java Programming, Fifth Edition

29 Understanding the First Class (continued)
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 Java Programming, Fifth Edition

31 Another Java Program: Example.java
/* This is a simple Java program. Call this file Example.java */ class Example { //A Java program begins with a call to main(). public static void main(String[] args) System.out.println("Java drives the Web."); // System.out : console output } // println() displays the string } // passed to it Change .println to Println and compile 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

32 Shell Code – create and use as a template for any class
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 Running and 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 Java Programming, Fifth Edition

37 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

38 Creating a Java Application Using 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 Application Using 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 and Finding 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 Java Programming, Fifth Edition

43 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

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 JOptionPane Execute command GUI
Provides methods for creating dialogs Java Programming, Fifth Edition

47 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 code

48 Quiz (cont.) Stand-alone programs are called Java ____.
Answer: applications Answer: True True or False: Not all classes have a main() method. Line comments start with ____. Answer: two forward slashes (//)

49 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: java

50 Quiz (cont.) A(n) ____ is a GUI object resembling a window in which you can place messages you want to display. Answer: dialog box

51 This is the end of our Unit 2 Seminar
Any questions ?


Download ppt "IT258 Foundation of Programming Using Java"

Similar presentations


Ads by Google