CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 645-4739 1.

Slides:



Advertisements
Similar presentations
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall NEW   NEW 1.
Lecture #3 – Agenda Cell phones off & name signs out –I’ll judge signs on Wednesday next week Quick review & Questions Activity Problem solving.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
Summary of the lecture We introduced the local variable dependency –This is a binary relationship (a relationship between two classes), similar to instantiation.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE111: Great Ideas in Computer Science Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE111: Great Ideas in Computer Science Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Lecture #4 Agenda Cell phones off & name signs out Review Questions? Objects The birds-and-the-bees talk.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Lecture #4 Agenda Announcements Review Questions? Classes and objects UML class diagrams Creating.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE 116 Introduction to Computer Science For Majors II Carl Alphonce 219 Bell Hall.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 1.
String Escape Sequences
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computer Science.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall NEW   NEW 1.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 1.
CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall 1.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSCE Fall 2013 Prof. Jennifer L. Welch.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
CSCE Fall 2012 Prof. Jennifer L. Welch.
Overloading functions
C Programming Pointers
Presentation transcript:

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall

Announcements / Reminders Cell phones off Name signs out 2

Expression evaluation evaluating new example1.Terrarium() –causes an object to be created and initialized –produces a value

(part of) memory

evaluating a ‘new’ expression used available used When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four byte for the sake of this example)

evaluating a ‘new’ expression used reserved by ‘new’ available used When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four bytes for the sake of this example) Next, new must secure a contiguous block of memory four bytes large, to store the representation of the object.

evaluating a ‘new’ expression used available used When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four byte for the sake of this example) Next, new must secure a contiguous block of memory four bytes large, to store the representation of the object. Bit strings representing the object are written into the reserved memory locations.

evaluating a ‘new’ expression used available used When evaluating an expression like ‘new example1.Terrarium()’, the operator ‘new’ first determines the size of the object to be created (let us say it is four byte for the sake of this example) Next, new must secure a contiguous block of memory four bytes large, to store the representation of the object. Bit strings representing the object are written into the reserved memory locations. The starting address of the block of memory holding the object’s representation is the value of the ‘new’ expression. This address is called a ‘reference’.

evaluating a ‘new’ expression A similar thing happens when we evaluate another ‘new’ expression like ‘new example1.Caterpillar()’. used available used

reserved by ‘new’ available used evaluating a ‘new’ expression A similar thing happens when we evaluate another ‘new’ expression like ‘new example1.Caterpillar()’. Supposing that an example1.Caterpillar object occupies two bytes of memory, new reserves a contiguous block of two bytes…

used available used evaluating a ‘new’ expression A similar thing happens when we evaluate another ‘new’ expression like ‘new example1.Caterpillar()’. Supposing that an example1.Caterpillar object occupies two bytes of memory, new reserves a contiguous block of two bytes, writes bit strings representing the object to those memory locations, and the starting address of this block of memory is the value of the ‘new’ expression.

DrJava’s response When we evaluate these ‘new’ expressions in DrJava, what is the response we get? > new example1.Terrarium() example1.Terrarium[frame0,0,0,608x434,layout=java.awt.BorderLayout,tit le=,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=j avax.swing.JRootPane[,4,30,600x400,layout=javax.swing.JRootPane$Roo tLayout,alignmentX=0.0,alignmentY=0.0,border=,flags= ,maximu mSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true] > new example1.Caterpillar()

DrJava’s response These responses don’t look like memory addresses. What’s going on? After DrJava evaluates the expression, it must print the value. The way Java works, when a reference is printed a textual representation of the object it refers to is produced (as defined by the object itself)

Object communication To put an example1.Caterpillar object inside an example1.Terrarium object, send an “add” message to the example1.Terrarium object. We say we “invoke” (or “call”) the add “method”. > new example1.Terrarium().add(new example1.Caterpillar())

Short aside Where do objects come from? (a.k.a The “birds and bees” talk) (Hint: it’s not the stork!)

Programmer writes a program in a high-level language like Java: example1.Terrarium.java Computers don’t understand programs expressed in high- level languages  ?

Compilation A compiler translates program to an equivalent low-level form that a computer can understand example1.Terrarium.class Compiler translates Programmer writes a program in a high-level language like Java: example1.Terrarium.java

Runtime Runtime refers to the time during which a program is executing, or running. Compiler translates

Objects exist only at runtime Objects do not exist while the programmer writes the program, except in their minds. Compiler translates

Huh? If objects are the basic building blocks of object-oriented programs, and programmers don’t directly manipulate objects, what do programmers write? They write class definitions. Objects are instances of classes. Classes are instantiated only at runtime.

...now we return to our regular programming...

We can add a Caterpillar to the Terrarium like this: new example1.Terrarium().add(new example1.Caterpillar()) We can add an Ant to the Terrarium similarly: new example1.Terrarium().add(new example1.Ant()) Can we add a Caterpillar and an Ant to the same Terrarium? Can we write the following? new example1.Terrarium().add(new example1.Caterpillar()) new example1.Terrarium().add(new example1.Ant()) We can write it, but it won’t do what we want. Q: what happens?

A: Two example1.Terrarium objects are created. –One contains an example1.Caterpillar –The other contains an example1.Ant When adding the Ant, we cannot refer to the Terrarium we added the Caterpillar to, because: –we have no reference to it after adding the Caterpillar The value of an expression is lost if it is not –used right away, or –remembered

Suppose we have a Caterpillar in a Terrarium: new example1.Terrarium().add(new example1.Caterpillar()) How do we make the Caterpillar move? We call “start()” on the Caterpillar. How do we do this? Can we write the following? new example1.Terrarium().add(new example1.Caterpillar()) new example1.Caterpillar().start() We can, but it won’t do what we want. Q: what happens?

A: Two example1.Caterpillar objects are created. –One is placed in the example1.Terrarium, and is NOT “started”. –The other is NOT put into the example1.Terrarium, but is “started”. We cannot refer to the Caterpillar we added to the Terrarium, because: –we have no reference to it after adding it. The value of an expression is lost if it is not –used right away, or –remembered.

We want to write something like this: t. add( c ) c. start() (notice ‘c’ used in two places) To do this, we need a variable

A variable is: (at its most basic) a storage location in memory for example, location 120: space for a variable

A variable has: a name a location a type a value a scope a lifetime  in the HLL (Java)  in memory  representation scheme/size  contents We’ll discuss these later

Why are types important? Recall the discussion from early in the semester about how to interpret a string like “ ” – the proper interpretation depends on the encoding scheme (e.g. two’s complement or IEEE754) The type of a variable tells the compiler, among other things, which encoding schemed to use to read/write data from/to the variable. It also tells us what methods we can call.

The variable declaration Consists minimally of: type & name What is a type? A class is a type. –Remember: objects are instantiated from classes. Examples of variable declarations: example1.Terrariumt; example1.Caterpillar c;