The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,

Slides:



Advertisements
Similar presentations
Chapter 1: Computer Systems
Advertisements

1 CS1001 Lecture Overview Java Programming Java Programming.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
Outline Java program structure Basic program elements
Chapter 2: Introduction to C++.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Language and SW Dev’t
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 CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Objects and Classes Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary.
Style Guidelines. Why do we need style?  Good programming style helps promote the readability, clarity and comprehensibility of your code.
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
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Introduction to programming in the Java programming language.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Java Classes, Objects, and Events: A Preview JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria.
Data Types, Variables, and Arithmetic JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Data Types, Variables, and Arithmetic Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
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.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Working with Java.
Crash course in the Java Programming Language
Chapter 1 Introduction to Computers, Programs, and Java
Java Methods /** * Chapter 5 */ Java Syntax and Style
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
University of Central Florida COP 3330 Object Oriented Programming
Statements, Comments & Simple Arithmetic
Programming Vocabulary.
2.1 Parts of a C++ Program.
Introduction to Java Programming
Chapter 1: Computer Systems
MSIS 655 Advanced Business Applications Programming
Java Methods /** * Chapter 5 */ Java Syntax and Style A & AB
elementary programming
Documentation and Style
Anatomy of a Java Program
Focus of the Course Object-Oriented Software Development
Chapter 2: Introduction to C++.
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. TM

4-2 Classes and Source Files l A class defines a class of objects. Class name:File name: SomeClass Ramblecs FallingCube SomeClass.java Ramblecs.java FallingCube.java Convention: a class name starts with a capital letter Same upper / lower case letters

4-3 Components of a Class A class consists of: l Fields l Constructors l Methods

4-4 public class SomeClass { l Fields l Constructors l Methods } private: visible only inside this class public: visible in other classes Attributes / variables that define the object’s state. Can hold numbers, characters, strings, other objects. Usually private. Code for constructing a new object and initializing its fields. Usually public. Actions that an object can take. Can be public or private.

4-5 Summer Focus Our focus this summer will be on creating basic Java applications that involve methods. For Labs 1-3, you will create applications that have only a main method. For Labs 4-8, you will create applications will involve multiple methods. You will write applications and classes that involve fields and constructors during the school year.

4-6 All Java applications have a class containing a main method: public class SomeApp { public static void main(String [ ] args) { // program begins by executing // code in main method } }

4-7 Syntax vs. Style l Syntax is part of the language. The compiler checks it. l Style is a convention widely adopted by software professionals. l The main purpose of style is to improve the readability of programs.

4-8 Comments l Comments are notes in plain English inserted in the source code. l Comments are used to: –document the program’s purpose, author, revision history, copyright notices, etc. –describe fields, constructors, and methods –explain obscure or unusual places in the code –temporarily “comment out” fragments of code

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

4-10 Reserved Words l In Java a number of words are reserved for a special purpose. l Reserved words use only lowercase letters. l Reserved words include: –primitive data types: int, double, char, boolean, etc. –storage modifiers: public, private, static, final, etc. –control statements: if, else, switch, while, for, etc. –built-in constants: true, false, null l There are about 50 reserved words total.

4-11 Programmer-Defined Names l In addition to reserved words, Java uses standard names for library packages and classes: String, Graphics, javax.swing, JApplet, JButton, ActionListener, java.awt l The programmer gives names to his or her classes, methods, fields, and variables.

4-12 Names (cont’d) l Syntax: A name can include: –upper- and lowercase letters –digits –underscore characters l Syntax: A name cannot begin with a digit. l Style: Names should be descriptive to improve readability.

4-13 Names (cont’d) l Programmers follow strict style conventions. l Style: Names of classes begin with an uppercase letter, subsequent words are capitalized: public class FallingCube l Style: Names of methods, fields, and variables begin with a lowercase letter, subsequent words are capitalized. private final int delay = 30; public void dropCube()

4-14 Names (cont’d) l Method names often sound like verbs: setBackground, getText, dropCube, start l Field names often sound like nouns: cube, delay, button, whiteboard l Constants sometimes use all caps: PI, CUBESIZE l It is OK to use standard short names for temporary “throwaway” variables: i, k, x, y, str

4-15 Syntax l The compiler catches syntax errors and generates error messages. l Text in comments and literal strings within double quotes are excluded from syntax checking. l Before compiling, carefully read your code a couple of times to check for syntax and logic errors.

4-16 Syntax (cont’d) l Pay attention to and check for: –matching braces { }, parentheses ( ), and brackets [ ] –missing and extraneous semicolons –correct symbols for operators +, -, =, <, <=, ==, ++, &&, etc. –correct spelling of reserved words, library names and programmer-defined names, including case

4-17 Syntax (cont’d) l Common syntax errors: Missing closing brace Public static int abs (int x) { If (x < 0); { x = - x } return x; public static int sign (int x)... Extraneous semicolon Spelling (p  P, if  If) Missing semicolon

4-18 Style l Arrange code on separate lines; insert blank lines between fragments of code. l Use comments. l Use meaningful variable names. l Indent blocks within braces.

4-19 Style (cont’d) public boolean moveDown(){if (cubeY<6*cubeX) {cubeY+=yStep; return true;}else return false;} public boolean moveDown() { if (cubeY < 6 * cubeX) { cubeY += yStep; return true; } else { return false; } Before:After: Both compile fine! In fact, they are identical to the compiler!