Download presentation
Presentation is loading. Please wait.
Published byNoah Mitchell Modified over 9 years ago
1
CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm: Monday, 10/17, 615-715, Thompson 104 Sample exam now available at course web site Office hours - now posted; held in LGRT 220; Lab time in 213 as indicated
2
Principal theme today: methods Methods organize (sub)jobs at the statement level They’re the fundamental mechanism for combining elementary operations together to make reuseable, more complex operations You can build an entire “world” with methods: very complex chores rely on complex chores, which rely on elementary chores, which rely on primitives.. and so on.
3
public class SimpleCoins { static final int HEADS = 1; static final int TAILS = 0; public int flip(){ if (Math.random() < 0.5) return TAILS; else return HEADS; } public int multiFlip(int flips) { int total = 0; for(int j = 0; j < flips; j++) { total += flip(); } return total; } }
4
public int multiFlip(int flips) { int total = 0; for(int j = 0; j < flips; j++) { total += flip(); } return total; } Parameter list: type followed by formal parameter Header line Method name Return type Visibility qualifier Method body Return statement
5
Common Errors public int multiFlip(int flips) { int total = 0; for(int j = 0; j < flips; j++) { total += flip(); } System.out.println( total); } public int multiFlip(int flips) { int total = 0; for(int j = 0; j < flips; j++) { total += flip(); } return “total”; }
6
Another example: let’s imagine we’ve created a WordStudy class, and we want to write a method that checks to see if the first character in a word or phrase is duplicated later in the phrase. Ignore case in your analysis. Return type? Parameter? Story of the algorithm?
7
Return type: boolean! Parameter: String: phrase Story: “First, turn the phrase into all lower case. Then, if there isn’t a first char, return false. Otherwise grab that first char. Now walk through all the other chars, looking for that first guy” Note: how do you “walk through” things?
8
public boolean firstRepeat(String s){ if (s.length() == 0) return false; else{ String t = s.toLowerCase(); char ch = t.charAt(0); boolean found = false; for(int j = 1; j < t.length(); j++){ if (t.charAt(j) == ch) found = true; } return found; } }
9
Until now: methods are passed, then return primitives, or maybe Strings We need to study parameter passing more carefully, look at how parameter values can, cannot change. Let’s look first at methods that return objects
10
Infant kid = new Infant("Jill",1); Infant kidTwin = kid.makeTwin("Ivan"); public Infant makeTwin(String name){ int myAge = getAge(); Infant i = new Infant(name,myAge); return i; } int myAge = getAge(); // troubling! int myAge = this.getAge(); this = “calling object”
11
public class infant{.. public Infant makeTwin(St..n) myAge = this.age; Or myAge = this.getAge();.. p.s.v main(…){ myKid = new Infant(“jill”, 1); myTwin = myKid.makeTwin(“fred”);
12
class SimplePt{ private int x; private int y; public SimplePt(int xx, int yy){ x= xx; y = yy; } public int getX(){ return x;} public int getY(){ return y;} public double dist(SimplePt other){ double deltaX = (this.getX() - other.getX()); double deltaY = (this.getY() - other.getY()); return Math.sqrt(deltaX*deltaX + deltaY*deltaY); }
13
(x,y) (x,0) SimplePt p Projection of p onto x axis
14
public SimplePt xProject(){ SimplePt q = new SimplePt(this.getX(), 0); return q; } }
15
Parameter passing in Java Consider this method: public void change(int x){x = x + 1;} int a = 3; change(a); System.out.println(a); What’s the value of a?
16
3 a change 3/4 x Value of a copied to x. Copy works just in one direction!
17
101010010110100101 myKid 101010010110100101 class: Infant name: Ted age : 3 -> 4 ….. memory address Remember this: method increments age by 1 myKid.anotherMonth();
18
The key example: public class Flight{ String id; String start; String end; boolean arrived; public Flight(String i, String st, String e,boolean here) { id = i; start = st; end = e; arrived =here; } myFlight = new Flight("CE777","JFK","LAX",false);
19
airportLAX.landFlight(myFlight); public void landFlight(Flight f){ f.setArrived(true); } We want the landFlight method to change the attributes of myFlight - and we can do it, because myFlight is literally a reference to data, and we aren’t changing that reference (that address). We’re jumping to that address and changing information there.
20
101110 myFlight landFlight 101110 Address of myFlight object information
21
101111010110100101 myFlight 101111010110100101 class: Flight name: CE777 arrived: false ->true ….. memory address The landFlight parameter does not change - it’s the address of the Flight object information. So our calling principle is not violated. But the referenced object itself does change state: The plane has arrived.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.