View
218
Download
1
Tags:
Embed Size (px)
Citation preview
Intro to Computer Science Class #2
Administrative Details and Introduction to Objects
Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault
University of Pennsylvania
30 January 2008
Today’s Agenda
• New Teaching Assistants
• Discussion (Reaction paper)
• Class Rules
• What is Java?
• BotPlay
Attendance/Participation
• I will take attendance at the beginning of every class.– Don’t be late– Don’t be disruptive– Do your work
• If you are late or absent, bring me a note.
• Questions and discussion are highly encouraged!
Stuff You Should Already Know
• DON’T COPY CODE – It’s easy to spot.– It only hurts you – the material builds on itself.– When does giving/getting help cross the line to
cheating?
• Course website: www.seas.upenn.edu/~eas285/forSLAStudents– I will also try to post other helpful links on the
resources page of the course website.
General Layout of Class
• 1:30-1:45 – Attendance and Discussion• 1:45-2:30 – Lecture• 2:30-2:40 – Short Break• 2:40-3:30* – Lab Work (*Sometimes 30 min guest speakers)
• I will teach for this class and the next, then the teaching assistants will start running the class, while I supervise and help out.
• You will be assigned a programming assignment each week which you are expected to turn in before class the next week.
What to do if you’re stuck…
1) Try things out! Don’t be scared of the computer. (Just make sure to have a backup copy of your work!)
2) Check out the resources page
3) Post to the bulletin board!
4) Come talk to us (before/after class).
5) Email me [email protected]
What is Java?• A high-level, object-oriented, programming language• Language:
– A way for people to communicate…• With Humans: Use human languages (English, Spanish, Chinese, …)
• With Computers: Use programming languages (Java, C++, Python,...)
– Made of vocabulary and syntax (rules for how to arrange the vocabulary)
What is Java?• A high-level, object-oriented, programming language• Language:
– A way for people to communicate…• With Humans: Use human languages (English, Spanish, Chinese, …)
• With Computers: Use programming languages (Java, C++, Python,...)
– Made of vocabulary and syntax (rules for how to arrange the vocabulary)
What is Java?• A high-level, object-oriented, programming language• Language:
– A way for people to communicate…• With Humans: Use human languages (English, Spanish, Chinese, …)
• With Computers: Use programming languages (Java, C++, Python,...)
– Made of vocabulary and syntax (rules for how to arrange the vocabulary)
What is Java?• A high-level, object-oriented, programming language• Language:
– A way for people to communicate…• With Humans: Use human languages (English, Spanish, Chinese, …)
• With Computers: Use programming languages (Java, C++, Python,...)
– Made of vocabulary and syntax (rules for how to arrange the vocabulary)
• English: – Vocabulary: Anything in an English dictionary. – Syntax: How to arrange noun, verb and prepositional phrases, etc
What is Java?• A high-level, object-oriented, programming language• Language:
– A way for people to communicate…• With Humans: Use human languages (English, Spanish, Chinese, …)
• With Computers: Use programming languages (Java, C++, Python,...)
– Made of vocabulary and syntax (rules for how to arrange the vocabulary)
• English: – Vocabulary: Anything in an English dictionary. – Syntax: How to arrange noun, verb and prepositional phrases, etc
• Java:– Vocabulary and Syntax: Keywords, and rules for arranging those key
words, that you will learn in this class! » You learned some keywords already: int, char, boolean, String» You also already learned some syntax: int a = 5; boolean b = true;
What is Java?• A high-level, object-oriented, programming language
• High-level: High-level of abstraction
• Abstraction: Hiding unimportant details– “The ball broke the window.”
• Does it matter what kind of ball? What color?• The speaker abstracted away unimportant details of the ball.
– Similarly in programming languages • If we programmed in “machine language” (a lower-level language) we
would have to write it specifically for that computer. • Programming in Java, we are choosing to ignore details about any
specific computer and focus on what all computers have in common.• This program will then be translated by another program to a
language specific to that machine.
What is Java?• A high-level, object-oriented, programming language
• High-level: High-level of abstraction
• Abstraction: Hiding unimportant details– “The ball broke the window.”
• Does it matter what kind of ball? What color?• The speaker abstracted away unimportant details of the ball.
– Similarly in programming languages • If we programmed in “machine language” (a lower-level language) we
would have to write it specifically for that computer. • Programming in Java, we are choosing to ignore details about any
specific computer and focus on what all computers have in common.• This program will then be translated by another program to a
language specific to that machine.
What is Java?• A high-level, object-oriented, programming language
Hardware (CPU) (electrical impulses)
Machine Language (0’s and 1’s)
Bytecode
Java Source Code (i.e. what you’ve written, held in a .java file)
Compiler compiles (translates) it into bytecode (held in a .class file)
Java Virtual Machine figures out how to run it on your computer
What is Java?• A high-level, object-oriented, programming language
• Object-Oriented: Thinking about your program as a collection of objects that have a state and a behavior. Similar to the way we think about the world.– Example:
• Object = Horse• State = name, hungry, mph• Behavior = eat, walk, gallop, jump
• Other ways to think about your program: functional, procedural…we won’t look into these
Horse
name
hungry
mph
horsey
yes
5
How We Define An Object Depends On How We’re Going To Use It
• Abstract away the unnecessary details – only keep the important (relevant to the program) ones in the state.
• Program: Keep track of baseball stats– Object: Baseball Player– State: name, G, AB, R, H, HR, on base, at bat,– Behavior: run, walk, hit ball, slide, batting
Baseball Player
name
G
AB
Ryan H
410
1461
R
H
HR
255
425
128
onBase
atBat
RBI
3
no
353
How We Define An Object Depends On How We’re Going To Use It
• Abstract away the unnecessary details – only keep the important (relevant to the program) ones in the state.
• Program: Simulate how athletes lifestyles affect their popularity with fans, salary and injury rate– Object: Athlete– State: name, sport, salary, days injured, popularity– Behavior: negotiate contract, party, complain, retire
Athlete
name Ryan H sport baseball salary 900,000
daysInjured 25 popularity high
How Do We Translate What We’ve Talked About Into Java?• class Athlete {
String name ;String sport; int salary;int daysInjured; String popularity;
Athlete(String n, String s) {name = n;sport = s;salary = 0;daysInjured = 0; popularity = “average”;
}
void negotiateContract(int newSalary) {salary = newSalary;
}
void party() {daysInjured = daysInjured + 10;
}
void complain() {popularity = “low”;
}
void retire() { salary = 0;popularity = “high”;
}
void printStates() {System.out.println(“name: “ + name + “, sport: “ + sport + “, salary: “ + salary + “, daysInjured: “ + daysInjured + “, popularity”);
}}
How Do We Translate What We’ve Talked About Into Java?• class Athlete {
String name ;String sport; int salary;int daysInjured; String popularity;
Athlete(String n, String s) {name = n;sport = s;salary = 0;daysInjured = 0; popularity = “average”;
}
void negotiateContract(int newSalary) {salary = newSalary;
}
void party() {daysInjured = daysInjured + 10;
}
void complain() {popularity = “low”;
}
void retire() { salary = 0;popularity = “high”;
}
void printStates() {System.out.println(“name: “ + name + “, sport: “ + sport + “, salary: “ + salary + “, daysInjured: “ + daysInjured + “, popularity”);
}}
The state or instance variables an object will have.
How Do We Translate What We’ve Talked About Into Java?• class Athlete {
String name ;String sport; int salary;int daysInjured; String popularity;
Athlete(String n, String s) {name = n;sport = s;salary = 0;daysInjured = 0; popularity = “average”;
}
void negotiateContract(int newSalary) {salary = newSalary;
}
void party() {daysInjured = daysInjured + 10;
}
void complain() {popularity = “low”;
}
void retire() { salary = 0;popularity = “high”;
}
void printStates() {System.out.println(“name: “ + name + “, sport: “ + sport + “, salary: “ + salary + “, daysInjured: “ + daysInjured + “, popularity”);
}}
The behaviors or methods of the object.
They can change the state/instance variables or show us the state when we ask.
How Do We Translate What We’ve Talked About Into Java?• class Athlete {
String name ;String sport; int salary;int daysInjured; String popularity;
Athlete(String n, String s) {name = n;sport = s;salary = 0;daysInjured = 0; popularity = “average”;
}
void negotiateContract(int newSalary) {salary = newSalary;
}
void party() {daysInjured = daysInjured + 10;
}
void complain() {popularity = “low”;
}
void retire() { salary = 0;popularity = “high”;
}
void printStates() {System.out.println(“name: “ + name + “, sport: “ + sport + “, salary: “ + salary + “, daysInjured: “ + daysInjured + “, popularity”);
}}
Ryan Howard is not the only Athlete. He is one of many.
The “blueprint” for an Athlete is laid out in this class.
An object is an instance of a class.
The class tells us how to set up the “state box” we’ve been drawing. But we don’t actually draw it until we create an object.
How Do We Translate What We’ve Talked About Into Java?• class Athlete {
String name ;String sport; int salary;int daysInjured; String popularity;
Athlete(String n, String s) {name = n;sport = s;salary = 0;daysInjured = 0; popularity = “average”;
}
void negotiateContract(int newSalary) {salary = newSalary;
}
void party() {daysInjured = daysInjured + 10;
}
void complain() {popularity = “low”;
}
void retire() { salary = 0;popularity = “high”;
}
void printStates() {System.out.println(“name: “ + name + “, sport: “ + sport + “, salary: “ + salary + “, daysInjured: “ + daysInjured + “, popularity”);
}}
The constructor.
This is where we set up the starting state by assigning values to the instance variables.
Now…
• Let’s play with that Athlete code.
• BotPlay (https://www.seas.upenn.edu/~cis1xx/projects/Botworld/botplay/)