+ All Categories
Home > Documents > CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up,...

CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up,...

Date post: 25-Dec-2015
Category:
Upload: noah-mitchell
View: 215 times
Download: 0 times
Share this document with a friend
21
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
Transcript
Page 1: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

CS 121 – Intro to Programming:Java - Lecture 6Announcements

Fourth programming assignment now up, due in Friday.

OWL assignments due as indicated

MidTerm: Monday, 10/17, 615-715, Thompson 104Sample exam now available at course web site

Office hours - now posted; held in LGRT 220; Lab time in 213 as indicated

Page 2: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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.

Page 3: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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; } }

Page 4: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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

Methodname

Returntype

Visibility

qualifier

Method body

Return statement

Page 5: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

Common Errorspublic 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”; }

Page 6: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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?

Page 7: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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?

Page 8: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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; } }

Page 9: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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

Page 10: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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”

Page 11: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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”);

Page 12: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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); }

Page 13: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

(x,y)

(x,0)

SimplePt p

Projection of p onto x axis

Page 14: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

public SimplePt xProject(){ SimplePt q = new SimplePt(this.getX(), 0); return q; } }

Page 15: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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?

Page 16: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

3

a

change

3/4

x

Value of a copied to x. Copy works just in one direction!

Page 17: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

101010010110100101

myKid 101010010110100101

class: Infant

name: Ted

age : 3 -> 4

…..

memory address

Remember this: method increments age by 1

myKid.anotherMonth();

Page 18: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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);

Page 19: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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.

Page 20: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

101110

myFlight myFlight

landFlight

101110

Address of myFlight object information

Page 21: CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:

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.


Recommended