1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013
Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter
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
Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter
Exercise Identify the class definition, body of methods, attributes, local variables. CPSC 233, winter 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. }
Exercise Identify the scope, in terms of line numbers, of each attribute and local variable CPSC 233, winter 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. }
Exercise Given the Inventory class, identify the invalid statements in the following Driver class. CPSC 233, winter 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. }
Exercise Given the Driver class, implement the missing methods in the Inventory class. CPSC 233, winter 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. }
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 public class Driver { static void main (String[] args) { }
Quiz 2 Definitions OO exercises Quiz 2 CPSC 233, winter