130 likes | 243 Views
Using Methods. Advanced Object Concepts. The this Reference. Each instantiation of a class has its own data fields One copy of a method exists for al instantiated objects The compiler accesses the correct object’s field based on a reference to the object The this reference. Methods.
E N D
Using Methods Advanced Object Concepts
The this Reference • Each instantiation of a class has its own data fields • One copy of a method exists for al instantiated objects • The compiler accesses the correct object’s field based on a reference to the object • The this reference
Methods • Classes contain methods • Most methods are instance methods • associated with instantiated objects (“this”) • public int getEmpNum() (no use of static) • aWorker.getEmpNum(); • Class (static) methods – • These methods do not have a “this” reference (because no object associated with them)
Class variables • Variables shared by every instantiation of a class • Create class variables using the static keyword • Example static private int COMPANY_ID = 12345; • private access allows variable to only be accessed by class where it is contained
“Constant Variables” ???? • Symbolic constants • Use upper case letter and the underscore • Use final keyword to prevent alteration • Symbolic constants must be initialized • The value of symbolic constants can’t be changed after they are declared static final double PI = 3.14159 static final int COMPANY_ID = 12345; static final double SALES_TAX = 0.075
Why use symbolic constants? • Values are more easily recognized • Changes are made within one program location • To avoid inadvertently changing the wrong value
Library of Classes • 500 classes available for use • don’t “reinvent the wheel” • package - a folder that provides a convenient grouping for classes • java.lang package is automatically imported into every Java program • fundamental classes • System, Character, Boolean, Byte, Short, Integer, Long, Float, Double
java.lang.Math • Constants and methods for common mathematical functions • All constants and methods are static (they are class variables and class methods) areaOfCircle = Math.PI * radius * radius; largerValue = Math.max(32, 75); posVal = Math.abs(-245);
Using Prewritten Imported Methods • Use entire path with the class name java.util.Date myAnniversary = new java.util.Date(); • Import the class import java.util.Date; /* as first line in program */ Date myAnniversary = new Date(); • Import the package of which the class is a part (most common) import java.util.*;
Date class • A Date object: year, month, and day (time is set to midnight) • The current moment is the number of milliseconds that have elapsed since midnight 1/1/1970. • Any year in Date is 1900 less than the actual year – e.g., 82 means 1982 and 105 means 2005. • Month – 0 (January) to 11 (December) • Methods: setMonth(), getMonth(), setDay(), getDay(), setYear(), getYear(), getTime()
Date Demo Date today = new Date(); Date birthday = new Date(82, 6, 14);// 7/14/1982 System.out.println(“Current month is “ + today.getMonth()); System.out.println(“Current year is “ + today.getYear()); xx.println(“My birthday day is “ + birthday.getDay()); today.setDay(today.getDay() + 180); today.getTime () – birthday.getTime();
Gregorian Calendars class GregorianCalendar calendar = new GregorianCalendar( ); // get the current time getTimeInMillis() – get time in milliseconds get(argu) – where argument: DAY_OF_YEAR (1 to 366) DAY_OF_MONTH (1 to 31) DAY_OF_WEEK (Sunday, …Saturday, 1 to 7) YEAR (the current year) MONTH (January,…, December, 0 – 11) HOUR (1 t0 12) HOUR_OF_DAY ( 0 – 23)
Gregorian Calendars Demo Import java.util.*; //don’t forget Int ayear = 1940, amonth = 0, aday = 31; GregorianCalendar aCalendar = new GregorianCalendar(ayear, amonth, aday); GregorianCalendar aCurrentDate = new GregorianCalendar( ); Int aCyear = aCurrentDate.get(aCurrentDate.YEAR); Int aCmonth = aCurrentDate.get(aCurrentDate.MONTH); Int yearOld = aCyear – aCalendar.get(aCalendar.Year);