{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

09 - CS133 Developing Programming Principles Lecture 9 All...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS133: Developing Programming Principles Lecture 9 All about static : static methods, static variables, main methods, constants, wrapper classes 2 CS133 Course Notes Lecture 9, Slide 2 Warning! Warning! Danger! You will use static methods sometimes: Math class main methods – Wrapper classes You will almost never write static methods You will almost never use or create static variables (except for constants) Don't overuse static!
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3 CS133 Course Notes Lecture 9, Slide 3 static methods Belong to the class as a whole, not specific instances (objects) of it. Can be accessed without instantiating an object of the class type. 4 CS133 Course Notes Lecture 9, Slide 4 Temperature conversions // C == Celsius, F == Fahrenheit, K == Kelvin public class Temperature { public static double fToC(double fahrenheitTemp) { return (fahrenheitTemp – 32.0)*5.0/9.0; } public static double cToK(double celsiusTemp) { return celsiusTemp + 273.15; } public static double fToK(double fahrenheitTemp) { double degCelsius = fToC(fahrenheitTemp); return cToK(degCelsius); } // Similarly, write cToF(), kToC(),and kToF() }
Image of page 2
5 CS133 Course Notes Lecture 9, Slide 5 Calling a static method • Syntax: ClassName . Method ( Arguments ); • Example: System.out.println(Temperature.fToC(-40.0)); Result: _________ System.out.println(Temperature.cToK(21.0)); Result: _________ System.out.println(Temperature.fToK(85.0)); Result: _________ 6 CS133 Course Notes Lecture 9, Slide 6 Invoking a non- static method in a static one static
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern