Sie sind auf Seite 1von 34

Introduc)on

COMP 401, Spring 2014


Lecture 1
1/9/2014

Why Do We Program?
To answers ques)ons
What is the average height of the men in this
class?
double[] height_of_men = readHeightData();!
double sum_of_heights = 0.0;!
for (int i=0; i<height_of_men.length; i++) {!
!sum_of_heights += height_of_men[i];!
}!
double avg_height = sum_of_heights / height_of_men.length;!

Why Do We Program?
To understand our world

Why Do We Program?
To discover

Why Do We Program
To make money

Why Do We Program?
To create new worlds

Why Do We Program?
To evolve

What Is Programming?
In contrast with many engineers who make
houses, cars, medicines, clothing for human
need and enjoyment, we make things that do
not themselves meet human needs, but serve as
tools in the mee)ng of needs. In a word, the
computer scien)st is a toolsmith no more, but
no less. It is an honorable calling.
-- Dr. Fred P. Brooks, Jr.

Programming is
Toolsmithing

How Do We Program?

How do we understand the world?

The Evolu)on of Programming


Simple programs are programmed simply.
A program as a sequence of instruc)ons.
Each instruc)on causes a change in overall state.
Named variables used to store values.
May employ simple loops and condi)onal execu)on
(i.e., if/then statements).

The Evolu)on of Programming


As a program becomes more complex
Refactor common opera)ons into parameterized
func)ons or procedures.
Can be generalized and put into libraries to be used
across dierent programs.

The Evolu)on of Programming


As complexity con)nues to grow
developing a program as recipe-like sequence of
instruc)ons becomes increasingly dicult.

Need to be able to develop and understand our


programming more like the way we understand
the world.
As a set of interac)ng abstrac)ons.
With dierent parts of the program opera)ng at dierent
levels of abstrac)on.

This is object-oriented programming.

Object-Oriented Programming
Programs expressed as a set of so_ware objects.
Each object is associated with data and a set of func)ons
or procedures that operate with that data.

Objects are dened by their class.


Each class represents an abstrac)on.
Abstrac)ons o_en layered on top of each other and/or
composited into more complex abstrac)ons.
Key challenge is developing the appropriate abstrac)ons.

The opera)on of the program is the result of crea)ng


objects and having them interact with each other.

Major Themes Of This Course

Abstrac)on
Encapsula)on
Inheritance
Polymorphism
Delega)on
Design Paderns
Asynchronous Programming

A lidle bit about myself


Background
Whats with the name?
And why cant I remember yours

Whats with the music?

Course Management
Sakai

Turning in assignments and as a gradebook

Piazza

Course notes, resources, Q&A, and all of other course related


communica)on.
Should have received an invita)on email either last night or this
morning.
If not, come see me a_er class.

hdp://piazza.com/unc/spring2014/comp401/home

Textbook / Resources

No required text.
Will be pos)ng links to lots of on-line materials on Piazza course
resources page.

Prerequisites
This should not be your rst introduc)on to programming.
COMP 110 or 116
High school
Hobbyist / Profession experience

If you have never programmed in Java before

hdp://www.oracle.com/technetwork/java/
compile-136656.html
hdp://docs.oracle.com/javase/tutorial/java/nutsandbolts/
index.html
Many others.

Your goal should be to get up to speed on basic syntax and control


structures.
Well be reviewing some of these today.

Assignments and Exams


10 programming assignments
2 midterms
Tuesday, 2/11
Thursday, 3/20

1 nal exam
Monday, 5/5, 12:00 noon
Loca)on TBA

Grading

40% assignments
10% recita)ons
15% x 2 midterms
20% nal
Compu)ng your grade

Each component mapped to a 4.0 scale


4.0 = A, 3.0 = B, 2.0 = C, 1.0 = D

Assignments and recita)ons will be mapped as follows:

95% -> 4.0


90% -> 3.5
80% -> 2.5
70% -> 1.5
40% -> 0.0

Exams will be curved

Assignments
May restrict the use of certain Java libraries and/or
features.
May require the use of certain programming
techniques or approaches to the solu)on.
Correctness is but one criteria for grading.
Elegance and readability will also count.

Some of the assignments will build on top of each


other.
This can make things dicult if you dont get an
assignment working.

You should keep working on an assignment un)l you do get it


working.
Late assignments will only count for 50%

Honor Code
Can discuss assignment solu)ons at a high level
but not at the level of code.
Code sharing = chea)ng
You are allowed to padern your code a_er anything I
post (i.e., class examples, etc.).

Can help each other debug, but again up to the


point of sharing or wri)ng code for another
person.
If it feels wrong, it is wrong.
If youre not sure, ask.

Recita)ons
Recita)ons are mandatory.
Adendance will be taken.
If you have a valid excuse, you must no)fy me in advance.
If missed for unforeseen circumstances (sickness, etc.), please be
prepared to provide documenta)on.

There may be graded quizzes/exercises during recita)ons.

You are free to go to any of the 4 recita)ons.


As of now, the last recita)on is severely under-enrolled
and the rst two are over-enrolled.
If this proves problema)c, we may need to do something to
rebalance the recita)ons.

Tomorrows recita)on is cancelled.

Adding The Course


If you are not already registered, we will be
trying to accommodate everyone.
Assump)on is that some of you will drop.
But if not, well be )ght.

Be sure to sign the adendance sheet.


If you dont nd your name on the sheet, be sure
to sign up on the blank page.
Provide name, onyen, email, and pid.
Print CLEARLY

OO Programming Languages
Almost all modern programming languages
support some sort of object-oriented
programming.
Not all of them will do so in the same way.
There isnt just one way of being object-oriented.

Well be using Java


Some things will be conceptual and will apply broadly
to many other OO programming languages.
Some things will be specic to the object-oriented
mechanisms provided by Java

Fundamental Characteris)cs of Java


Strongly typed

Variables must be declared with a type specied.

Dichotomy between value types and reference types.


In some OO languages, everything is an object.
Not quite true in Java.

Some basic data types are objects (e.g., String, arrays)


Other basic data types are not (integers, real numbers, booleans)

Garbage collected memory

Memory is automa)cally allocated when objects are


created.
Memory is automa)cally reclaimed when no possible
reference to an object can exist.

Installing the Java SDK


Java SE

Latest version is 7 update 10


Some)mes referred to as 1.7

You want to install the So_ware Development Kit (SDK)


Note this is dierent than the Java Run)me Environment (JRE)

Available from Oracle at:

hdp://www.oracle.com/technetwork/java/javase/

Mac Users

Built in version of Java for older Macs is 1.6


Strange rela)onship between Apple and Oracle.
This should work for what we are going to do.

Latest version is available from Oracle

But, only for MacOS > 10.8 running on a 64-bit processor

Eclipse
Java-based Integrated development
environment (IDE)
Not necessarily the best, but reasonable, free, and
fairly straighuorward to use.
Available at hdp://eclipse.org

You dont have to use it for assignments, but


Some recita)on exercises will require it so you
need to have it installed.
I will be using it for examples in class.

Program Organiza)on and Execu)on


All code in Java is organized into classes and interfaces
One public class or interface per le.

Not strictly true, but accept this as true for now.


Also, dont worry about what an interface is for now.

Must match le name.


Classes can be organized into packages.
Again, more on that later.

Java compiler (javac) produces byte code.


Java interpreter (java) executes program

Need to specify a specic class name as the main class.


Interpreter starts execu)on with the method main
Must be dened with the signature:
public static void main(String[] args)!

AverageHeightApp (1/2)
Open a plain text editor.
Create a le called AverageHeightApp.java
Enter the following text:
public class AverageHeightApp { !
!
!
public static void main(String[] args) {!
double[] height_of_men = readHeightData();!
double sum_of_heights = 0.0;!
for (int i=0; i<height_of_men.length; i++) {!
sum_of_heights += height_of_men[i];!
!}!
!double avg_height = sum_of_heights /!
height_of_men.length;!
!System.out.println("The average height is: " + avg_height + " inches");!
}!
!
static double[] readHeightData() {!
double[] height_data = {66.0, 72.0, 69.5, 68.2, 75.0, 64.5, 63.0};!
return height_data;!
}!
}!

AverageHeightApp (2/2)
At a terminal command line (console for you
Windows people), execute the following
commands:
> javac AverageHeightApp.java
> java AverageHeightApp

AverageHeightApp Notables
Demonstrates:
Use of javac and java from the command line
Appropriate declara)on of main
Using a for loop to iterate over an array
Using System.out.println for console output
See reference documenta)on for System class
Link on Piazza resources page

Automa)c type conversion from integer to string


Contextual interpreta)on of + operator

You might want to drop the class if


you didnt recognize the idea of an array of values and/or
the no)on of retrieving a value from the array by its index.
you cant recognize what in the program is ac)ng as a
variable.
you are unsure of how a for loop works.
... you dont grock the no)on of dening a func)on and/or
calling a func)on.
you cant write a program of similar complexity in some
other language if not Java.
The above are necessary condi)ons for success in this class,
but may not be sucient condi)ons for success.
Trust your own judgement, dont be afraid of a challenge.
Programming takes prac)ce.

AverageHeightApp take 2
Same as before, but with Eclipse.
Eclipse Workspace
Crea)ng new project
Crea)ng a new package
Crea)ng new class

More in recita)on next week

Das könnte Ihnen auch gefallen