Introduction - UNC Computer Science

Download Report

Transcript Introduction - UNC Computer Science

Introduction
COMP 401, Fall 2014
Lecture 1
8/19/2014
Why Do We Program?
• To answers questions
– What is the average height of the people in this
class?
double[] heights = readHeightData();
double sum_of_heights = 0.0;
for (int i=0; i<heights.length; i++) {
sum_of_heights += heights[i];
}
double avg_height = sum_of_heights / heights.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 meeting of needs. In a word, the
computer scientist 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 Evolution of Programming
• Simple programs are programmed simply.
– A program as a sequence of instructions.
• Each instruction causes a change in overall state.
• Named variables used to store values.
• May employ simple loops and conditional execution
(i.e., if/then statements).
The Evolution of Programming
• As a program becomes more complex…
– Refactor common operations into parameterized
functions or procedures.
• Can be generalized and put into libraries to be used
across different programs.
The Evolution of Programming
• As complexity continues to grow…
– … developing a program as recipe-like sequence of
instructions becomes increasingly difficult.
• Need to be able to develop and understand our
programming more like the way we understand
the world.
– As a set of interacting abstractions.
• With different parts of the program operating at different
levels of abstraction.
– This is object-oriented programming.
Object-Oriented Programming
• Programs expressed as a set of software “objects”.
– Each object is associated with data and a set of functions
or procedures that operate with that data.
• Objects are defined by their “class”.
– Each class represents an abstraction.
– Abstractions often layered on top of each other and/or
composited into more complex abstractions.
– Key challenge is developing the appropriate abstractions.
• The operation of the program is the result of creating
objects and having them interact with each other.
Major Themes Of This Course
•
•
•
•
•
•
•
Abstraction
Encapsulation
Inheritance
Polymorphism
Delegation
Design Patterns
Asynchronous Programming
A little bit about myself
• Background
• What’s with the name?
– And why can’t I remember yours
• What’s 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
communication.
– Should have received an invitation email either last night or this
morning.
• If not, come see me after class.
– http://piazza.com/unc/fall2014/comp401/home
• Textbook / Resources
– No required text.
– Will be posting links to lots of on-line materials on Piazza course
resources page.
Prerequisites
• This should not be your first introduction to
programming.
– COMP 110 or 116
– High school
– Hobbyist / Profession experience
• If you have never programmed in Java before
– Links posted on Piazza:
• Oracle Java Tutorials
• Java for Python Programmers
– Many others.
• Your goal should be to get up to speed on basic syntax and control
structures.
• We’ll be reviewing some of these today.
Assignments and Exams
• 10-12 programming assignments
• 2 midterms
– Tuesday, 9/23
– Tuesday, 11/3
• 1 final exam
– Thursday, 12/11, 12:00 noon
– Location TBA
Grading
•
•
•
•
•
40% assignments
10% recitations
15% x 2 midterms
20% final
Computing your grade
– Each component mapped to a 4.0 scale
• 4.0 = A, 3.0 = B, 2.0 = C, 1.0 = D
– Assignments and recitations 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 solution.
• 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 difficult if you don’t get an
assignment working.
• You should keep working on an assignment until you do get it
working.
• Late assignments will only count for 50%
Honor Code
• Can discuss assignment solutions at a high level
but not at the level of code.
– Code sharing = cheating
– You are allowed to pattern your code after anything I
post (i.e., class examples, etc.).
• Can help each other debug, but again up to the
point of sharing or writing code for another
person.
• If it feels wrong, it is wrong.
– If you’re not sure, ask.
Recitations
• Recitations are mandatory.
– Attendance will be taken.
– If you have a valid excuse, you must notify me in advance.
• If missed for unforeseen circumstances (sickness, etc.), please be
prepared to provide documentation.
– There may be graded quizzes/exercises during recitations.
• Try to go to the recitation you are scheduled for.
– But if necessary, you can go to any of the recitations.
• This week’s recitation is cancelled.
– Vegas, Baby!
Adding The Course
• If you are not already registered, we will be
trying to accommodate everyone.
– Assumption is that some of you will drop.
– But if not, we’ll be tight.
• Be sure to sign the attendance sheet.
– If you don’t find 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 isn’t just one way of being object-oriented.
• We’ll be using Java
– Some things will be conceptual and will apply broadly
to many other OO programming languages.
– Some things will be specific to the object-oriented
mechanisms provided by Java
Fundamental Characteristics of Java
• Strongly typed
– Variables must be declared with a type specified.
• 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 automatically allocated when objects are
created.
– Memory is automatically reclaimed when no possible
reference to an object can exist.
Installing the Java SDK
• Java SE
– Latest version is 8 but I will be using 7
• Shouldn’t matter
– You want to install the Software Development Kit (SDK)
• Note this is more than just the Java Runtime Environment (JRE)
– Available from Oracle at:
• http://www.oracle.com/technetwork/java/javase/
• Mac Users
– Built in version of Java for older Macs is 1.6
• Strange relationship 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 straightforward to use.
– Available at http://eclipse.org
• You don’t have to use it for assignments, but…
– Some recitation exercises will require it so you need to
have it installed.
– I will be using it for examples in class.
– Assignment directions will assume you are.
Program Organization and Execution
• All code in Java is organized into classes and interfaces
– One public class or interface per file.
• Not strictly true, but accept this as true for now.
• Also, don’t worry about what an interface is for now.
– Must match file 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 specific class name as the “main class”.
– Interpreter starts execution with the method “main”
• Must be defined with the signature:
public static void main(String[] args)
AverageHeightApp (1/2)
• Open a plain text editor.
• Create a file called AverageHeightApp.java
• Enter the following text:
public class AverageHeightApp {
public static void main(String[] args) {
double[] heights = readHeightData();
double sum_of_heights = 0.0;
for (int i=0; i<heights.length; i++) {
sum_of_heights += heights[i];
}
double avg_height = sum_of_heights /
heights.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 declaration of main
– Using a for loop to iterate over an array
– Using System.out.println for console output
• See reference documentation for System class
• Link on Piazza resources page
– Automatic type conversion from integer to string
– Contextual interpretation of + operator
You might want to drop the class if…
• … you didn’t recognize the idea of an array of values and/or
the notion of retrieving a value from the array by its index.
• … you can’t recognize what in the program is acting as a
variable.
• … you are unsure of how a for loop works.
• ... you don’t grock the notion of defining a function and/or
calling a function.
• … you can’t write a program of similar complexity in some
other language if not Java.
• The above are necessary conditions for success in this class,
but may not be sufficient conditions for success.
– Trust your own judgment, don’t be afraid of a challenge.
– Programming takes practice.
AverageHeightApp – take 2
• Same as before, but with Eclipse.
– Eclipse Workspace
– Creating new project
– Creating a new package
– Creating new class
• More in recitation next week