DOC PREVIEW
UVA CS 101 - Methods and classes

This preview shows page 1-2-3-4-5-39-40-41-42-43-44-78-79-80-81-82 out of 82 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 82 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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-static3MethodsInstance (or member) methodOperates 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) methodService provided by a class and it is not associated with a particular objectString t = String.valueOf(n);Instance methodClass method4VariablesInstance variable and instance constantsAttribute of a particular objectUsually a variablePoint p = new Point(5, 5);int px = p.x; Class variables and constantsCollective information that is not specific to individual objects of the classUsually a constantColor favoriteColor = Color.MAGENTA;double favoriteNumber = Math.PI - Math.E;Instance variableClass constants5static and non-static rulesMember/instance (i.e. non-static) fields and methods can ONLY be accessed by the object nameClass (i.e. static) fields and methods can be accessed by Either the class name or the object nameNon-static methods can refer to BOTH class (i.e. static) variables and member/instance (i.e. non-static) variablesClass (i.e. static) methods can ONLY access class (i.e. static) variables6Static vs. non-staticConsider 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-staticAnd 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-staticExecution 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 demoStaticTest.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.javaSupport conversion between English and metric valuesd degrees Fahrenheit = (d – 32)/1.8 degrees Celsius1 mile = 1.609344 kilometers1 gallon = 3.785411784 liters1 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

UVA CS 101 - Methods and classes

Documents in this Course
Classes

Classes

53 pages

Recursion

Recursion

15 pages

Iteration

Iteration

88 pages

PLEDGED

PLEDGED

6 pages

Objects

Objects

33 pages

PLEDGED

PLEDGED

11 pages

CS 101

CS 101

42 pages

Classes

Classes

83 pages

Iteration

Iteration

92 pages

Classes

Classes

186 pages

Classes

Classes

208 pages

Hardware

Hardware

21 pages

Arrays

Arrays

70 pages

Load more
Download Methods and classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Methods and classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Methods and classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?