Lecter Java: Program design - Chapter 7: Programming with methods and classes

This chapter explores Java mechanisms that support the manipulation and calculation of values through method invocations; the use of class and instance variables; and the implementation of constructors, accessors, mutators, and facilitators. The examination begins with the method invocation process. | Programming with methods and classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Methods Instance method Operates on a object (., and instance of the class) String s = new String("Help every cow reach its " + "potential!"); int n = (); Class method Service provided by a class and it is not associated with a particular object String t = (n); Instance method Class method Data fields Instance variable and instance constants Attribute of a particular object Usually a variable Point p = new Point(5, 5); int px = ; Class variable and constants Collective information that is not specific to individual objects of the class Usually a constant Color favoriteColor = ; double favoriteNumber = - ; Instance variable Class constants Task – Support conversion between English and metric values 1 gallon = liters 1 mile = kilometers d degrees Fahrenheit = (d – . | Programming with methods and classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Methods Instance method Operates on a object (., and instance of the class) String s = new String("Help every cow reach its " + "potential!"); int n = (); Class method Service provided by a class and it is not associated with a particular object String t = (n); Instance method Class method Data fields Instance variable and instance constants Attribute of a particular object Usually a variable Point p = new Point(5, 5); int px = ; Class variable and constants Collective information that is not specific to individual objects of the class Usually a constant Color favoriteColor = ; double favoriteNumber = - ; Instance variable Class constants Task – Support conversion between English and metric values 1 gallon = liters 1 mile = kilometers d degrees Fahrenheit = (d – 32)/ degrees Celsius 1 ounce (avdp) = grams 1 acre = square miles = hectares Conversion Implementation public class Conversion { // conversion equivalencies private static final double LITERS_PER_GALLON = ; private static final double KILOMETERS_PER_MILE = ; private static final double GRAMS_PER_OUNCE = ; private static final double HECTARES_PER_ACRE = ; Conversion implementation Conversion Implementation // temperature conversions methods public static double fahrenheitToCelsius(double f) { return (f - 32) / ; } public static double celsiusToFahrenheit(double c) { return * c + 32; } // length conversions methods public static double kilometersToMiles(double km) { return km / KILOMETERS_PER_MILE; } Conversion Implementation // mass conversions methods public static double litersToGallons(double liters) { return liters / LITERS_PER_GALLON; } public static double gallonsToLiters(double gallons) { return .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.