100 likes | 272 Views
Introduction to Computer Science for Majors II CPSC 233, Winter 2013. Tutorial 7 , Feb 6/7, 2013. Quiz 2. Definitions OO exercises Quiz 2. Definitions. Access modifiers: public vs. private OO: declaration vs. instantiation OO: reference vs. object Variables : scope vs. lifetime
E N D
Introduction to Computer Science for Majors II CPSC 233, Winter 2013 Tutorial 7, Feb 6/7, 2013 CPSC 233, winter 2013
Quiz 2 • Definitions • OO exercises • Quiz 2 CPSC 233, winter 2013
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
Quiz 2 • Definitions • OO exercises • Quiz 2 CPSC 233, winter 2013
Exercise 1. public class Inventory 2. { 3. private String productName; 4. private intproductID; 5. public intstockLevel; 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. } • Identify the class definition, body of methods, attributes, local variables. CPSC 233, winter 2013
Exercise 1. public class Inventory 2. { 3. private String productName; 4. private intproductID; 5. public intstockLevel; 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. } • Identify the scope, in terms of line numbers, of each attribute and local variable CPSC 233, winter 2013
Exercise • Given the Inventoryclass,identify the invalid statements in the following Driver class. 1. public class Driver 2. { 3. static void main (String[] args) { Inventory product; 6. product.setProductInfo("bread", 3456, 100); 7. product = new Inventory(); 8. product.setProductInfo ("milk", 1234, 50); 9. System.out.println(product.productName); product.productID = 3425; product.stockLevel --; 12. product = null; 13. System.out.println(product.getProductInfo()); 14. } 15. } CPSC 233, winter 2013
Exercise • Given the Driverclass, implement the missing methods in the Inventoryclass. 1. public class Driver 2. { 3. static void main (String[] args) { 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. } CPSC 233, winter 2013
Exercise • Given the Inventory class, complete the main() method in the Driverclass to do the following. • Instantiates two objects of the Inventoryclass • Update the product info for the 1st object to “coke” with product ID as 1239 and stock level 40 • Update the produce info for the 2nd 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 public class Driver { static void main (String[] args) { } } CPSC 233, winter 2013
Quiz 2 • Definitions • OO exercises • Quiz 2 CPSC 233, winter 2013