Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.

Презентация:



Advertisements
Похожие презентации
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Advertisements

Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Object-Oriented Programming Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
1/27 Chapter 9: Template Functions And Template Classes.
1 © Luxoft Training 2012 Inner and anonymous classes.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
Ecology and fashion. Project was done by Borodina Ludmila from 10 B.
Unit II Constructor Cont… Destructor Default constructor.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
CONSTRAINTS 52. You do your CONSTRAINING in Sketcher mode to create your part to exacting dimensions. This is the opposite of free-form creating we have.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Simple Past vs. Present Perfect When do we use each tense in English?
HPC Pipelining Parallelism is achieved by starting to execute one instruction before the previous one is finished. The simplest kind overlaps the execution.
Транксрипт:

Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig

Objects An object has an interface, determined by its class. A class is an abstract data type, or user- defined type. Designing a class means defining its interface.

Built-In Types Think of an int … What is its interface? How do you send it messages? How do you make one? Where does it go when youre done with it?

Example Suppose Ive defined this class in Java: To make one, I type BottleOfJuice myJuice = new BottleOfJuice(); If I want myJuice opened, I say myJuice.open(); Bottle OfJuice

But Why Not Just… This is legal, but just makes a reference variable named myJuice This variable can refer to any BottleOfJuice object, but currently refers to nothing The operator new actually causes an object to be created, so we tell it what kind we want BottleOfJuice myJuice;

Designers Design, Users Use The interface is the critical part, but the details (implementation) are important too. Users use the interface (the public part); the implementation is hidden by access control. Bottle OfJuice

Objects vs. Procedural Libraries C libraries are like this, sort of: The library designer invents a useful struct. Then she provides some useful functions for it. The user creates an instance of the struct, then applies library functions to it. One big difference is that anyone can change any part of the struct. Booo, hsss! Another difference is in initialization.

Two Ways of Reusing Classes Composition: One class has another as a part (indicated by the diamond aggregation symbol). BottleOfJuice CaseOfJuice Shop

Two Ways of Reusing Classes Inheritance: One class is a specialized version of another (indicated by the triangle inheritance symbol). BottleOfJuice BottleOfOrangeJuice

Polymorphism Different subclasses respond to the same message, possibly with different actions. +JuicePlease:Polite +JuicePlease:Rude+JuicePlease:InGerman

Some Java Code Patron p1 = new Patron(); Patron p2 = new YankPatron(); Patron p3 = new BritPatron(); Patron p4 = new GermanPatron(); p1.JuicePlease() // polite request p2. JuicePlease() // rude request p3. JuicePlease() // polite request p4. JuicePlease() // request in German (but polite) Dont forget: Java is a case-sensitive language

Creating Objects We usually assume this is free; with built-in types like int or char, we just say int i; char c; With user-defined types (the ones we make), we need to be explicit about what we want: constructor function This is a very important issue!

Destroying Objects If an object goes out of scope, it can no longer be used (its name is no longer known). In C++, we might need to write an explicit function to free memory allocated to an object. Java uses references and garbage collection.

Example of Object Scope What happens to lect ? The LectureNotes object still exists, but the reference lect disappears (its out of scope after return). Eventually, the garbage collector removes the actual LectureNotes object. public String getTitle(int lectureNumber) { LectureNotes lect; lect = syllabus.getLecture(lectureNumber); String s = lect.getLine(1); return s; }

A Simple Model of Memory The reference variable lect holds a memory location 0x123422C++ Versus Java\n… The LectureNotes object is at that location When you speak of the variable lect, you are referring to the actual LectureNotes object. When lect goes out of scope it is automatically destroyed. The LectureNotes object lives on, but nobody can use it…unless there are other references to it memory

Javas Use of Memory Stack Heap Static variables Constants Non-RAM storage

Javas Primitive Types TypeSizeWrapper type boolean-Boolean char16-bitCharacter byte8-bitByte short16-bitShort int32-bitInteger long64-bitLong float32-bitFloat double64-bitDouble void-Void

Wrapper Types Variables of primitive types are automatic, i.e., they are stored on the stack. They are automatically deleted when they go out of scope. What if you want an object holding a primitive type? Example: char c = x; Character C = new Character(x); Java characters are in Unicode, which is a 16-bit encoding.

Really Big Numbers BigInteger, BigDecimal These are arbitrary precision, as big as they need to be. You cant use the usual operators (+-*/) since they are objects. But there are methods (functions) to do these things.

Creating New Types class MyNewType { // definition here } Now its legal to say MyNewType m = new MyNewType();

Class Members Fields (a.k.a. member variables, data members) Methods (a.k.a. member functions) class MyClass { int a; YourClass b; float memberFunction(int x, float f) { return 0; }

Lets Write Something // Our first program. File: HelloDate.java // Note that the file name is exactly the same // as the class name, including capitalization. import java.util.*; public class HelloDate { public static void main(String[] args) { System.out.println(Hello, it is ); System.out.println(new Date()); }

The Major Issues Editing Use any text editor you like (not a word processor!); save as HelloDate.java Compiling From a DOS or UNIX command line, type > javac HelloDate.java This should produce the file HelloDate.class Running Again from the command prompt, type > java HelloDate Output: Hello, it is: