DON’T PANIC!! Lots of new notions coming in these slides

Slides:



Advertisements
Similar presentations
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
Advertisements

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
©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. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Getting Started with Java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Lecture 1 Introduction to Computers and Object-
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 1 Introduction to Object-Oriented Programming.
Chapter Four Defining Your Own Classes continued.
Introduction to Methods
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 1 Introduction to Object-Oriented Programming and.
Writing Classes (Chapter 4)
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.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
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.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
User-Written Functions
Programming Logic and Design Seventh Edition
Some Eclipse shortcuts
Java Primer 1: Types, Classes and Operators
2.5 Another Java Application: Adding Integers
Primitive and Reference Data Values
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Programming Language Concepts (CIS 635)
Programmazione I a.a. 2017/2018.
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.
Chapter 4 Defining Instantiable Classes
Primitive and Reference Data Values
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Chapter 2 Java Programming Basics
Defining Classes and Methods
Defining Your Own Classes
Chapter 6 Methods: A Deeper Look
Chapter 1: Computer Systems
CMSC 202 Java Primer 2.
Programs and Classes A program is made up from classes
Chapter 2: Introduction to C++.
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
In this class, we will cover:
Classes, Objects and Methods
Getting Started with Java
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Intro to OOP with Java, C. Thomas Wu
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

DON’T PANIC!! Lots of new notions coming in these slides Don’t worry if not all of it makes perfect sense We’ll meet most of this stuff again in detail later Do worry if none of it makes any sense You should get the general picture now Now brace yourself … stop me if you get confused … ask questions … throw money … The two most important concepts in object-oriented programming are the class and the object. In the broadest term, an object is a thing, both tangible and intangible, which we can imagine. A program written in object-oriented style will consist of interacting objects. For a program to maintain bank accounts for a bank, we may have many Account, Customer, Transaction, and ATM objects. An object is comprised of data and operations that manipulate these data.

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 parts define what objects in the class look like. Every class is automatically in existence when the program runs. The two most important concepts in object-oriented programming are the class and the object. In the broadest term, an object is a thing, both tangible and intangible, which we can imagine. A program written in object-oriented style will consist of interacting objects. For a program to maintain bank accounts for a bank, we may have many Account, Customer, Transaction, and ATM objects. An object is comprised of data and operations that manipulate these data.

Classes and Objects An object is an instance of a class, and is created using the new operator. The non-static part of the class defines what each object looks like. Many instances (objects) can be created from a class … no limit except reality An object contains information and functionality of a “thing”, e.g., Account, Vehicle, Employee, etc. The two most important concepts in object-oriented programming are the class and the object. In the broadest term, an object is a thing, both tangible and intangible, which we can imagine. A program written in object-oriented style will consist of interacting objects. For a program to maintain bank accounts for a bank, we may have many Account, Customer, Transaction, and ATM objects. An object is comprised of data and operations that manipulate these data.

Classes’ and Objects’ Components Classes (and thus also objects) are composed of methods and data values Data values store information Methods do things, and also have their own local data

Graphical Representation The class name appears on top of the icon. Account An icon for a class is the rectangle. The object’s name appears on top of the icon. SV129 Account The class name is placed inside the object icon. An icon for an object is the rounded rectangle.

Instance-of Relationship Employee Before you can create instances of a class, the class must be defined. Employee Bill Steve Andy The dotted line shows the instance-of relationship. The class name can be omitted since it is clear which class these objects belong to . This diagram shows three instances of the Employee class. Their names are Bill, Steve, and Andy. They are more commonly referred to as Employee objects. One key fact to remember: A class must be defined before you can create an instance (object) of the class.

Visibility Modifiers: public and private The modifiers public and private designate the accessibility of objects’ and class’ data values and methods If a component is declared private, nothing outside the class can access it. If a component is declared public, anything outside the class can access it.

In general, be private (military demotion) Make class components private whenever you can This supports the notion of encapsulation, which makes for more robust software development

Class and Instance Data Values A class data value (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances. An instance data value is used to maintain information specific to individual instances. Make instance data values private always Every object from the same class will have the same set of data values. The values themselves for individual objects would be different, of course. We assume here that the minimum balance is the same for all accounts. If this is the case, then the single value for minimum balance is shared by all Account objects. So we need to keep one value of the minimum balance that will be shared by all Account objects. Likewise, there’s exactly one value for the average balance so it is appropriate as a class data value.

Sample Data Values Account Account SV129 Account SV506 Account SV008 There is one copy of minimum balance for the whole class and shared by all instances. minimum balance 100.00 All three Account objects possess the same instance data value current balance. Account SV129 Account SV506 Account SV008 To see the significance of a class data value, consider the situation where the minimum balance is represented as an instance data value. Each of the three Account objects will have its own copy of minimum balance. Since there’s a single value for the minimum balance, these copies are redundant duplicates that waste memory space. More importantly, keeping duplicates in memory will complicate the processing. When we have to change the value for the minimum balance, for example, we have to locate all copies and update them. Imagine updating the duplicates copies of 1000 Account objects. This is a very time consuming and error-prone activity, which can be avoided easily by representing the data as a class data value. current balance 908.55 1304.98 354.00

Primitive and Reference Data Values byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed. Primitive variables contain values Reference variables point at objects

Variable and Constant Data Values There are two types of data values: Account SV129 Account A variable whose value can change over time. minimum balance 100.00 current balance 908.55 account prefix 6427 opening balance 100.00 A constant whose value must remain fixed over time. A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed. Constants are indicated by the final modifier Non-final public class data values will give you warts

Methods Methods have code (to do stuff) and data A method defined for a class is called a class method (indicated by the static modifier) and a method defined for an object is called an instance method.

Method Data = Local Variables A local variable is a variable that is declared within a method. Local variables are accessible only in the method in which they are declared. The final and static modifiers do useful things to local variables - more about this later

Messages To instruct a class or an object to do something, we a message to one of its methods Values passed to a method when sending a message are called arguments or parameters of the message. The (formal) parameters of a method are local variables that receive the message parameters Methods can return one data value to the calling method

Sending a Message chk-008 Account deposit 250.00 deposit 250.00 Message deposit with the argument 250.00 is sent to chk-008. Account chk-008 deposit deposit 250.00 Message name is usually omitted in the diagram. deposit 250.00 Notice that the name of the message we send to an object or a class must be the same as the method’s name. Because they are the same, the message name is commonly omitted from the diagram as shown above. Because they are the same, the phrase “calling an object’s method” is synonymous to “sending a message to an object.”

Getting an Answer chk-008 Account getMonthlyFee monthly fee This message has no argument. Account chk-008 getMonthlyFee monthly fee The method returns the value monthly fee back to the message sender. The deposit method in the previous slide is an action-only method. It takes some action when called, but returns no answer back to the caller. The getMonthlyFee method above is a value-returning method. When called, it returns a value. A method can be both: executes an action and returns some value.

Calling a Class Method Account getAverageBalance average balance The average balance of all accounts is returned. Notice the shape of the class method. The class method getAverageBalance shown above returns the average balance of all Account objects. A method such as getAverageBalance that deals with collective information about the class instances is usually defined as a class method. General Idea: You define an instance method for a task that pertains to an individual instance and a class method for a task that pertains to multiple instances.

Program Components A Java file is composed of comments, import statements, and class declarations.

Program Component: Comment // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Comment A comment is any sequence of text that begins with the marker /* and terminates with another marker */. Another marker for a comment is double slashes //. This marker is used for a single-line comment. Any text between the double-slash marker and the end of a line is a comment. The third type of comment is called a javadoc comment. It is a specialized comment that can appear before the class declaration and other program elements yet to be described in the book. Javadoc comments begin with the /** marker and end with the */ marker. Although not required to run the program, comments are indispensable in writing easy-to- understand code.

Program Component: Import Statement // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Import Statement The import statement allows the program to use classes and their instances defined in the designated package. We develop object-oriented programs by using predefined classes whenever possible and defining our own classes when no suitable predefined classes are available. In Java, classes are grouped into packages. The Java compiler comes with many packages, and we supply one package called javabook with this textbook. We will explain the motivation behind using the javabook package in the next section. You can also put your own classes into a package so they can be used in other programs.

Import Statement Syntax and Semantics Package Name Name of the package that contains the classes we want to use. Class Name The name of the class we want to import. Use asterisks to import all classes. <package name> . <class name> ; e.g. javabook . InputBox; More Examples import javabook.*; import java.awt.image.ColorModel; import com.drcaffeine.galapagos.*; To use the MainWindow class in the javabook package, we write javabook.MainWindow; which we read as “javabook dot MainWindow.” This notation is called dot notation. Notice that the import statement is terminated by a semicolon. If you need to import more than one class from the same package, then instead of using an import statement for every class, you can import them all using asterisk notation: <package name> . * ;

Program Component: Class Declaration // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Class Declaration A Java program is composed of one or more classes, some of them are predefined classes, while others are defined by ourselves. In this sample program, there are two classes—MainWindow and MyFirstApplication. The MainWindow class is from the javabook package and the MyFirstApplication class is the class we define ourselves. To define a new class, we must declare it in the program. The word class is a reserved word used to mark the beginning of a class declaration. We can use any valid identifier to name the class. One of the classes in a program must be designated as the main class. The main class of the sample program is MyFirstApplication. In the designated main class, then we must define a method called main, because when a Java program is executed, the main method of a main class is executed first.

Program Component: Method Declaration // Program MyFirstApplication /* This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Method Declaration

Method Declaration Elements Modifier Return Type Method Name Parameter Method Body public static void main ( String[ ] args ) { MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } The syntax for method declaration is <modifiers> <return type> <method name> ( <parameters> ) { <method body> } where <modifiers> is a sequence of terms designating different kinds of methods, <return type> is the type of data value returned by a method, <method name> is the name of a method, <parameters> is a sequence of values passed to a method, and <method body> is a sequence of instructions. We will explain the meanings of modifies, return types, and parameters in detail gradually as we progress through the book.

Statements Method bodies contain statements Simple statements end with a ; Compound statements are enclosed in {}s public static void main (String[] args) { int someData = 0; if (someData == 27) { System.out.println(“Cosmic rays!”); someData = 0; } The syntax for method declaration is <modifiers> <return type> <method name> ( <parameters> ) { <method body> } where <modifiers> is a sequence of terms designating different kinds of methods, <return type> is the type of data value returned by a method, <method name> is the name of a method, <parameters> is a sequence of values passed to a method, and <method body> is a sequence of instructions. We will explain the meanings of modifies, return types, and parameters in detail gradually as we progress through the book.

Sample Method public double fromDollar( double dollar ) { Parameter Local Variables public double fromDollar( double dollar ) { double amount, fee; fee = exchangeRate - feeRate; amount = dollar * fee; return amount; }

Files and Classes A Java program file ends with .java There must be one public class per file It must have the same name as the file One public class (i.e., one file) must have the main method

Simple Java Programs Simple Java programs can be written in just the one file, containing One public class (with the main method) Other class methods and final data values as required Such programs do not create any objects, but simply run class methods (starting with the main method) and use primitive data.

Template for Simple Java Applications center of the screen, and the size of the window is almost as big as the screen. */ import javabook.*; public class MyFirstApplication { public static void main(String[ ] args) MainWindow mainWindow; mainWindow = new MainWindow(); mainWindow.setVisible( true ); } Comment Import Statements Class Name Comment: Use a comment to describe the program Import Statements: Include a sequence of import statements. Most of the early sample applications require only import javabook.*; Class Name: Give a descriptive name to the main class. Method Body: Include a sequence of instructions. Method Body

DON’T PANIC!! We’ll write some programs without creating objects, i.e., you’ll have to think about only classes (and their methods and data values) We’ll write some programs that create objects from pre-existing classes, i.e., you’ll use objects before you have write code to define them. Then we’ll write programs that define and create their own objects The two most important concepts in object-oriented programming are the class and the object. In the broadest term, an object is a thing, both tangible and intangible, which we can imagine. A program written in object-oriented style will consist of interacting objects. For a program to maintain bank accounts for a bank, we may have many Account, Customer, Transaction, and ATM objects. An object is comprised of data and operations that manipulate these data.