CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011...
-
Upload
damon-malson -
Category
Documents
-
view
214 -
download
2
Transcript of CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011...
![Page 1: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/1.jpg)
CSE 113Introduction to
Computer Programming
Lecture slides for Week 3
Monday, September 12th, 2011Instructor: Scott Settembre
![Page 2: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/2.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
2
ADMINISTRATION NOTESSection 1
Monday, Sept. 12th, 2011
![Page 3: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/3.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
3
Greenfoot Details
• The Greenfoot resource page that can be found at:
http://www.greenfoot.org/book/
• I usually put the information we need and copy it to UBLearns, but you may find the link above useful, as well as the following link for additional information, videos, and tips:
http://www.greenfoot.org/Monday, Sept. 12th, 2011
![Page 4: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/4.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
4
Lab Attendance
• Lab attendance is definitely mandatory. If you cannot go to your assigned lab (or miss it), then you should ask your TA if you can go instead to another lab time.
• Lab times are:
Monday, Sept. 12th, 2011
Lab Day Section # Time TA in charge
Wed B1 12-1:50pm Bich Vu
Wed B4 2-3:50pm Troy Koss
Thurs B2 9-10:50am Troy Koss
Friday B3 10-11:50am Bich Vu
![Page 5: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/5.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
5
Contact
• You can email any one of the TA’s for your questions and if they cannot help you, please do email me.
• Remember to include “CSE113:” in the subject line.
Monday, Sept. 12th, 2011
![Page 6: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/6.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
6
Teacher Assistants and Office hours
• You can find the up-to-date hours and locations in the “Contact” section of UBLearns.
• Here are the names, emails, and office hours as of 9/12/2011:
Monday, Sept. 12th, 2011
Name Email address Office hours (Bell 329)
Bich (pronounced “Bic”) Vu [email protected] Monday (11-11:50pm)Friday (12-12:50pm)
Troy Koss [email protected] Tuesday (3-5pm)
Scott Settembre [email protected] TDB after poll class
![Page 7: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/7.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
7
UB Helpful Information• UB Free Softwarehttp://ubit.buffalo.edu/software/ • Using your own computer at UBhttp://ubit.buffalo.edu/newstudent/ • Help from UB IThttps://wiki.cse.buffalo.edu/services/ • FREE Microsoft Software – ALL OF IT! – MSDN-AAhttps://wiki.cse.buffalo.edu/services/content/microsoft-developer-network-academic-alliance-msdn-aaMonday, Sept. 12th, 2011
![Page 8: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/8.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
8
VERY IMPORTANT!
• Go to Bell 101 and log in by Monday, September 12th, 2011.– Your username/password is the same as your UB Email
username/password.
• If you cannot log in, the make sure you get your account enabled.– Either talk to the attendant on duty in Bell 101, OR– Go to: http://www.sens.buffalo.edu/accounts/
• If you go to lab and cannot log in, everyone will point and laugh at you!!!
Monday, Sept. 12th, 2011
![Page 9: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/9.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
9
You are behind the class if…
• If you have not completed chapter 1 and started chapter 2, then you are behind the rest of the class.
• Please do the following:– Attempt setting up and doing chapter 1 and 2 in Bell 101.– If you have too much trouble getting started, then:
• Go to a TA office hour, OR• Email a TA, OR• Go to an additional lab section before you go to your lab.
Monday, Sept. 12th, 2011
![Page 10: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/10.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
10
GETTING STARTED NOTESSection 2
Monday, Sept. 12th, 2011
![Page 11: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/11.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
11
Lab Tips from Week 2
• If you didn’t have the book at this time, you had the option to use the “tutorial” at the Greenfoot website:
http://www.greenfoot.org/doc/tutorial/tutorial.html
• In addition, free chapters 2 and 3 could also be downloaded from:
http://www.greenfoot.org/book/toc.html
Monday, Sept. 12th, 2011
![Page 12: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/12.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
12
Greenfoot Scenarios
• For this course, in order to follow the book, you need to additionally download the book scenarios.
• I have posted them on UBLearns (guaranteed virus free), or you can download them directly from the book website:
http://www.greenfoot.org/book/(Click the second link in the section marked “For students”)
• You should create a directory called “greenfoot” and then place the extracted “book-scenarios” directory into that directory.
Monday, Sept. 12th, 2011
![Page 13: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/13.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
13
For those that used the Tutorial…
• Please quickly do/look through chapter 1 when you get the book to make sure that you understand all the concepts.
• The tutorial also has some “vague” instructions, including where you should place the code and a change that is fixed if you change all the “.gif” references to “.png”.
Monday, Sept. 12th, 2011
![Page 14: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/14.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
14
CHAPTER 3Section 3
Monday, Sept. 12th, 2011
![Page 15: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/15.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
15
What is a “package”?
• A package is a bunch of code.– It may be one file or many files.– It may call methods or use objects from any of
those files.– Usually, a package contains related objects and
there is some dependency.
Monday, Sept. 12th, 2011
![Page 16: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/16.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
16
Can “objects” be created using packaged code?
• In this course, you will find the answer to this to be “Yes.”
• However, you do not always need to create an object to use the code in a package.
• These are called “static” methods.
Monday, Sept. 12th, 2011
![Page 17: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/17.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
17
I’m confused, “static” methods?
• We have been shown that objects can have methods.
• We have seen how to call an object method.
• In the case of “static” methods, these are not part of an instantiated object (a created object), instead they are part of the class.
Monday, Sept. 12th, 2011
![Page 18: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/18.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
18
“static” method demo
• See that when we call the “getRandom” method, we need to refer to the package name.
• This “reference” to the method is called “dot notation”.
• The package name is proceeded by a “.” period and then by the name of the method you wish to call.– For example, “Greenfoot.getRandomNumber(100);”
Monday, Sept. 12th, 2011
![Page 19: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/19.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
19
What is a “subclass”?
• A subclass is a more specific instance of its superclass.– A “dog” is more specific than “animal”.– A “dog” object would then have functionality
(methods or properties) that are not in “animal”.– For example, a method called “wagTail” could be in
the “dog” class, but not the “animal” class, since not all animals have tails.
• We can create a subclass quite easily in Greenfoot.
Monday, Sept. 12th, 2011
![Page 20: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/20.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
20
New subclass demonstration
• You create a new subclass by right-clicking on the class and then selecting “new subclass”.
Monday, Sept. 12th, 2011
![Page 21: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/21.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
21
Adding new code to a class
• To add new code to a class, you will want to put the method signature inside the “code block” of the class.
• The “code block” is the outer set of curly braces “{“ and “}”
• The new method must be inside that code block so that the computer knows that it describes a method that belongs to that class.
Monday, Sept. 12th, 2011
![Page 22: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/22.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
22
Adding a new method demo
• Go to the code of the new class and show where to put any new methods that will be created.
• Notice how the new method then appears in the dropdown menu of any new object you create of the new class.
Monday, Sept. 12th, 2011
![Page 23: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/23.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
23
Number comparisons
• In this chapter, you will have the need to compare numbers (or the return values of methods) to other numbers.
• Java has the following comparison operators:
Monday, Sept. 12th, 2011
Operator Meaning Example
== Equal to 5 == 5
!= Not equal to 5 != 2
< Less than 2 < 5
> Greater than 6 > 4
<= Less than or equal to 5 <= 5
>= Greater than or equal to 6 >= 6
![Page 24: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/24.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
24
Comparisons can be used in Boolean Expressions
• The “if” statement contains a test for a Boolean expression.
Monday, Sept. 12th, 2011
public void act() {
if ( atWorldEdge() ) { turn(17); }
if ( Greenfoot.getRandomNumber(100) < 10 ) { turn(5); } move(); }
Notice that the there is a comparison here that compares the return value from the static method “getRandomNumber” to the value 10.
![Page 25: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/25.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
25
Getting input from users
• This is one of the more important things to know how to do.
Monday, Sept. 12th, 2011
public void checkKeypress(){ if (Greenfoot.isKeyDown(“left”)) { turn(-4); } if (Greenfoot.isKeyDown(“right”)) { turn(4); }}
Notice that “isKeyDown” is a static method of the package “Greenfoot”. You do not have an object called “Greenfoot”.
![Page 26: CSE 113 Introduction to Computer Programming Lecture slides for Week 3 Monday, September 12 th, 2011 Instructor: Scott Settembre.](https://reader036.fdocuments.us/reader036/viewer/2022070308/551c1a61550346b24f8b5893/html5/thumbnails/26.jpg)
University at Buffalo: CSE 113 Instructor: Scott Settembre
26
Lab for this week
• You will experiment and learn how to use these key concepts.– Making a subclass.– Writing a new method.– Calling “static” methods of a package.– Getting keyboard input.
• Get to know how to do this, since you will need to understand how to do these things to get a good grade on project 1.
Monday, Sept. 12th, 2011