cpsc 111
play

CPSC 111 Introduction to Computation Introduction to Computation - PowerPoint PPT Presentation

CPSC 111 Introduction to Computation Introduction to Computation October 1 st , 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger Administrative Stuff Administrative Stuff If you have flu symptoms, there's a good chance that it really


  1. CPSC 111 Introduction to Computation Introduction to Computation October 1 st , 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger

  2. Administrative Stuff Administrative Stuff If you have flu symptoms, there's a good chance that it really is the flu really is the flu. If it is the flu, there s a really good chance If it is the flu there's a really good chance that it's the H1N1 or "swine flu" virus. Do not go to class, or lab, or tutorial, or office hours. Stay home until you are well (at least 24 hours after your fever subsides without the help of medication). p ) Do not go to your doctor just to get a note to bring to us. We don't want you to get a note We don t want you to get a note. We want you to point your We want you to point your web browser to this URL: https://www cs ubc ca/local/facilities/h1n1 jsp https://www.cs.ubc.ca/local/facilities/h1n1.jsp

  3. Administrative Stuff Here's the same URL again: htt https://www.cs.ubc.ca/local/facilities/h1n1.jsp // b /l l/f iliti /h1 1 j Here, you can inform us that you are staying home because y y y g of the flu with the click of a button. Instructors in all your CS classes will be automatically informed that you are sick. Clicking the button is your substitute for medical Clicking the button is your substitute for medical documentation (i.e., it's your doctor's note). It applies only if you suspect you have the flu, not to other illnesses When you have determined that you are well enough to return, you return to the URL and click the button that let's us know you're back. Again, your CS instructors will be automatically notified.

  4. Administrative Stuff Administrative Stuff Between the time you declare yourself sick and the time you declare yourself well you are excused from exams declare yourself well, you are excused from exams, quizzes, homework, and labs in all your CS courses. You'll have to make them up later. You are not expected to attend class, and you will not be allowed to write exams or quizzes in any CS courses during that time. If you are sick but feel well enough to try to keep up, follow along with your assigned readings, published lecture slides or notes and homework assignments as best you can or notes, and homework assignments as best you can. You You can also do your labs at home at show them to your lab TA when you are well. When you return, you and I can discuss how to catch up.

  5. Administrative Stuff Administrative Stuff Your first exam is next week! • Wed Oct 7, 6:30 -7:30 • Location: L ti - Go to the course WebCT Vista page - Select link “Important: Midterm 1 rooms” - Find your name in the list that appears Find your name in the list that appears - The label next to it indicates the room you will have to go to ill ha e to go to

  6. Administrative Stuff Administrative Stuff Homework assignment due tomorrow! TIP: • Ch 3.9 (5.5 in second edition) explains some of the concepts you need to do part 2 of the assignment. • It also introduces the Graphics2D package and some methods associated with it methods associated with it. • You can safely ignore them, as long as you use only the methods listed in the assignment instructions in your solution your solution

  7. Administrative Stuff Administrative Stuff Big reading in Big Java (2nd or 3rd edition) : Chapter 1.1 through 1.8 Chapter 2 1 through 2 10 Chapter 2.1 through 2.10 Chapter 4.1, 4.7 before your next lab Chapter 3.1 through 3.8 p g Chapter 4 this week (includes material already covered in Ch. 2)

  8. Review of previous Review of previous 1 1. Creating our own classes and objects C ti l d bj t 2. Understanding encapsulation and how it is g p enforced via private variables

  9. Classes for a Roller Coasters Simulator Classes for a Roller Coasters Simulator

  10. There is even a Game about Roller Coasters : Roller Coaster Tycoon ll

  11. Roller Coaster class so far public class RollerCoaster2 Class header { private int numberOfRiders; p ; private int capacity; public RollerCoaster2() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public void emptyCoaster() { numberOfRiders = 0; }

  12. Roller Coaster class so far public class RollerCoaster2 { private int numberOfRiders; p ; • Instance variables (fields) Instance variables (fields) private int capacity; • Declared as private to preserve encapsulation • They can only be accessed via y y p public RollerCoaster2() () { methods of this class numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); y p ( y g ); } public void emptyCoaster() p p y () { numberOfRiders = 0; }

  13. Roller Coaster class so far public class RollerCoaster2 { • Constructor method private int numberOfRiders; p ; • The method that actually creates a private int capacity; new object of a class Instance • Must have the same name as the class public RollerCoaster2() p () { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); y p ( y g ); } public void emptyCoaster() p p y () { numberOfRiders = 0; }

  14. Roller Coaster class so far includes this.. public class RollerCoaster2 { private int numberOfRiders; p ; private int capacity; public RollerCoaster2() p () { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); y p ( y g ); } • Simulates people getting off a coaster public void emptyCoaster() p p y () by resetting numberOfRiders to 0 { numberOfRiders = 0; • It is called a mutator method (or a } setter ) because it changes (sets) one the object’s instance variables (fields) h b ’ bl (f ld )

  15. ...followed by this followed by this public void board() { if (numberOfRiders < capacity) { numberOfRiders = numberOfRiders+1; System.out.println(“Welcome on board!”); } else { System.out.println(“Sorry, the coaster’s full"); } } } • Simulates a new rider trying to board the coaster • Checks if there are still seats available • If yes, increases number of riders by one, if not expresses regret If i b f id b if

  16. Modified main method Modified main method public class ThemePark2 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster2 thunderbolt = new RollerCoaster2(); RollerCoaster2 flightDeck = new RollerCoaster2(); g (); thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster (); p y (); thunderbolt.board(); flightDeck.board(); flightDeck.board(); } } What now? We run it and see what happens, of course. ? f

  17. Today’s plan Today s plan 1. How to make a method return values via the return statement 2. Describe how to how to create a method that expects values passed as parameters expects values passed as parameters 3. Wake-up call

  18. Let’s make another method Let s make another method emptyCoaster() is a mutator method (aka a setter ) b because it changes the value on an instance variable it h th l i t i bl Now let’s make an accessor method (aka a getter ), that is a method that allows us to retrieve the value of an instance variable. In this case, we want to provide a way to retrieve the number of riders on a roller coaster from outside the class. For instance, we’d like to be able to add this to l F i t ’d lik t b bl t dd thi t our theme park...

  19. ThemePark3 public class ThemePark3 { public static void main (String[] args) p ( g[] g ) { System.out.println("Building my theme park"); RollerCoaster3 thunderbolt = new RollerCoaster3(); RollerCoaster3 flightDeck = new RollerCoaster3(); g thunderbolt.board(); thunderbolt.board(); thunderbolt.board(); thunderbolt.emptyCoaster(); p y thunderbolt.board(); flightDeck.board(); flightDeck.board(); System.out.println("There are " + thunderbolt.getnumberOfRiders() + “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.getnumberOfRiders() + “ riders on Flight Deck."); } }

  20. Why? Why can we just do System.out.println("There are " + thunderbolt.numberOfRiders + y p ( “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.numberOfRiders + “ riders on Flight Deck."); } }

  21. Why? Why can we just do System.out.println("There are " + thunderbolt.numberOfRiders + y p ( “ riders on Thunderbolt."); System.out.println("There are " + flightDeck.numberOfRiders + “ riders on Flight Deck."); } } Because numberOfRiders is private and cannot be p directly accessed from outside the RollerCoaster class Encapsulation!

  22. What will the new method look like? What will the new method look like? public class RollerCoaster3 { private int numberOfRiders; private int capacity; public RollerCoaster3() { numberOfRiders = 0; capacity = 2; System.out.println("Another ride is ready to go!"); } public ????????? { ?????? }

Recommend


More recommend