Download presentation
Presentation is loading. Please wait.
1
CS180 Chapter 4 2/1/08
2
Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters 1-4 –Study Session: Tuesday, February 5, 6-8 pm, LWSN B131 Project 1 handed back
3
Classes Ways to organize data –Use to create objects containing data Primitives Objects –Contain methods to perform certain operations Accessors Mutators
4
Terminology Class – a blueprint for an object Object – an instance of a class Method/function – means to perform operations with the class Instance variables – data held within a class
5
Methods Used to provide an interface to the class Helper methods – break up your code into understandable chunks Types of methods –Void –Returning a value
6
Methods public int factorial(int n) { int fact = 1; for (int k=n; k>1; k--) fact *= k; return fact; }
7
Methods public void stringCount(String str, char c) { for (int k=0; k<str.length(); k++) { if (str.charAt(k) == c) count++; } System.out.println(c + “ occurs “ + count + “ times in “ + str); }
8
Scope Can only use variables within the current scope –Loop/conditional scope: if a variable is declared within an if/else block or any type of loop, it is not accessible outside the block Includes any declarations in the for loop declaration –Method scope: local variables declared within a method are not accessible outside the method Includes argument list –Class scope: manageable with public/private modifiers When given the choice, Java will always choose the variable with closest scope –If you have a class variable x and a local variable x declared, x refers to the local variable, this.x refers to the class variable
9
Constructors (Next Week) To demonstrate some basics, we need to introduce non-trivial constructors Constructors are used to initialize objects to contain certain values Very similar to methods, but… –Are always non-static (next week…) –Have no return type public class Point { private int x, y, z; public Point(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } … }
10
this keyword this is a pointer to an object’s self Always used implicitly, but sometimes useful to be used explicitly In case of ambiguity resulting from using the same variable name twice, use this to refer to the class’s variable and not the local variable class A { private int a; public int add(int a) { return this.a + a; } A more clear version avoids ambiguity class A { private int a; public int add(int b) { return a + b; }
11
this keyword Use this keyword to call another constructor (introduced in Chapter 7) public class A { private int x; public A() { this(0); } public A(int x) { this.x = x; } … } public class A { private int x; public A() { x=0; } public A(int x) { this.x = x; } … }
12
Equality Comparison For primitives, compare for equality with == For objects, when to use ==, when to use equals() method? –All object variables are simply addresses to memory locations, addresses are simply integers –When using ==, you are comparing addresses True: if they are aliases False: if they are not aliases (even if they hold the same data) –Write your own equals method to compare actual data within the class
13
toString Write a toString to convert object into a reasonable String representation Java printing statements automatically call toString for you (rarely do you have to call it yourself)
14
Pass-By-Value public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.x; } public void copy(A a) { a = new A(x); } public void set(int x) { this.x = x; } public int get() { return x; } } A a1 = new A(); A a2 = new A(); a1.set(4); a2.copy(a1); a1.get()? Parameters in Java are passed by value (as opposed to pass by reference).
15
Quiz public class Quiz { private int x; public Quiz() { x = 0; } public void doStuff(Quiz q, int x) { q = new Quiz(); q.x = x; } public String toString() { return “x = “ + x; } public static void main(String[] args) { int x = 5; Quiz q = new Quiz(); q.doStuff(q, x); System.out.println(q); }
16
Review Complete the method below, which counts the number of vowels within a given String. Then, using this method, write a single expression which computes the number of consonants within the given String. public int numVowels(String str) { }
17
Review Complete the method below, which counts the number of vowels within a given String. Then, using this method, write a single expression which computes the number of consonants within the given String. public int numVowels(String str) { int count = 0; str = str.toLowerCase(); for (int k=0; k<str.length(); k++) { switch (str.charAt(k)) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’:count++; break; } return count; }
18
Review Complete the method below, which prints out all factors of a given number. public void printFactors(int x) { }
19
Review Complete the method below, which prints out all factors of a given number. public void printFactors(int x) { for (int k=1; k <= x/2; k++) { if (x % k == 0) System.out.println(k); } System.out.println(x); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.