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.

Slides:



Advertisements
Similar presentations
Introduction to Java.
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Road Map Introduction to object oriented programming. Classes
©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
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Introduction to Methods
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
The Java Programming Language
Object Oriented Programming … and other things you need to program in java.
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
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.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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,
More about Java Classes Writing your own Java Classes More about constructors and creating objects.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
OOP Basics Classes & Methods (c) IDMS/SQL News
Object Oriented Programming Object and Classes Lecture 3 MBY.
Unified Modeling Language (UML)
Features of JAVA PLATFORM INDEPENDENT LANGUAGE JAVA RUNTIME ENVIRONMENT (JRE) JAVA VIRTUAL MACHINE (JVM) JAVA APP BYTE CODE JAVA RUNTIME ENVIRONMENT.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Java Programming: Guided Learning with Early Objects
Examples of Classes & Objects
Java Primer 1: Types, Classes and Operators
Methods Chapter 6.
Road Map Introduction to object oriented programming. Classes
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Corresponds with Chapter 7
Classes & Objects: Examples
Group Status Project Status.
Implementing Classes Chapter 3.
Take out a piece of paper and PEN.
Programs and Classes A program is made up from classes
Anatomy of a Java Program
Classes, Objects and Methods
Final and Abstract Classes
Presentation transcript:

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 of Object Oriented Programs

The program declares one class called DrawSquare, and the class includes one method called main. public class DrawSquare { public static void main(String[] args) {

From this main method, the DrawSquare class creates a SketchPad object named paper with an initial size of 300 by 300 pixels. SketchPad paper; paper = new SketchPad(300, 300);

Another object called pencil is created using the DrawingTool class and named pencil with a drawing area represented by the paper object. A sequence of messages to place and move the pencil object are sent to draw the square. DrawingTool pencil; pencil = new DrawingTool(paper); pencil.forward(100); pencil.turnLeft(90);......

/* Programming languages allow the inclusion of comments that are not part of the actual code. Documentation is an important part of writing programs as it helps the reader to understand what is going on. Java allows for two styles of comments: */ Documentation /* This style will allow you to write longer documentation notes as the text can wrap around to succeeding lines. */ // This style automatically ends at the end of the line.

Programmers try to avoid "reinventing the wheel" by using predefined libraries of code. In Java, these predefined classes are grouped into packages. **The import statement allows the program to use predefined classes. Import Statement

Java comes with many packages, such as apcslib. In our example program, the DrawingTool and SketchPad classes are imported from the apcslib package with the statement //importing the library apcslib import apcslib.*;

A Java program is composed of one or more classes. The syntax for declaring a class is: class class_name { class_member_declarations; }

The word class is a Java reserved word, which means it is not available for programmers to use. It is used to mark the beginning of a class declaration. class class_name

class_name is the name of the class. Any valid identifier can be used to name the class. class class_name

class_member_declarations is a sequence of either data values or methods. class class_name { class_member_declarations; }

Every Java program consists of a main method. A method has the following general syntax: modifiers return_type method_name ( parameters ) { method_body } Methods

The modifiers refer to a sequence of terms designating different kinds of methods. modifiers return_type method_name ( parameters )

The return_type refers to the type of data a method returns. The data type can be one of the predefined types ( int, double, char, void ) or a user-defined type. modifiers return_type method_name ( parameters )

The method_name is the name of the method. In our program DrawSquare.jav, the name of the method is main. modifiers return_type method_name ( parameters )

The parameters list will allow us to send values to a method. modifiers return_type method_name ( parameters )

{ method_body } The method_body contains statements to accomplish the work of the method.

Every object in a program must be declared. An object declaration designates the name of an object and the class to which the object belongs. Its syntax is: class_name object_name a. class_name is the name of the class to which these objects belong. b. object_name is a sequence of object names separated by commas.

DrawingTool pencil; Account checking; Customer Bob, Betty, Bill; Examples class_name object_name;

No objects are actually created by the declaration. An object declaration simply declares the name (identifier) that we use to refer to an object. An object is created by using the new operation. The syntax for new is object_name = new class_name(arguments);

object_name is the name of the declared object. class_name is the name of the class to which the object belongs. arguments is a sequence of zero or more values passed to the new operation.

In the DrawSquare example, the paper object is created (instantiated) with the statement paper = new SketchPad(300, 300);

After the object is created, we can start sending messages to it. The syntax for sending a message to an object is: object_name.method_name(arguments);

object_name is the name of the declared object.

object_name.method_name(arguments); method_name is the name of a method of the object.

object_name.method_name(arguments); arguments is a sequence of zero or more values passed to the object.

DrawSquare example: The pencil object is sent a sequence of messages; forward with an argument of 100, and turnLeft with an argument of 90. pencil.forward(100); pencil.turnLeft(90);

A class diagram shows the key features of a class including:  the class name the class attributes the class methods Class Name Attributes Methods

A class consists of two groups of members: attributes (think of these as nouns) methods (think of these as verbs) DrawSquare SketchPad paper DrawingTool pencil SketchPad paper DrawingTool pencil DrawSquare() (Nouns) (Verbs) Class Name Attributes Methods

An attribute, often represented by an instance variable, names a single instance of an object’s state. A method is an operation that can be performed upon an object.

The only method in the DrawSquare class has the same name as the class. (DrawSquare()). This is a common occurrence in some programming languages. DrawSquare SketchPad paper DrawingTool pencil SketchPad paper DrawingTool pencil DrawSquare() (Nouns) (Verbs) Class Name Attributes Methods

The DrawSquare class also makes use of another class: DrawingTool (the class of the pencil object). DrawingTool (Attributes) > DrawingTool()... > void down() void forward(int d) void turnLeft(double degrees)... > DrawingTool()... > void down() void forward(int d) void turnLeft(double degrees)...

An object is very closely associated with the class to which it belongs. a) An object has attributes as defined by its class. b)An object’s behavior is restricted by the methods that are included in its class.

A class 1. is a template that defines attributes and methods. 2. is written by a programmer as a part of a program. 3. does not exist when programs execute, except in the form of one or more member objects. 4. its code cannot be altered during program execution. Classes –What are they?

Objects – what are they? An object 1. must belong to some class. 2. exists during the time that a program executes. 3. must be explicitly declared and constructed by the executing program. 4. has attributes that can change in value and methods that can execute during program execution. 5. is often referenced using a variable name.

Example Classes vs Objects ClassObject PeopleMrs_Hanna Michael_Phelps StudentsCollege_student Elementary_student Bank_AccountSavings_account Checking_account Vehiclecar truck CourseMath Computer science