Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "DON’T PANIC!! Lots of new notions coming in these slides"— Presentation transcript:

1 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.

2 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.

3 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.

4 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

5 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.

6 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.

7 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.

8 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

9 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.

10 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 354.00

11 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

12 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

13 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.

14 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

15 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

16 Sending a Message chk-008 Account deposit 250.00 deposit 250.00
Message deposit with the argument is sent to chk-008. Account chk-008 deposit deposit 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.”

17 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.

18 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.

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

20 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.

21 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.

22 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> . * ;

23 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.

24 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

25 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.

26 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.

27 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; }

28 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

29 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.

30 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

31 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.


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

Similar presentations


Ads by Google