260 likes | 390 Views
CSE 113 Introduction to Computer Programming. Lecture slides for Week 3. Monday, September 12 th , 2011 Instructor: Scott Settembre. Section 1. administration notes. Greenfoot Details. The Greenfoot resource page that can be found at: http://www.greenfoot.org/book/
E N D
CSE 113Introduction toComputer Programming Lecture slides for Week 3 Monday, September 12th, 2011 Instructor: Scott Settembre
Section 1 administration notes University at Buffalo: CSE 113 Instructor: Scott Settembre
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/ University at Buffalo: CSE 113 Instructor: Scott Settembre
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: University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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: University at Buffalo: CSE 113 Instructor: Scott Settembre
UB Helpful Information • UB Free Software http://ubit.buffalo.edu/software/ • Using your own computer at UB http://ubit.buffalo.edu/newstudent/ • Help from UB IT https://wiki.cse.buffalo.edu/services/ • FREE Microsoft Software – ALL OF IT! – MSDN-AA https://wiki.cse.buffalo.edu/services/content/microsoft-developer-network-academic-alliance-msdn-aa University at Buffalo: CSE 113 Instructor: Scott Settembre
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!!! University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 2 GETTING STARTED notes University at Buffalo: CSE 113 Instructor: Scott Settembre
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 University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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”. University at Buffalo: CSE 113 Instructor: Scott Settembre
Section 3 Chapter 3 University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
“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);” University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
New subclass demonstration • You create a new subclass by right-clicking on the class and then selecting “new subclass”. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre
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: University at Buffalo: CSE 113 Instructor: Scott Settembre
Comparisons can be used in Boolean Expressions • The “if” statement contains a test for a Boolean expression. Notice that the there is a comparison here that compares the return value from the static method “getRandomNumber” to the value 10. • public void act() • { • if ( atWorldEdge() ) • { • turn(17); • } • if ( Greenfoot.getRandomNumber(100) < 10 ) • { • turn(5); • } • move(); • } University at Buffalo: CSE 113 Instructor: Scott Settembre
Getting input from users • This is one of the more important things to know how to do. • 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”. University at Buffalo: CSE 113 Instructor: Scott Settembre
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. University at Buffalo: CSE 113 Instructor: Scott Settembre