Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
Animation Mrs. C. Furman. Animation  We can animate our crab by switching the image between two pictures.  crab.png and crab2.png.
Chapter 1: Introduction
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Getting Started with Java
Modules, Hierarchy Charts, and Documentation
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
© 2007 Lawrenceville Press Slide 1 Chapter 7 Top-Down Development  Problem-solving approach  Breaking a task down into smaller subtasks  First level.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Writing Classes (Chapter 4)
Introduction to Programming Writing Java Beginning Java Programs.
Java: Chapter 1 Computer Systems Computer Programming II.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Java Language and SW Dev’t
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
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.
JavaDoc1 JavaDoc DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY July 24, 2006 by Emil Vassev & Joey Paquet revision 1.2 –
The Java Programming Language
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Classes CS 21a: Introduction to Computing I First Semester,
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Vladimir Misic: Java1 Basic Java Syntax The java language will be described by working through its features: –Variable types and expressions.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
Introduction to Programming Writing Java Beginning Java Programs.
Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
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,
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Adding and Eating Worms Mrs. C. Furman August 23, 2010.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
SUMMARY OF CHAPTER 2: JAVA FUNDAMENTS STARTING OUT WITH JAVA: OBJECTS Parts of a Java Program.
Advanced Programing practices
More Sophisticated Behavior
Working with Java.
Chapter 3 GC 101 Java Fundamentals.
Yanal Alahmad Java Workshop Yanal Alahmad
Class Definitions and Writing Methods
More About Objects and Methods
Chapter 7 Top-Down Development
Chapter 4 Defining Instantiable Classes
Using variables, for..loop and functions to help organize your code
Chapter 2 Java Programming Basics
Defining Your Own Classes
Chapter 4 void Functions
Chapter 1: Computer Systems
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Anatomy of a Java Program
Advanced Programing practices
Classes CS 21a: Introduction to Computing I
Classes, Objects and Methods
DON’T PANIC!! Lots of new notions coming in these slides
Getting Started with Java
Presentation transcript:

Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to document what a classes, variables, or methods are used for. –Upper and lower case letters are different in Java. ThisOne is not the same as thisOne. –Identifiers should not include punctuation and can not include spaces.

Identifiers –A good programming standard for identifiers composed of multiple words is to capitalize the first character of the additional words. The case of the first letter of the first word will depend upon the use of the identifier. For Example, myCar, or bobAndSusieAreFriends

Identifiers Class names begin with a capital letter. –This standard makes it easier to understand which identifiers represent classes. –In the MyFirstApplication we have two class names: MyFirstApplication and MainWindow

Identifiers Variable (object names) and method names begin with a lower case letter. –In MyFirstApplication we have one object: mainWindow Note that mainWindow is different from MainWindow. –In MyFirstApplication we have one method: main(…)

Creating a Java Program The first step to creating a Java program is to define a class containing the main method. –The MyFirstApplication program on page 39 of Wu is an example of a Java class containing a main method. This Java class definition can be thought of as the driver class or the ring leader class.

MyFirstApplication main(String[] args)

Declaring Objects In order to create objects to work within a Java program they must first be declared. –The object declaration includes the class name that the object belongs to and the variable name for the object. –The format for declaring an object is: ; –The class must be defined before you can declare the object.

Declaring Objects –Think about our cars. Cars was the class name and we had three instances of the this class. To declare each of the cars we had created we would type: Cars clintsPorsche; Cars jessicasBMW; Cars johnsLamborghini;

Declaring Objects The MyFirstApplication program in Wu defines one object called mainWindow of the class type MainWindow. MainWindow mainWindow; –Note the differences in the text above.

Declaring Objects Declaring an object only creates a name to refer to the object, it does not create the instance of that object. –In order to use the named object instance, the object must be instantiated (or created).

Object Instantiation Object instantiation (creation) actually creates a copy of the class and stores it in memory with the name defined in the object declaration. The format for object instantiation is: = new ( );

Object Instantiation = new ( ); The is the name for the object as you declared it in your program. The is the name of the class that this object has been declared to belong to. The represent possible data that may be required to create the object instance.

Object Instantiation We declared three types of Cars: Cars clintsPorsche; Cars jessicasBMW; Cars johnsLamborghini; To instantiate the instances of the Cars: clintsPorsche = new Car(); jessicasBMW = new Car(); johnsLamborghini = new Car();

Object Instantiation The MyFirstApplication instantiates the object mainWindow on line two of the main method: mainWindow = new MainWindow();

MyFirstApplication Main(String[] args) mainWindow MainWindow

Message Passing Once an object exists, the program can begin to send messages to the object. –The message must include the object to which the message is addressed, the name of the task to be completed, and any data required to complete the task. –In Java speak:. ( );

Message Passing Let’s create messages for our cars. –We need to know how much gas Clint has. clintsPorsche.getGasLevel(); –We want to turn on John’s hazards. johnsLamborghini.turnOnHazards();

Message Passing Now lets look at the last line of the main method in MyFirstApplication. mainWindow.setVisible(true);

Method Declarations The method declaration provides the function implementation in the program. –The basic format of a method declaration is: ( ) { }

Method Declarations ( ) { } –Modifiers represent terms that determine what kind of method is declared. (Chap 4) –The Return Type is the data type of the value returned by the method. If the method does not return a value this value is void.

Method Declarations ( ) { } –The method name should identify the task to be completed by the method –The parameters are the data that the method requires to complete the task. –The method body includes the code statements that implement the task.

Method Declarations Let’s look at the main method in MyFirstApplication.

Class Declarations Java programs are composed of one or more classes. –Some classes are already defined in Java but we also need to be able to define our own classes. class { }

Class Declarations class { } –Every class must have a unique name. –The class member declarations include method definitions and variable (data value).

Class Declarations One class in every program must include the main method. –This class is the driver class. –The main method is the first method that is called when a program is executed. Let’s look at the MyFirstApplication program.

Import Statement The import statement is used to allow a class definition to access predefined Java classes. –Related predefined Java classes are stored in packages. We will use the package called javabook provide by Wu. –In order to import predefined classes: import. ;

Import Statement import. ; –The package name represents the the particular package to look into for the class name. java.* or javabook.* –The class name can be a specific class or every class in the package. import javabook.MainWindow; import javabook.*;

Comments Providing a description of what the program does is called commenting the code. –Commenting is used to provide an overall description of the class and what it does, who the author is, when the class was created and modified, etc. – Header comments –Commenting is also used throughout your code to provide a description of a method, as well as to provide descriptions of complicated code. – The use of meaningful variable and method names helps “comment” the code.

Comments Java provides three ways to comment code. –MyFirstApplication includes two types. –The /* …. */ is used for multiple line comments. –The // is used for a single line comment. –The final type is javadoc.

Comments The javadoc comment appears before the class declaration. –The javadoc comment begins with /** rather than /*. The javadoc comments end with */ –The benefit of using a javadoc comment is that the programmer can use a tool that will automatically create a web page from the comment. This is only true for javadoc comments.

Comments Comments can not be put inside of another comment. Comments are for the programmer only, they do not represent executable code. –The computer ignores comments.

Commenting Rules of Thumb Do not state the obvious for self-evident information. For example if the code is: –x = x + y; // it is not necessary that you are adding x and y and storing the result in x. Provide a brief statement about each variable and a brief description for each method.

Commenting Rules of Thumb Do not comment code that is bad, if you are writing a long paragraph to describe what is going on, then re-write the code to make it simpler. Make sure your comments agree with the code. Use comments to Clarify not confuse the unsuspecting reader. Use them to help the reader.

Creating a Java Program The filename.class file is interpreted by the Java interpreter every time it is run. Editor Filename.javaFilename.class Java Compiler Java Interpreter Only Once Every Time