Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 302 Week 8 Jim Williams, PhD.

Similar presentations


Presentation on theme: "CS 302 Week 8 Jim Williams, PhD."— Presentation transcript:

1 CS 302 Week 8 Jim Williams, PhD

2 This Week Lab: Debugging with Debugger Lecture: Static memory area
Object-Oriented Programming ArrayList Wrapper Classes Thursday: Gary Dahl

3 What is the value of num? num: 10 Stuff.num: 6 Stuff.num: 10
class Stuff { final static int MAX_VALUE = 10; //allowed P2 static int num = 6; //NOT allowed in P2 static void change( int n) { num = n + 1; } public static void main( String [] args) { int num = MAX_VALUE; change( num); System.out.println("num:" + num); System.out.println("Stuff.num:" + Stuff.num); num: 10 Stuff.num: 6 Stuff.num: 10 Stuff.num: 11 error try it.

4 What are values of x and y?
static int x; static int y = 2; static int methodA( int y) { x = y + 1; return x + y; } public static void main(String []args) { int x = 5; y = methodA( x); System.out.println("x="+x+ " y="+y); x=5 y=11 x=3 y=2 x=5 y=2 x=3 y=11 try it.

5 Instance vs. Class (static) Methods
method definition has “static” modifier use name of class when calling Math.max( 10, 20); Instance (non-static) Methods method definition does Not have “static” modifier use instance of class when calling Random randGen = new Random(); randGen.nextInt( 5);

6 What is the answer? AP? String s1 = "An important programming tool.";
String s2 = s1.substring( 9, 10); String s4 = new String( "?"); if ( s1.contains( "gram")) { s4 = s1.substring( 2, 4).trim(); } char c3 = s1.charAt( s1.indexOf('g') -3); String answer = (s2 + c3 + s4).toUpperCase(); AP? api ANP IM API try it.

7 Object-Oriented Programming
ArrayLists - specific example of use Concepts - Big Picture Grouping related data and methods User Defined Data Type (class) Creating Instances from a Class Creating a Class

8 Arrays fixed number of same data type (contiguous in memory)
What would we like to do with arrays? Add elements, remove elements...

9 How many elements in an Array vs ArrayList?
static void methodA(int [] arr) { //how to find length of array? } static void methodB(ArrayList list) { //how to find length of list? arr.length list.size() list.length() arr.size correct: A (try it)

10 What happens when array grows automatically array index out of bounds error who knows? You try to add more elements than will fit into an array? B and technically C since B is an error. (try it)

11 What happens when ArrayList grows automatically ArrayList index out of bounds error who knows? You try to add more elements than will fit into an ArrayList? A (try it)

12 What happens when you have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically You want to insert an element in an array at a specific index? A is the best answer.

13 What happens when you have to make sure there is enough room and move all the elements down and then insert it. Insertion is handled automatically You want to insert an element in an ArrayList at a specific index? B is the best answer

14 Data members Instance data members (variables) should be ______ unless there is a very good reason. private public protected static A is the best answer

15 Instance methods Instance methods should be ______ . private public
depends static depends is the best answer. Instance methods that are private can only be called from within the class. private methods can be helpful and since they are not public code outside the class cannot call them and so they can potentially be changed (improved) over time (fewer dependencies). All calls to the private methods must be in the same class.

16 Can primitive values be stored in an ArrayList?
yes no If they are boxed int yes, char no C is the best answer. Wrapper classes are utilized to create instances in order to put primitive type values in data structures such as an ArrayList.

17 Will this work? Integer i = 10; yes no sometimes error
yes, it is an example of auto-boxing.


Download ppt "CS 302 Week 8 Jim Williams, PhD."

Similar presentations


Ads by Google