Chapter 1: Creating Java Programs

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

The Web Warrior Guide to Web Design Technologies
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
MC697 Object-Oriented Programming Using Java. In this class, we will cover: How the class will be structured Difference between object-oriented programming.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter 1: Introduction
Object Orientated Programming
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Computers: Tools for an Information Age
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program C# Programming: From Problem Analysis to Program Design 2 nd Edition.
Introduction to Computing and Programming
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Getting Started with Java
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Introducing Java.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
IT258 Foundation of Programming Using Java
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Microsoft Visual Basic 2005: Reloaded Second Edition
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Object Oriented Systems Lecture 01 First Java Programming Jaeki Song.
1Object-Oriented Program Development Using C++ Computer Science and Programming Languages Computers are ubiquitous Computer literacy is essential Computer.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
1.  At the end of this slide, student can:  Explore tools, features, properties and interface of the Textpad.  Creating a new project.  Open and run.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
The Java Programming Language
1 JavaScript in Context. Server-Side Programming.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
Chapter 1: Introduction to Programs, and Java 1. Objectives To review programs (§ ). To understand the relationship between Java and the World Wide.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Creating a Java Application and Applet
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Overview of Programming.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use simple Output statements Understand the different types and uses of comments.
ITP 109 Week 2 Trina Gregory Introduction to Java.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Introduction to Algorithm. What is Algorithm? an algorithm is any well-defined computational procedure that takes some value, or set of values, as input.
C# Programming: From Problem Analysis to Program Design
GC101 Introduction to computer and program
Chapter 3: Using Methods, Classes, and Objects
Java programming lecture one
C# Programming: From Problem Analysis to Program Design
Computer Programming-1 CSC 111
Presentation transcript:

Chapter 1: Creating Java Programs

Objectives Define basic programming terminology Compare procedural and object-oriented programming Describe the features of the Java programming language Analyze a Java application that produces console output Java Programming, Seventh Edition

Objectives (cont’d.) Compile a Java class and correct syntax errors Run a Java application and correct logical errors Add comments to a Java class Create a Java application that produces GUI output Find help Java Programming, Seventh Edition

Learning Programming Terminology Computer program A set of written instructions that tells the computer what to do Machine language The most basic circuitry-level language A low-level programming language Java Programming, Seventh Edition

Learning Programming Terminology (cont’d.) High-level programming language Allows you to use a vocabulary of reasonable terms Syntax A specific set of rules for the language Program statements Similar to English sentences Commands to carry out program tasks Java Programming, Seventh Edition

Learning Programming Terminology (cont’d.) Compiler or interpreter Translates language statements into machine code Syntax error Misuse of language rules A misspelled programming language word Debugging Freeing program of all errors Logic errors Also called semantic errors Incorrect order or procedure The program may run but provide inaccurate output Java Programming, Seventh Edition

Comparing Procedural and Object-Oriented Programming Concepts Procedural programming Sets of operations executed in sequence Variables Named computer memory locations that hold values Procedures Individual operations grouped into logical units Object-oriented programs Create classes Blueprints for an object Create objects from classes Create applications Java Programming, Seventh Edition

Comparing Procedural and Object-Oriented Programming Concepts (cont’d Object-oriented programming was used most frequently for two major types of applications Computer simulations Graphical user interfaces (GUIs) Not all object-oriented programs are written to use a GUI Object-oriented programming differs from traditional procedural programming Basic concepts Polymorphism Inheritance Encapsulation Java Programming, Seventh Edition

Understanding Classes, Objects, and Encapsulation Describes objects with common properties A definition An instance Attributes Characteristics that define an object Differentiate objects of the same class The value of attributes is an object’s state Objects Specific, concrete instances of a class Java Programming, Seventh Edition

Understanding Classes, Objects, and Encapsulation (cont’d.) Java Programming, Seventh Edition

Understanding Classes, Objects, and Encapsulation (cont’d.) Method A self-contained block of program code that carries out an action Similar to a procedure Encapsulation Conceals internal values and methods from outside sources Provides security Keeps data and methods safe from inadvertent changes Java Programming, Seventh Edition

Understanding Inheritance and Polymorphism An important feature of object-oriented programs Classes share attributes and methods of existing classes but with more specific features Helps you understand real-world objects Polymorphism Means “many forms” Allows the same word to be interpreted correctly in different situations based on context Java Programming, Seventh Edition

Features of the Java Programming Language Developed by Sun Microsystems An object-oriented language General-purpose Advantages Security features Architecturally neutral Java Programming, Seventh Edition

Features of the Java Programming Language (cont’d.) Java (cont’d.) Can be run on a wide variety of computers Does not execute instructions on the computer directly Runs on a hypothetical computer known as a Java Virtual Machine (JVM) Source code Programming statements written in high-level programming language Java Programming, Seventh Edition

Features of the Java Programming Language (cont’d.) Development environment A set of tools used to write programs Bytecode Statements saved in a file A binary program into which the Java compiler converts source code Java interpreter Checks bytecode and communicates with the operating system Executes bytecode instructions line by line within the Java Virtual Machine Java Programming, Seventh Edition

Features of the Java Programming Language (cont’d.) Java Programming, Seventh Edition

Java Program Types Applets Java applications Programs embedded in a Web page Java applications Called Java stand-alone programs Console applications Support character output Windowed applications Menus Toolbars Dialog boxes Java Programming, Seventh Edition

Analyzing a Java Application that Produces Console Output Even the simplest Java application involves a fair amount of confusing syntax Print “First Java application” on the screen Java Programming, Seventh Edition

Analyzing a Java Application that Produces Console Output (cont’d.) Java Programming, Seventh Edition

Understanding the Statement that Produces the Output Literal string Will appear in output exactly as entered Written between double quotation marks Arguments Pieces of information passed to a method Method Requires information to perform its task System class Refers to the standard output device for a system Java Programming, Seventh Edition

Understanding the Statement that Produces the Output (cont’d.) Java Programming, Seventh Edition

Understanding the First Class Everything used within a Java program must be part of a class Define a Java class using any name or identifier Requirements for identifiers Must begin with one of the following: Letter of the English alphabet Non-English letter (such as α or π) Underscore Dollar sign Cannot begin with a digit Java Programming, Seventh Edition

Understanding the First Class (cont’d.) Requirements for identifiers (cont’d.) Can only contain: Letters Digits Underscores Dollar signs Cannot be a Java reserved keyword Cannot be true, false, or null Access specifier Defines how a class can be accessed Java Programming, Seventh Edition

Understanding the First Class (cont’d.) Java Programming, Seventh Edition

Understanding the First Class (cont’d.) Java Programming, Seventh Edition

Understanding the First Class (cont’d.) Java Programming, Seventh Edition

Understanding the First Class (cont’d.) Java Programming, Seventh Edition

Indent Style For every opening curly brace ( { ) in a Java program, there must be a corresponding closing curly brace ( } ) Placement of the opening and closing curly braces is not important to the compiler Java Programming, Seventh Edition

Understanding the main() Method static A reserved keyword Means the method is accessible and usable even though no objects of the class exist void Use in the main() method header Does not indicate the main() method is empty Indicates the main() method does not return a value when called Does not mean that main() doesn’t produce output Java Programming, Seventh Edition

Understanding the main() Method (cont’d.) Figure 1-7 The parts of a typical main() method Java Programming, Seventh Edition

Understanding the main() Method (cont’d.) Java Programming, Seventh Edition

Saving a Java Class Saving a Java class Save the class in a file with exactly the same name and .java extension For public classes, class name and filename must match exactly Java Programming, Seventh Edition

Compiling a Java Class and Correcting Syntax Errors Compile the source code into bytecode Translate the bytecode into executable statements Using a Java interpreter Type javac First.java Compilation outcomes javac is an unrecognized command Program language error messages No messages indicating successful completion Java Programming, Seventh Edition

Compiling a Java Class and Correcting Syntax Errors (cont’d.) Reasons for error messages Misspelled the command javac A misspelled filename Not within the correct subfolder or subdirectory on the command line Improper installation of Java Java Programming, Seventh Edition

Correcting Syntax Errors The first line of the error message displays: The name of the file where the error was found The line number The nature of the error Next lines identify: The symbol The location Compile-time error The compiler detects a violation of language rules Refuses to translate the class to machine code Parsing Used by the process compiler to divide source code into meaningful portions Java Programming, Seventh Edition

Running a Java Application and Correcting Logical Errors Run the application from the command line Type java First Shows the application’s output in the command window The class is stored in a folder named Java on the C drive Java Programming, Seventh Edition

Running a Java Application and Correcting Logical Errors (cont’d.) Figure 1-17 Output of the First application Java Programming, Seventh Edition

Modifying a Compiled Java Class Modify the text file that contains the existing class Save the file with changes using the same filename Compile the class with the javac command Interpret the class bytecode and execute the class using the java command Java Programming, Seventh Edition

Modifying a Compiled Java Class (cont’d.) Figure 1-18 First class containing output modified from the original version Java Programming, Seventh Edition

Correcting Logical Errors Logic error The syntax is correct but incorrect results were produced when executed Run-time error Not detected until execution Often difficult to find and resolve Java Programming, Seventh Edition

Adding Comments to a Java Class Program comments Nonexecuting statements added to a program for documentation Use to leave notes for yourself or others Include the author, date, and class’s name or function Comment out a statement Turn it into a comment The compiler does not translate, and the JVM does not execute its command Java Programming, Seventh Edition

Adding Comments to a Java Class (cont’d.) Types of Java comments Line comments Start with two forward slashes (//) Continue to the end of the current line Do not require an ending symbol Block comments Start with a forward slash and an asterisk (/*) End with an asterisk and a forward slash (*/) Java Programming, Seventh Edition

Adding Comments to a Java Class (cont’d.) Types of Java comments (cont’d.) Javadoc comments A special case of block comments Begin with a slash and two asterisks (/**) End with an asterisk and a forward slash (*/) Use to generate documentation Java Programming, Seventh Edition

Adding Comments to a Java Class (cont’d.) Figure 1-21 A program segment containing several comments Java Programming, Seventh Edition

Creating a Java Application that Produces GUI Output JOptionPane Produces dialog boxes Dialog box A GUI object resembling a window Messages placed for display import statement Use to access a built-in Java class Package A group of classes Java Programming, Seventh Edition

Creating a Java Application that Produces GUI Output (cont’d.) Figure 1-22 The parts of a typical FirstDialog class Java Programming, Seventh Edition

Creating a Java Application that Produces GUI Output (cont’d.) Figure 1-23 Output of the FirstDialog application Java Programming, Seventh Edition

Finding Help Java API FAQs on the Java Web site Also called the Java class library Provides prewritten information about Java classes FAQs on the Java Web site Java Development Kit (JDK) A software development kit (SDK) of programming tools Free to download Java Programming, Seventh Edition

You Do It Your First Application Compiling a Java Class Adding Comments to a Class Creating a Dialog Box Exploring the Java Web Site Java Programming, Seventh Edition

Don’t Do It Don’t forget the file’s name must match the class name Don’t confuse these terms: 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 Don’t forget to end every statement with a semicolon Do not end class or method headers with a semicolon Don’t forgot to recompile when making changes Java Programming, Seventh Edition

Summary Computer program Object-oriented programs A set of instructions that tells a computer what to do Object-oriented programs Classes Objects Applications Java Virtual Machine (JVM) A standardized hypothetical computer Everything in a Java program must be part of a class Java Programming, Seventh Edition

Summary (cont’d.) Access specifier A word that defines circumstances under which a class can be accessed All Java applications must have a method named main() Program comments Nonexecuting statements Add to a file for documentation javac A compile command Java Programming, Seventh Edition

Summary (cont’d.) java JOptionPane An execute command A GUI Provides methods for creating dialogs Java Programming, Seventh Edition