Anatomy of a Java Program

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 1: Computer Systems
IT151: Introduction to Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
LESSON 3 - Identifiers JAVA PROGRAMMING. Identifiers All the Java components —classes, variables, and methods— need names. In Java these names are called.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Excerpts from Introduction to Java Programming, 4E Author: Y. Daniel Liang (Copyright by Prentice Hall)
Introduction to Java Programming, 4E
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
S.W. Ma/CIM/LWL41211/2 Prog. IIA Page 1 HKIVE (Lee Wai Lee Campus) Department of CIM Course : Year 2 Module : Programming IIA Textbook : Introduction.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Comments are for people Header comments supply basic information about the artifact.
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.
Introduction to Java Programming. History F James Gosling and Sun Microsystems F Oak F Java, May 20, 1995, Sun World F HotJava –The first Java-enabled.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
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
Introduction to Java Programming with Forte Y. Daniel Liang.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Chapter 2: Java Fundamentals
Introduction To JAVA By Ihtesham Ul Haq. Course Objectives Upon completing the course, you will understand Upon completing the course, you will understand.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to programming in the Java programming language.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Introduction to Java Programming. 2 Chapter 1 Introduction to Java and Forte F What Is Java? F Getting Started With Java Programming –Create, Compile.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Introduction to Java Programming, 4E Y. Daniel Liang.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Working with Java.
Chapter 1 Introduction to Computers, Programs, and Java
GC101 Introduction to computer and program
Lecture 3: Operators, Expressions and Type Conversion
University of Central Florida COP 3330 Object Oriented Programming
Chapter 1 Introduction to Computers, Programs, and Java
Statements, Comments & Simple Arithmetic
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Java Programming
Chapter 3 Classes and Objects
Chapter 1: Computer Systems
elementary programming
Documentation and Style
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Anatomy of a Java Program Comments Reserved words Modifiers Statements Blocks Classes Methods The main method

a) Comments Comments ignored during program execution Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Comments start with: // Traditional comments: /* ... */

b) Reserved Words Words that have a specific meaning to the compiler Cannot be used for other purposes in the program. Key words examples are: Key words are lower case (Java is a case sensitive language). goto and const are C++ keywords, but not currently used in Java. If they appear in Java, Java compilers will produce error messages. boolean continue return public class static void int double private protected package

c) Modifiers Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used. Examples of modifiers are public and static. Other modifiers are private, final, abstract, and protected. A public datum, method, or class can be accessed by other programs. A private datum or method cannot be accessed by other programs.

d) Statements A statement represents an action or a sequence of actions. The statement System.out.println("Welcome to Java!") in the program is a statement to display the greeting "Welcome to Java!" Every statement in Java ends with a semicolon (;).

e) Blocks A pair of braces in a program forms a block that groups components of a program.

f) Classes The class is the essential Java construct. A class is a template or blueprint for objects. To program in Java, you must understand classes and be able to write and use them. A program is defined by using one or more classes.

g) Method System.out.println is a method. Method is a collection of statements that performs a sequence of operations to display a message on the console. It is used by invoking a statement with a string argument. The string argument is enclosed within parentheses. In this case, the argument is "Welcome to Java!" You can call the same println method with a different argument to print a different message.

h) main () Method public static void main(String[] args) { The main method provides the control of program flow. The Java interpreter executes the application by invoking the main method.   The main method looks like this: public static void main(String[] args) { // Statements; }

Programming Style and Documentation in Java Appropriate comments and comments style Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class, lecture’s name, date, and a brief description of your code at the beginning of the program.

Comments Style A “block” comment is placed between /* and */ marks: A single-line comment goes from // to the end of the line: /* Exercise 5-2 for Java Methods Author: Miss Brace Date: 3/5/2010 Rev. 1.0 */ weight *= 2.2046; // Convert to kilograms

Javadoc Comments (cont’d) /** indicates a javadoc comment /** * Returns total sales from all vendors; * sets <code>totalSales</code> * to 0. * * @return total amount of sales from all vendors */ Can use HTML tags Common style

b) Naming Conventions Choose meaningful and descriptive names. Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea.

Cont.. Class names: Constants: Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE

c) Proper Indentation and Spacing Indent two spaces. Spacing Use blank line to separate segments of the code.

d) Block Styles Use end-of-line style for braces.