Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 2 Introduction to Java Applications Read D&D Ch 2 1 Programs • Program – a description of steps to perform, written with code • Program is written in a Programming language • A computer runs a program by executing the code 2 3 4 5 6 7 8 9 10 11 About Programming in Java • Java is an object-oriented language Different from C or COBOL • Proper use requires a different way of thinking about programming Think in terms of building objects that will work together Objects often model real-world entities or concepts 12 Object-Oriented Design A software design method that models the characteristics of abstract or real objects using classes and objects. 13 Models • A model is a simplified representation of something • Examples: – A model car: smaller, exterior looks like actual car, but many details missing – A street map: shows relative locations of streets at a small scale • Model serves some purpose 14 15 Models and Purpose • Model excludes details of the real entity • Which details are important is determined by purpose – Toy car Purpose: play Details: has body and wheels – Wind tunnel model Purpose: determine drag of car shape Details: must have exact shape of car 16 Objects • General view: an entity or concept • Examples: – Physical: car, tire, customer, instructor – Conceptual: Order, location, date, complex number 17 Java Objects • A component of a java program • Software objects are often used to model real-world objects you find in everyday life. • For example, a real-world bicycle could be represented as a software object in the program that controls an electronic exercise bike. 18 Behavior • Behavior of an object are tasks it can perform • For example the behavior of the real-world object, bicycle, are: – Braking – Accelerating – Slowing down – Changing gears 19 Implementation of the Behavior of A Software Object • A software object implements its behavior with methods. • A method is a function (subroutine) associated with an object. Definition: A software object is a bundle of variables and related methods. 20 Representation of A Software Object 21 Instance Methods • In addition to its variables, the software bicycle would also have methods to brake, change the pedal cadence, and change gears. • These methods are formally known as instance methods because they inspect or change the state of a particular bicycle instance 22 • In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an instance of the class of objects known as bicycles. 23 Classes • A Class is a conceptual grouping of similar objects with common behavior – Examples: Cars, Repair people, Purchase orders, Buildings • Object of class is an instance of class – Examples: My BMW 520, Ahmad Aziz, PO # A89765, Housing Bank Complex • In Java (and C++) a programmer writes classes that can be used to create objects 24 Predefined Classes • Java comes with libraries of classes • Programmers can build collections of classes that can be shared • It is possible to use these classes in many different programs 25 Referring to an Object • Reference – a phrase that refers to an object • Ex: “the seat in the front row” • Java example: system.out Name of an instance (or object) of the predefined PrintStream class This object represents the console of the computer – where text output is displayed 26 Messages to Objects • Message – a request for behavior (or action) from a particular object – Send the message to that particular object • Message to data projector from remote: – Receiving object: “projector” – Message: “ turn on lamp” – Java-esque : Projector.lamp(“on”); 27 Messages in Java • To send message need to identify – Reference to receiving object – Name of desired behavior (message “name”) – Message contents • Ex: print “hello!” to console – System.out .Println(“hello!”) ^^^^^^^^^^^^^^^^ message Add semicolon to make this a Java statement – like an English sentence 28 A Java Program Import java.io .*; Class Program1 { public static void main( String args[]){ System.out.Println(“hello!”); } } 29