Programming with methods and classesStatic vs. non-staticMethodsVariablesstatic and non-static rulesSlide 6Slide 7Slide 8Static vs. non-static: memory diagramProgram demoHow well do you feel you understand static-ness?Hand PaintingsConversion.javaTask – Conversion.javaConversion ImplementationConversion implementationSlide 17Slide 18Slide 19Conversion useA Conversion useA preferred Conversion useProgram DemoHow well do you feel you understand Conversion.java?FractalsParameter passingJava parameter passingSlide 29Slide 30Slide 31Slide 32Slide 33Method invocationsValue parameter passing demonstrationSlide 36Slide 37ParameterDemo.java walkthroughSlide 39How well do you feel you understand parameter passing?Honda’s best commercialCastingSlide 43Casting, take 2Casting, take 3Casting, take 4How well do you feel you understand casting?OverloadingSlide 49LegalSlide 51What’s the output?How well do you feel you understand overloading?2004 IOCCC winnersScopeWhat’s wrong with this code?Slide 57Blocks and scope rulesSlide 59Legal but not recommendedSlide 61Slide 62How well do you feel you understand scoping?Triple.javaTask – Triple.javaSlide 67Triple.java implementationSlide 69Slide 70RecommendationTriple.java toString() implementationTriple.java clone() implementationTriple.java equals() implementationTriple.java equals()Slide 76Slide 77Using our Triple classSlide 79How well do you feel you understand Triple.java?SummarySummary of key pointsCubic TragedyA two legged dog….1Programming withmethods and classesChapter 7Fall 2006CS 101Aaron Bloomfield22Static vs. non-staticStatic vs. non-static3MethodsInstance (or member) methodOperates on a object (i.e., and instance of the class)String s = new String("Help every cow reach its "+ "potential!");int n = s.length(); Class (i.e. static) methodService provided by a class and it is not associated with a particular objectString t = String.valueOf(n);Instance methodClass method4VariablesInstance variable and instance constantsAttribute of a particular objectUsually a variablePoint p = new Point(5, 5);int px = p.x; Class variables and constantsCollective information that is not specific to individual objects of the classUsually a constantColor favoriteColor = Color.MAGENTA;double favoriteNumber = Math.PI - Math.E;Instance variableClass constants5static and non-static rulesMember/instance (i.e. non-static) fields and methods can ONLY be accessed by the object nameClass (i.e. static) fields and methods can be accessed by Either the class name or the object nameNon-static methods can refer to BOTH class (i.e. static) variables and member/instance (i.e. non-static) variablesClass (i.e. static) methods can ONLY access class (i.e. static) variables6Static vs. non-staticConsider the following code:public class Staticness {private int a = 0;private static int b = 0;public void increment() {a++;b++;}public String toString() {return "(a=" + a + ",b=" + b + ")";}}7Static vs. non-staticAnd the code to run it:public class StaticTest {public static void main (String[] args) {Staticness s = new Staticness();Staticness t = new Staticness();s.increment();t.increment();t.increment();System.out.println (s);System.out.println (t);}}8Static vs. non-staticExecution of the code…Output is:(a=1,b=3)(a=2,b=3)9Staticness s = new Staticness();Staticness t = new Staticness();s.increment();t.increment();t.increment();System.out.println (s);System.out.println (t);Staticness s = new Staticness();Staticness t = new Staticness();s.increment();t.increment();t.increment();System.out.println (s);System.out.println (t);Static vs. non-static: memory diagramStaticness- a = 0 tStaticness- a = 0 s0bStaticness- a = 1Staticness- a = 1Staticness- a = 21231010Program demoProgram demoStaticTest.javaStaticTest.java11 How well do you feel How well do you feel you understand static-ness?you understand static-ness?a)a)Very well! This stuff is so easy.Very well! This stuff is so easy.b)b)With a little review, I’ll be good.With a little review, I’ll be good.c)c)Not very well at all.Not very well at all.d)d)I’m so lost. What’s static again?I’m so lost. What’s static again?e)e)I’d rather not answer this question, I’d rather not answer this question, thanks.thanks.1212Hand PaintingsHand Paintings1313Conversion.javaConversion.java14Task – Conversion.javaSupport conversion between English and metric valuesd degrees Fahrenheit = (d – 32)/1.8 degrees Celsius1 mile = 1.609344 kilometers1 gallon = 3.785411784 liters1 ounce (avdp) = 28.349523125 grams 1 acre = 0.0015625 square miles = 0.40468564 hectares15Conversion Implementationpublic class Conversion {// conversion equivalenciesprivate static final doubleKILOMETERS_PER_MILE = 1.609344;private static final doubleLITERS_PER_GALLON = 3.785411784;private static final doubleGRAMS_PER_OUNCE = 28.349523125;private static final doubleHECTARES_PER_ACRE = 0.40468564;16Conversion implementationpublic static double fahrenheitToCelsius (double f) {return (f - 32) / 1.8;} }Modifier public indicates other classes can use the methodModifier static indicates the method is a class methodNo use of member/instance variables!!!17Conversion Implementation// temperature conversions methodspublic static double fahrenheitToCelsius(double f) {return (f - 32) / 1.8;}public static double celsiusToFahrenheit(double c) {return 1.8 * c + 32;}// length conversions methodspublic static double kilometersToMiles(double km) {return km / KILOMETERS_PER_MILE;}18Conversion Implementation// mass conversions methodspublic static double litersToGallons(double liters) {return liters / LITERS_PER_GALLON;}public static double gallonsToLiters(double gallons) {return gallons * LITERS_PER_GALLON;}public static double gramsToOunces(double grams) {return grams / GRAMS_PER_OUNCE;}public static double ouncesToGrams(double ounces) {return ounces * GRAMS_PER_OUNCE;}19Conversion Implementation// area conversions methodspublic static double hectaresToAcres(double hectares) {return hectares / HECTARES_PER_ACRE;}public static double acresToHectares(double acres) {return acres * HECTARES_PER_ACRE;}20Conversion useScanner stdin = new Scanner (System.in);System.out.print("Enter a length in kilometers: ");double kilometers = stdin.nextDouble();double miles = Conversion.kilometersToMiles(kilometers);System.out.print("Enter a mass in liters: ");double liters =
View Full Document