CS 121 – Intro to Programming:Java - Lecture 2 Announcements Course home page: First Owl assigment is up, due Friday. Second assignment up soon. First programming assignment now up due in class, on paper, next Tuesday Recommended IDE - jGRASP It comes with the disk in your textbook (in windows format however). I’ll make public some additional notes shortly on how get jGRASP, where to put it, etc.
Design Goals of the Programming Enterprise Correctness Efficiency Implementation Goals Robustness - what happens when you type with your elbows Adaptability - what happens when people start actually using your beautiful program - and they want to make changes to it Reusability - How well can the pieces of what you’ve written be repackaged for future use
Java’s Object Model Classes - a class is a blueprint or template for an object an Object is an example or an instantiation of a class definition an instance variable or attribute is a characteristic of an Object a method is an activity that serves an object Example: A Student class is a template for a student Attributes - name, age, credits, year, etc Methods - getAge, creditsLeft, calcGPA, and so forth Writing a class definition prepares this template Different students mean different objects -- with different attribute values!
Hospital class attributes? methods? Bank Account class attributes? methods? Person class Baseball player class Mother class
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…)
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
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.
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;"); } }
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); } }
Primitive Data Types objects are Java’s main currency Too tedious for them to be the only currency - making numbers a kind of object is a pain, however. Primitive data types integers (4), floats(2), char, boolean. That these aren’t actually objects will turn out to be a pain, too. Statement like these are fairly common: int count = 0; boolean okSoFar = false; char averageGrade = ‘C’; // note the single quotes Details in section 2.4
Operator Precedence Pretty important, but not very interesting num = / 3; not the same as num = (2 + 8) / 3; *, / evaluated first, unless overridden by parentheses. Moral: use parentheses pretty much always… Make sure you read up on 2.4, 2.5, 2.6 for Thursday!
Strings A very important class. String greeting; greeting = new String(“ola”); greeting2 = new String(“howdy”); greeting = greeting2; System.out.println(greeting); Some caveats: 1)Strings are not primitives (unlike float, double, int, etc) 2)Strings are so important that there’s a shorthand for String creation: greeting = “ola”; // works fine 3) Cell model for variables is different: now a cell hold reference to location of (actual) object -- rather than the object itself
As we’ve seen, classes/objects are served by methods Certainly true for Strings String pupName = “spot”; int len = pupName.length(); char what = pupName.charAt(1); char what = pupName.charAt(0); String huh = pupName.concat(“less”); String bigHuh = pubName.toUpperCase();
A word on +: String noise = “moo”; int age = 20; age = age + age; noise = noise + noise; noise = 20+noise; noise = noise; noise = noise+20+30;
Let’s mess with jGRASP, and some changes to OldMacdonald -change “woof” to “bark” --add a dotted line divider before each verse