1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013...
-
Upload
cory-sullivan -
Category
Documents
-
view
214 -
download
0
description
Transcript of 1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013...
CPSC 233, winter 2013 1
Introduction to Computer Science for
Majors II
CPSC 233, Winter 2013
Tutorial 7, Feb 6/7, 2013
2
Quiz 2Definitions
OO exercises
Quiz 2
CPSC 233, winter 2013
3
DefinitionsAccess 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 2Definitions
OO exercises
Quiz 2
CPSC 233, winter 2013
5
ExerciseIdentify the class definition, body of methods, attributes, local
variables.
CPSC 233, winter 2013
1. public class Inventory2. {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
ExerciseIdentify the scope, in terms of line numbers, of each attribute
and local variable
CPSC 233, winter 2013
1. public class Inventory2. {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
ExerciseGiven the Inventory class, identify the invalid statements in
the following Driver class.
CPSC 233, winter 2013
1. public class Driver2. {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
ExerciseGiven the Driver class, implement the missing methods in
the Inventory class.
CPSC 233, winter 2013
1. public class Driver2. {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 19. 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 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
CPSC 233, winter 2013
public class Driver{ static void main (String[] args) {
}}
10
Quiz 2Definitions
OO exercises
Quiz 2
CPSC 233, winter 2013