Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex.

Similar presentations


Presentation on theme: "Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex."— Presentation transcript:

1 Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex Model: a simplified representation of something – It includes features that are considered important to the other – Neglects other features not considered important Characteristics of a model – Elements of the model represent other, more complex things – Model elements exhibit consistent behavior – Model elements can be grouped into different categories, depending on their behaviors – Actions external to the model element cause the behavior of the model element

2 Objects, behavior, and classes In Java programming, model elements are called objects Objects that share common behavior are grouped into classes Defining a class in Java is writing code that specifies how objects of the class behave or act – Once a class has been defined, objects of that class can be created – Every object belongs to exactly one class and is an instance of that class Predefined objects and classes – Java comes with some classes already defined – We will also use classes created by other programmers

3 Thinking about classes and objects Think of a library To model a library, what would the relevant classes and objects be?

4 Modeling a monitor A device upon which information can be displayed – For computers, monitors are devices to display information on (computers -> monitors) – For people, monitors are devices to read information from (monitors -> people)

5 Some terminology Java uses a references to identify an object Messages are sent to references, specifying behavior with supporting details Reference: any phrase that is used to refer to an object Messages: a request for some desired behavior from an object

6 Java monitor is predefined A monitor object is an instance of the PrintStream class (which is predefined) – Does not model all features of a monitor (color) – Used to display sequences of characters A predefined PrintStream object – System.out refers to the predefined PrintStream object – To display information on the monitor object, the Java language sends a message to the System.out reference println Specifies a desired bahavior (printing a line) Further details (the characters to be displayed) must also be sent

7 Sending a message To send a message we must specify the object and the behavior for that object In Java this means – A reference to the receiver object – A period – The message to be sent To display “Hello World” on a monitor: System.out.println(“Hello World”); referencemessage

8 A Java program A Java program needs – A name (called an identifier) Identifier: a sequence of letters, digits, or underscores The first character of an identifier must be a letter – Surround it with additional notation Download FirstProgram.zip demo

9 Mechanics of Java programs Java programs are actually contained in a class that has a main() method Java source files have the same name as the class they contain, with the.java extension – FirstProgram.java In CodeWarrior, we create projects that are the same name as the primary class – FirstProgram.mcp

10 Mechanics of Java Programs Source files ( FirstProgram.java ) need to be translated to a form that machines can understand These text files are compiled into Java Byte Code (an idealized machine language), which will be contained in a file with the same name as the text file but with the.class extendion ( FirstProgram.class ) – CodeWarrior actually bundles all these class files into one package called AppClasses The Java Byte Code is interpreted by the Java interpreter for the particular machine you are working with – Can be executed by any type of machine that has a Java interpreter – Relatively easy to imbed Java interpreters into other programs (web browsers)

11 Exercise Let us modify FirstProgram as an exercise

12 PrintStream messages Message println() : – Two versions println( ); println(empty); – In the first, the will be displayed. Next character to be displayed will start on the next line – In the second, nothing will displayed, and the next character to be displayed will appear on the next line – Example System.out.println(“Hello”); System.out.println(“World”); – Result Hello World

13 PrintStream messages Message print() – One versions print( ); – The will be displayed. Next character to be displayed will start on the same line, immediately after the last character in the – Example System.out.print(“Hello”); System.out.print(“World”); – Result HelloWorld – Next character to be printed will follow ‘d’

14 The String class (2.5) A Java predefined class that models a sequence of characters Any group of characters in double quotes is a String object – “Hello World” We have used these as arguments to println() and print() messages These are known as String Constants (cannot be changed)

15 Messages for String objects println() and print() are messages for PrintStream objects What messages can we send to String objects? In other words – What behavior does the String class provide? – What methods do the String class provide? A beginning list can be found on page of the text A complete listing can be found in the Java API (Application Programming Interface)Java API

16 String references A String constant is a String reference – Models precisely the character sequence – References can be used as arguments to messages In other words, sending an object to another object – Can also be used to send messages to objects they refer to Evoking the behavior of the object referred to

17 Example: toUpperCase() To send a message to the object, use the form reference.methodname(arguments); To send the toUpperCase() message (which has no arguments) to the String reference “java” “java”.toUpperCase(); The receiver is the String object “java” refers to – This message does not change the receiver – A String method never changes the String object – But produces a new String object with the desired change

18 String messages create new String objects A new object is useless unless it can be referenced – toUpperCase() creates a reference (return value) to the new object – The message ( “java”.toUpperCase(); ) becomes the reference to the new object What to do with this reference? – Send messages to the object referred to (not here) – As an argument in a message to another object System.out.println( “java”.toUpperCase());

19 Methods, arguments, and return values ClassMethodReturn valueArguments received PrintStreamprintlnNone PrintStreamprintlnNoneReference to a String object PrintStreamprintNoneReference to a String object StringtoUpperCaseReference to a String object None

20 Methods, arguments, and return values Signature of a method – Name – Description of arguments Prototype of a method – Signature – Description of its return value Table on preceding page gives us information about four methods and their prototypes

21 Reference variables Variable – An identifier that can be given a value – It can contain different values at different times – Form: type identifier; type identifier1, identifier2, …; Reference variables – An identifier that is given a reference – To be used later or repeatedly – Form: class identifier; class identifier1, identifier2, …;

22 Assignment Assignment statements – An assignment statement gives a variable a value – Form identifier = value; Variable on the left-hand side Value on the right-hand side – Variable must have already been declared – Value must be consistent with the variable type or class

23 Assignment and Equality String t = “Some String”; This is an imperative statement – Tells the computer that the variable t refers to the String object “Some String” – This is not a statement of equality – Because t can be made to refer to another String object t = “A different String”; – The previous object “Some String” is lost and no longer accessible to the program

24 Multiple assignment of objects String t = “Some String”; String s = t; t = “A different String”; – The variable t refers to the String object “Some String” – The variable s refers to the same String object “Some String” as the variable t – The variable t refers to a new String object “A different String” – Variables are independent of each other; Changing t did not affect s

25 Using String Methods Strings are immutable (they cannot be changed) – String methods do not change the reference object They provide information about the object They create references to new String objects (such as toUpperCase() ) Some commonly used String methods – toUpperCase()- toLowerCase() – length()- trim() – concat(String)- indexOf(character); – substring(number)- substring (number, number) Look at the String API for each of these methodsAPI

26 Two demonstration programs Download MiddleCharacter.zip demoMiddleCharacter.zip Download NameInitials.zip demoNameInitials.zip


Download ppt "Programs and Models Almost all computer programs model some artifact – Artifact: product of civilization, either concrete or abstract, simple or complex."

Similar presentations


Ads by Google