1 / 10

Tutorial 7 , Feb 6/7, 2013

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

keanu
Download Presentation

Tutorial 7 , Feb 6/7, 2013

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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

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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

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

More Related