Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.

Similar presentations


Presentation on theme: "1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013."— Presentation transcript:

1 1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013

2 Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter 2013 2

3 Definitions Access modifiers: public vs. private OO: declaration vs. instantiation OO: reference vs. object Variables: scope vs. lifetime Class members: attributes vs. methods Information hiding: Mutators vs. accessors CPSC 233, winter 2013 3

4 Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter 2013 4

5 Exercise Identify the class definition, body of methods, attributes, local variables. CPSC 233, winter 2013 5 1. public class Inventory 2. { 3. private String productName; 4. private int productID; 5. public int stockLevel; 6. public void setProductInfo (String name, int id, int level) 7. { 8. productName = name; 9. productID = id; 10. stockLevel = level; 11. } 12. public void getProductInfo () 13. { 14. String info = ""; 15. info += productName + "," + productID + "," + stockLevel; 16. return info; 17. } 18. }

6 Exercise Identify the scope, in terms of line numbers, of each attribute and local variable CPSC 233, winter 2013 6 1. public class Inventory 2. { 3. private String productName; 4. private int productID; 5. public int stockLevel; 6. public void setProductInfo (String name, int id, int level) 7. { 8. productName = name; 9. productID = id; 10. stockLevel = level; 11. } 12. public void getProductInfo () 13. { 14. String info = ""; 15. info += productName + "," + productID + "," + stockLevel; 16. return info; 17. } 18. }

7 Exercise Given the Inventory class, identify the invalid statements in the following Driver class. CPSC 233, winter 2013 7 1. public class Driver 2. { 3. static void main (String[] args) 4. { 5. Inventory product; 6. product.setProductInfo("bread", 3456, 100); 7. product = new Inventory(); 8. product.setProductInfo ("milk", 1234, 50); 9. System.out.println(product.productName); 10. product.productID = 3425; 11. product.stockLevel --; 12. product = null; 13. System.out.println(product.getProductInfo()); 14. } 15. }

8 Exercise Given the Driver class, implement the missing methods in the Inventory class. CPSC 233, winter 2013 8 1. public class Driver 2. { 3. static void main (String[] args) 4. { 5. Inventory product = new Inventory(); 6. product.setProductName ("bread"); 7. product.setProductID (3456); 8. product.increaseStock (); // increase stock level by 1 9. System.out.println(product.getProductInfo()); 10. product.stockLevel --; 11. System.out.println(product.getProductInfo()); 12. } 13. }

9 Exercise Given the Inventory class, complete the main() method in the Driver class to do the following. Instantiates two objects of the Inventory class Update the product info for the 1 st object to “coke” with product ID as 1239 and stock level 40 Update the produce info for the 2 nd object to “pepsi” with produce ID as 1237 and stock level 39 Increase the stock level for both product using the new method from the previous slide Print the information of both products CPSC 233, winter 2013 9 public class Driver { static void main (String[] args) { }

10 Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter 2013 10


Download ppt "1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013."

Similar presentations


Ads by Google