Download presentation
Presentation is loading. Please wait.
Published byAnnis Cameron Roberts Modified over 8 years ago
1
CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment due on friday, Read chapter 4 through 4.5 for today, on mid-term. Midterm review link up soon. No honors section tomorrow MidTerm: Monday, 10/18, 615-715, Thompson 102,106 Office hours - now posted; held in LGRT 220
2
The state of affairs: So far most of our attention has been focused on Java at the statement level: what do statements do, and how can we string them together to get a job done (e.g.calculate compound interest) Today we take a serious look at the object model in Java We’ve already done some of this informally..
3
A class definition in Java functions as a template for object. A definition, however, is not an object. It only tells how to make one, and what actions are available to service one, once it’s been made. Two main elements: attributes (describe state of an object) methods (describe possible behaviors) A good example - a radio
4
public class Radio { boolean on = false; boolean fm; // false means am! float station; public Radio(boolean onFM; float curStation){ fm = onFM; station = curStation; } public void turnOff() {on = false;} public void setStation(float newStation){station = newStation;} public boolean amNow(){return (!fm);} }
5
Radio radio1 = new Radio(true, 88.5); Radio radio2 = new Radio(false, 1410.0); radio1.turnOff();
6
œ’÷◊public class Coin{ œœß œœßœÌœprivate final int HEADS = 0; œœßœÌœprivate final int TAILS = 1; œœß œœßœÌœprivate int face; œœß œœßœfifl‡public Coin(){ œœßœœ®ππœflip(); œœßœœ©} œœß œœßœfifl‡public void flip(){ œœßœœ®ππœface = (int)(Math.random()*2); œœßœœ©} œœß œœßœfifl
7
‡public boolean isHeads(){ œœß¬πƒππœreturn(face == HEADS); œœßœœ©} œœß œœßœfifl‡public String toString(){ œœßœœ®πÌœString faceName; œœßœœ®π≥¥if (face == HEADS) œœßœœßœ6æππœfaceName = "heads"; œœßœœßœˆπelse faceName = "tails"; œœß¬πƒππœreturn faceName; œœßœœ©} œœ©}
8
œ’÷◊public class CoinTest1{ œœß œœßœfifl‡public static void main(String[] args){ œœßœœß œœßœœ®πÌœCoin c = new Coin(); œœßœœ®ππœc.flip(); œœßœœ®ππœSystem.out.println(c); œœßœœ®ππœc.flip(); œœßœœ®ππœSystem.out.println(c); œœßœœ©} œœ©} œ´œ ----jGRASP exec: java CoinTest1 œœßœheads œœßœtails œœßœœœ©œ ----jGRASP: operation complete.
9
œ’÷◊public class CoinTest2{ // toss two coins - how many trials before two heads show up?œœß œœßœfifl‡public static void main(String[] args){ œœßœœß œœßœœ®πÌœCoin c1 = new Coin(); œœßœœ®πÌœCoin c2 = new Coin(); œœßœœ®πÌœint count = 0; œœßœœ®ππ±while(!c1.isHeads() || !c2.isHeads()){ œœßœœßœœ7ππœcount++; œœßœœßœœ7ππœc1.flip(); œœßœœßœœ7ππœc2.flip(); œœßœœßœœ∞} œœßœœ®ππœSystem.out.println(count); œœßœœ©} œœ©} ----------> typical answer: 4
10
œ’÷◊public class CoinTest3{ // how many flips are needed before 6 heads in row are found?// h œœßœfifl‡public static void main(String [] args){ œœßœœ®πÌœint count = 0; œœßœœ®πÌœint target = 6; œœßœœ®πÌœint headsSoFar = 0; œœßœœ®πÌœCoin c = new Coin(); œœßœœ®ππ±while (headsSoFar < target){ œœßœœßœœ7ππœcount++; œœßœœßœœ7ππœc.flip(); œœßœœßœœ7π≥πif (c.isHeads()) headsSoFar++; œœßœœßœœ5œˆπelse headsSoFar = 0; œœßœœßœœ∞} œœßœœ®ππœSystem.out.println("flips to " + target + " is " + count); œœßœœ©} œœ©}
11
œœ œœ´œ ----jGRASP exec: java CoinTest3 œœßœflips needed to 6 is 81 œœßœ œœ©œ ----jGRASP: operation complete. œœœœ œœ´œ ----jGRASP exec: java CoinTest3 œœßœflips needed to 6 is 403 œœßœ œœ©œ ----jGRASP: operation complete. Some sample data
12
A new method for the Coin class // does “flips” number of coin tosses, returns # of heads public int headCount(int flips){ int ct = 0; for (int j = 0; j < flips; j++){ flip(); if (isHeads()) ct++; } return ct; }
13
Another method for the Coin class // returns true if the state of a coin is tails public boolean isTails(){ return (!isHeads()); }
14
Write a method called maybeSwap. It is passed two strings as parameters, and prints them out in the given order with a space in between - unless the second string is longer. If that happens, print them out in the reverse order: second string first.
15
public void maybeSwap(String r, String s){ if (r.length() < s.length()) System.out.println(s + “ “ + r); else System.out.println(r + “ “ + s); }
16
Big ideas Class syntax - the legal pattern of symbols Scope - who can see a variable Methods and return statements - subprograms Method parameters - adjusting method behavior Local data - data used in a local context Constructors - makes an object Method overloading - different signatures for methods Method decomposition - using several methods to get a job done.
17
Old MadDonald does Java Old Macdonald had a farm // first two lines make chorus œœßœei, ei, o; œœßœand on that farm he had a pig œœßœei ei O œœßœWith an oink oink here œœßœAnd a oink oink there œœßœHere a oink there a oink œœßœEverywhere a oink oink œœßœOld Macdonald had a farm œœßœei, ei, o; œœßœOld Macdonald had a farm œœßœei, ei, o; and on that farm he had a dog œœßœei ei O (etc…)
18
Overall structure Chorus Pig verse Chorus Dog verse Chorus Who are the players? Chorus object Verse object - it’s parameterized (pig, dog, etc) A coordinating “song” class
19
public class MacSong{ public static void main(String[] args){ MacChorus m = new MacChorus(); MacVerse p = new MacVerse("pig", "oink"); MacVerse d = new MacVerse("dog", "woof"); m.chorus(); p.verse(); m.chorus(); d.verse(); m.chorus(); } } Three objects made - a chorus object, and two verse objects. The verse objects differ in that their attributes hold different values. The verse method exploits this to give different verses.
20
public class MacChorus{ public void chorus(){ // a method that serves MacChorus System.out.println("Old Macdonald had a farm"); System.out.println("ei, ei, o;"); } }
21
public class MacVerse{ String name; String noise; // attributes public MacVerse(String animalName, String animalNoise){ name = animalName; noise = animalNoise; } public String getName(){return name;} public String getNoise(){return noise;}; public void verse(){ System.out.println("and on that farm he had a " + name); System.out.println("ei ei O"); System.out.println("With a " + noise + " " + noise + " here"); System.out.println("And a " + noise + " " + noise + "there"); System.out.println("Here a " + noise + " there a " + noise); System.out.println("Everywhere a " + noise + " " + noise); } }
22
public void verse(){ System.out.println("and on that farm he had a " + name); System.out.println("ei ei O"); System.out.println("With a " + noise + " " + noise + " here"); System.out.println("And a " + noise + " " + noise + "there"); System.out.println("Here a " + noise + " there a " + noise); System.out.println("Everywhere a " + noise + " " + noise); } A proposed alteration: System.out.println("With " + addArticle(noise) + " " + noise + " here");
23
public boolean isVowel(char c){ return((c == ‘a’) || (c == ‘e’) || (c == ‘i’) || (c == ‘o’) || (c == ‘u’) ); } public String addArticle(String s){ char c = s.charAt(0); if (isVowel(c) ) return (“an “ + s); else return (“a “ + s); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.