mysteries revealed terminology
play

Mysteries Revealed Terminology A class is a data type - PDF document

Methods A method (a.k.a. func2on, procedure, rou2ne) is a piece of code that performs a useful ac2on Defining Classes and Methods You defined a method


  1. Methods ¡ • A ¡ method ¡(a.k.a. ¡func2on, ¡procedure, ¡rou2ne) ¡is ¡ a ¡piece ¡of ¡code ¡that ¡performs ¡a ¡useful ¡ac2on ¡ Defining Classes and Methods – You ¡defined ¡a ¡method ¡called ‘ main’. ¡ (Savitch, Chapter 5) – When ¡you ¡run ¡a ¡Java ¡program, ¡it ¡always ¡begins ¡by ¡ running ¡the ¡main ¡method. ¡ TOPICS • A ¡ method ¡can ¡also ¡return ¡a ¡value ¡to ¡the ¡program ¡ that ¡called ¡them ¡ • Java methods – More ¡details ¡in ¡a ¡minute… ¡ • Java objects • Static keyword • Parameter passing • Constructors CS 160, Spring Semester 2013 2 Mysteries ¡Revealed ¡ Terminology ¡ • A ¡ class ¡is ¡a ¡data ¡type ¡ public class Temperature { – Combines ¡variables ¡with ¡methods ¡ public static void main(String[] args) { // your code here • An ¡ object ¡is ¡an ¡instance ¡of ¡a ¡class ¡ } – Must ¡be ¡explicitly ¡created ¡in ¡program ¡ } • Crea2ng ¡an ¡object ¡is ¡called ¡ instan,a,on ¡ – This ¡involves ¡use ¡of ¡ new ¡operator ¡ You also define a method called In our recitations and ‘ main ’ that takes an array of assignments, you define Strings as its arguments classes (e.g. P1, R1). CS 160, Spring Semester 2013 3 CS 160, Spring Semester 2013 4 1

  2. Data ¡inside ¡objects ¡and ¡classes ¡ Another ¡mystery: ¡sta2c ¡ • Methods ¡are ¡called ¡with ¡an ¡instan2ated ¡object ¡ • They ¡are ¡of ¡two ¡types ¡ of ¡the ¡type ¡class: ¡ – They ¡may ¡belong ¡to ¡the ¡class ¡(and ¡will ¡take ¡the ¡ – The ¡nota2on ¡is ¡ objectname.method() ¡ same ¡value ¡for ¡all ¡the ¡objects) ¡ – You ¡must ¡have ¡a ¡String ¡variable ¡called ¡word ¡to ¡call ¡word.length() ¡ – They ¡may ¡belong ¡to ¡the ¡object ¡(and ¡can ¡take ¡ – The ¡length() ¡method ¡can ¡access ¡data ¡in ¡the ¡instance ¡it ¡is ¡called ¡on ¡ different ¡values ¡for ¡each ¡object) ¡ – Such ¡methods ¡are ¡called ¡instance ¡methods ¡ • Excep2on: ¡sta2c ¡methods ¡can ¡be ¡called ¡with ¡ – Objects ¡of ¡the ¡former ¡type ¡must ¡be ¡marked ¡as ¡ only ¡the ¡class ¡name, ¡i.e. ¡no ¡instance: ¡ sta2c ¡to ¡allow ¡the ¡compiler ¡to ¡differen2ate ¡ – The ¡nota2on ¡is ¡ classname.method() ¡ – Not ¡all ¡methods ¡need ¡to ¡access ¡data ¡specific ¡to ¡objects ¡ – Sta2c ¡declares ¡that ¡a ¡method ¡will ¡not ¡access ¡instance ¡data ¡ – Sta2c ¡methods ¡may ¡access ¡class ¡data, ¡but ¡not ¡instance ¡data ¡ CS 160, Spring Semester 2013 5 CS 160, Spring Semester 2013 6 public ¡sta2c ¡void ¡main ¡ Sta2c ¡methods ¡ • Remember ¡that ¡magic ¡incanta2on ¡at ¡the ¡start ¡ • ‘ main ’ ¡is ¡an ¡example ¡of ¡a ¡sta2c ¡method ¡ of ¡your ¡program? ¡ • It ¡can ¡only ¡access ¡class ¡variables ¡(or ¡sta2c ¡ – main ¡is ¡the ¡name ¡of ¡your ¡method ¡ variables) ¡ • The ¡main ¡method ¡is ¡called ¡by ¡the ¡OS ¡at ¡program ¡startup. ¡ – void ¡says ¡that ¡the ¡main ¡func2on ¡does ¡not ¡return ¡a ¡value ¡ • Therefore ¡ ‘ main ’ ¡cannot ¡access ¡instance ¡ • What ¡would ¡the ¡OS ¡do ¡with ¡a ¡return ¡value? ¡ variables. ¡To ¡use ¡instance ¡variables, ¡we ¡will ¡ – sta5c ¡says ¡that ¡main ¡will ¡not ¡access ¡instance ¡variables ¡ have ¡main ¡create ¡an ¡instance ¡of ¡its ¡class… ¡ • Because ¡the ¡OS ¡needs ¡to ¡call ¡it ¡without ¡crea2ng ¡a ¡class ¡instance ¡ – public ¡is ¡des2ned ¡to ¡remain ¡a ¡mystery ¡just ¡a ¡bit ¡longer. ¡ ¡ • But ¡first, ¡let ’ s ¡see ¡some ¡sta2c ¡methods ¡ – First ¡we ¡will ¡see ¡sta2c ¡methods ¡that ¡don ’ t ¡share ¡data ¡ – Then ¡we ¡will ¡see ¡sta2c ¡methods ¡that ¡can ¡share ¡data ¡ CS 160, Spring Semester 2013 7 CS 160, Spring Semester 2013 8 2

  3. Simple ¡example ¡(main ¡method ¡ Simple ¡example ¡(snowService ¡ calling ¡snowService) ¡ method) ¡ import ¡java.u2l.Scanner; ¡ ¡ public ¡class ¡SnowRemoval ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡sta2c ¡int ¡snowService(String ¡home){ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡public ¡sta2c ¡void ¡main(String[] ¡args) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.prina("Clearing ¡driveway ¡of ¡%s...", ¡home); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("Enter ¡your ¡address:"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println( “ …done"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Scanner ¡keyboard ¡= ¡new ¡Scanner(System.in); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("Clearing ¡sidewalk"); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡address ¡= ¡keyboard.nextLine(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡delay ¡= ¡snowService(address); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if(delay==0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("My ¡driveway ¡is ¡safe ¡now ” ); ¡ } ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡// ¡assume ¡status ¡is ¡non-­‑nega2ve ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println( “ I ¡have ¡to ¡wait ¡for ¡ “ ¡+ ¡delay ¡+ ¡ “ ¡hours ” ); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ CS 160, Spring Semester 2013 9 CS 160, Spring Semester 2013 10 Communica2on ¡between ¡ Communica2on ¡between ¡ calling ¡and ¡called ¡methods ¡ calling ¡and ¡called ¡methods ¡ • Method ¡parameters: ¡ • Method ¡return ¡type ¡and ¡value: ¡ – Method ¡declares ¡a ¡parameter ¡ “ formal ¡parameter ” ¡to ¡ – Can ¡return ¡void ¡(i.e., ¡nothing) ¡ ¡ state ¡what ¡can ¡be ¡provided ¡by ¡the ¡calling ¡program. ¡ ¡ – Can ¡return ¡a ¡type ¡(e.g., ¡int, ¡char, ¡ ¡String, ¡etc) ¡ ¡ • If ¡a ¡type ¡is ¡returned, ¡there ¡must ¡be ¡a ¡return ¡statement ¡in ¡ the ¡method ¡body ¡ public ¡String ¡reverseCase ¡(String ¡s1) ¡ • There ¡must ¡be ¡a ¡return ¡for ¡each ¡reachable ¡part ¡of ¡the ¡code ¡ – Indicates ¡the ¡calling ¡program ¡must ¡specify ¡a ¡String ¡ – Return ¡type ¡must ¡match ¡in ¡calling ¡program ¡ public ¡int ¡returnRandom() ¡ ¡ – Indicates ¡the ¡calling ¡program ¡specifies ¡no ¡params ¡ CS 160, Spring Semester 2013 11 CS 160, Spring Semester 2013 12 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend