1 / 13

Using Methods

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.

diella
Download Presentation

Using Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using Methods Advanced Object Concepts

  2. 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

  3. 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)

  4. 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

  5. “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

  6. Why use symbolic constants? • Values are more easily recognized • Changes are made within one program location • To avoid inadvertently changing the wrong value

  7. 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

  8. 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);

  9. 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.*;

  10. 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()

  11. 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();

  12. 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)

  13. 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);

More Related