3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

1 VBScript Session What we learn last session?
Final and Abstract Classes
Basic Java Constructs and Data Types – Nuts and Bolts
1 Packages: Putting Classes Together. 2 Introduction The main feature of OOP is its ability to support the reuse of code: Extending the classes (via inheritance)
1 Applets Programming Enabling Application Delivery Via the Web.
Classes and Objects in Java
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Chapter 1 The Study of Body Function Image PowerPoint
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 1 Embedded Computing.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Communicating in J2EE.
19 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Developing Web Services.
18 Copyright © 2005, Oracle. All rights reserved. Distributing Modular Applications: Introduction to Web Services.
12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Introducing the Java and Oracle Platforms Objectives 1-2 What Is Java? 1-3 Key Benefits of Java.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
8 Copyright © 2005, Oracle. All rights reserved. Object Life Cycle and Inner Classes.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
15 Copyright © 2005, Oracle. All rights reserved. Container-Managed Relationships (CMRs)
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
15 Copyright © 2005, Oracle. All rights reserved. Adding User Interface Components and Event Handling.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
17 Copyright © 2005, Oracle. All rights reserved. Deploying Applications by Using Java Web Start.
Programming Language Concepts
Lilian Blot 11 Spring 2014 TPOP 1. Lilian Blot 22 Spring 2014 TPOP 2.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
13 Copyright © 2005, Oracle. All rights reserved. Monitoring and Improving Performance.
Semantic Analysis and Symbol Tables
Understanding class definitions Looking inside classes 5.0.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Chapter 10: The Traditional Approach to Design
Systems Analysis and Design in a Changing World, Fifth Edition
Types of selection structures
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
PSSA Preparation.
Abstract Class, Packages and interface from Chapter 9
1 Abstract Class and Packages from Chapter 9 Lecture.
Introduction to Programming G51PRG University of Nottingham Revision 1
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
By Mr. Muhammad Pervez Akhtar
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Presentation transcript:

3 Copyright © 2005, Oracle. All rights reserved. Basic Java Syntax and Coding Conventions

3-2 Copyright © 2005, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Identify the key components of the Java language Identify the three top-level constructs in a Java program Identify and describe Java packages Describe basic language syntax and identify Java keywords Identify the basic constructs of a Java program Compile and run a Java application Examine the JavaBean architecture as an example of standard coding practices Use the CLASSPATH variable and understand its importance during compile and run time

3-3 Copyright © 2005, Oracle. All rights reserved. Notes Page

3-4 Copyright © 2005, Oracle. All rights reserved. Examining Toolkit Components The J2SE/J2EE from Sun provides: Compiler Bytecode interpreter Documentation generator J2SE

3-5 Copyright © 2005, Oracle. All rights reserved. Exploring Packages in J2SE/J2EE The J2SE/J2EE from Sun provides standard packages for: Language Windowing Input/output Network communication J2SE

3-6 Copyright © 2005, Oracle. All rights reserved. Documenting Using the J2SE The J2SE/J2EE from Sun provides documentation support for: Comments –Implementation –Documentation Documentation generator J2SE

3-7 Copyright © 2005, Oracle. All rights reserved. Contents of a Java Source A Java source file can contain three top-level constructs: –Only one package keyword followed by the package name, per file –Zero or more import statements followed by fully qualified class names or * qualified by a package name –One or more class or interface definitions followed by a name and block File name must have the same name as the public class or public interface.

3-8 Copyright © 2005, Oracle. All rights reserved. Establishing Naming Conventions Naming conventions include: Class names – Customer, RentalItem, InventoryItem File names – Customer.java, RentalItem.java Method names – getCustomerName(), setRentalItemPrice() Package names – oracle.xml.xsql, java.awt, java.io

3-9 Copyright © 2005, Oracle. All rights reserved. Notes Page

3-10 Copyright © 2005, Oracle. All rights reserved. More About Naming Conventions Variables: – customerName, customerCreditLimit Constants: – MIN_WIDTH, MAX_NUMBER_OF_ITEMS Uppercase and lowercase characters Numerics and special characters

3-11 Copyright © 2005, Oracle. All rights reserved. Notes Page

3-12 Copyright © 2005, Oracle. All rights reserved. Defining a Class Class definitions typically include: Access modifier Class keyword Instance fields Constructors Instance methods Class fields Class methods

3-13 Copyright © 2005, Oracle. All rights reserved. Rental Class: Example public class Rental { //Class variable static int lateFee; // Instance variables int rentalId; String rentalDate; float rentalAmountDue; … // Instance methods float getAmountDue (int rentId) { … } … } Declaration Instance variable Instance method Access modifier

3-14 Copyright © 2005, Oracle. All rights reserved. Notes Page

3-15 Copyright © 2005, Oracle. All rights reserved. Creating Code Blocks Enclose all class declarations. Enclose all method declarations. Group other related code segments. public class SayHello { public static void main(String[] args) { System.out.println("Hello world"); }

3-16 Copyright © 2005, Oracle. All rights reserved. Defining Java Methods Always define within a class. Specify: –Access modifier –Static keyword –Arguments –Return type [access-modifiers] [static] "return-type" "method-name" ([arguments]) { "java code block … } return

3-17 Copyright © 2005, Oracle. All rights reserved. Examples of a Method public float getAmountDue (String cust){ // method variables int numberOfDays; float due; float lateFee = 1.50F; String customerName; // method body numberOfDays = getOverDueDays(); due = numberOfDays * lateFee; customerName = getCustomerName(cust); return due; } Declaration Method variables Method statements Return

3-18 Copyright © 2005, Oracle. All rights reserved. Declaring Variables You can declare variables anywhere in a class block, and outside any method. You must declare variables before they are used inside a method. It is typical to declare variables at the beginning of a class block. The scope or visibility of variables is determined in the code block. You must initialize method variables before using them. Class and instance variables are automatically initialized.

3-19 Copyright © 2005, Oracle. All rights reserved. Examples of Variables in the Context of a Method public float getAmountDue (String cust) { float due = 0; int numberOfDays = 0; float lateFee = 1.50F; {int tempCount = 1; // new code block due = numberOfDays * lateFee; tempCount++; … } // end code block return due; } Method variables Temporary variables

3-20 Copyright © 2005, Oracle. All rights reserved. Rules for Creating Statements Use a semicolon to terminate statements. Define multiple statements within braces. Use braces for control statements.

3-21 Copyright © 2005, Oracle. All rights reserved. What Are JavaBeans? A JavaBean is a platform-neutral reusable software component that: Can be manipulated visually in a builder tool Communicates with other JavaBeans via events Comprises visible components that must inherit from other visible components Provides an architecture for constructing the building blocks of an application

3-22 Copyright © 2005, Oracle. All rights reserved. Managing Bean Properties Properties are the bean class member variables. (Variables can be primitive types or objects.) A property can be: –Unbound, which is a simple property –Bound, which triggers an event when the field is altered –Constrained, in which changes are accepted or vetoed by interested listener objects

3-23 Copyright © 2005, Oracle. All rights reserved. Exposing Properties and Methods Getter methods (public) Setter methods (public void) T getVar() T[] getArr() boolean isVar() setVar(T val) setArr(T[] val) setVar(boolean val) private T var; T[] arr;

3-24 Copyright © 2005, Oracle. All rights reserved. JavaBean Standards at Design Time The benefits at design time include: A facilitated interaction between designer, tool, and bean Instantiated and functioning beans in a visual tool Highly iterative development environment Building applications in small bits that plug in and out Storage and recovery of instantiated objects

3-25 Copyright © 2005, Oracle. All rights reserved. Compiling and Running a Java Application To compile a.java file: To execute a.class file: Remember that case matters. prompt> javac SayHello.java … compiler output … prompt> java SayHello Hello world prompt>

3-26 Copyright © 2005, Oracle. All rights reserved. The CLASSPATH Variable Is defined in the operating system Directs the JVM and Java applications where to find.class files References built-in libraries or user-defined libraries Enables interpreter to search paths, and loads built-in classes before user-defined classes Can be used with javac and java commands

3-27 Copyright © 2005, Oracle. All rights reserved. CLASSPATH : Example C:\>set CLASSPATH=D:labs\les03\classes\oe Setting CLASSPATH Location of.class files in the oe package

3-28 Copyright © 2005, Oracle. All rights reserved. Summary In this lesson, you should have learned the following: J2SE provides basic Java tools. J2SE provides a rich set of predefined classes and methods. Java programs are made up of classes, objects, and methods. Adhering to programming standards makes code easier to read and reuse.

3-29 Copyright © 2005, Oracle. All rights reserved. Practice 3: Overview This practice covers: Examining the Java environment Writing and running a simple Java application Examining the course solution application Inspecting classes, methods, and variables Creating class files and an application class with a main( ) method Compiling and running an application

3-30 Copyright © 2005, Oracle. All rights reserved. Notes Page for Practice 3

3-31 Copyright © 2005, Oracle. All rights reserved. Practice 3: Notes

3-32 Copyright © 2005, Oracle. All rights reserved. Practice 3: Notes

3-33 Copyright © 2005, Oracle. All rights reserved. Practice 3: Notes

3-34 Copyright © 2005, Oracle. All rights reserved.