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

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 1: Introduction
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
C++ fundamentals.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
Computer Programming 12 Mr. Jean March 3 rd, 2014.
PHP: Hypertext Processor Fred Durao
Introducing Java.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2 How to Compile and Execute a Simple Program.
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
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
Introduction to Computer Systems and the Java Programming Language.
POS 406 Java Technology And Beginning Java Code
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Programming Process Programming in Java is an exercise in using pre-defined classes and writing new classes to fill in the gaps A procedure for determining.
Introduction to Java Java Translation Program Structure
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Getting Started.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
By Mr. Muhammad Pervez Akhtar
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Classes, Interfaces and Packages
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Execution ways of program References: www. en.wikipedia.org/wiki/Integrated_development_environment  You can execute or run a simple java program with.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Basic Computation
“Under the hood”: Angry Birds Maze
Chapter 3 GC 101 Java Fundamentals.
Console Output, Variables, Literals, and Introduction to Type
String class.
Data types and variables
Statements, Comments & Simple Arithmetic
PHP.
Tutorial 10: Programming with javascript
In this class, we will cover:
Computer Programming-1 CSC 111
Instructor: Alexander Stoytchev
Presentation transcript:

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

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

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

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)

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

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

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

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

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

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)

Exercise Let us modify FirstProgram as an exercise

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

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’

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)

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

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

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

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());

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

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

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, …;

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

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

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

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

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