1 / 19

Programmer-defined classes, part 2

Programmer-defined classes, part 2. More on identifier scope & Parameter passing. Categories of data storage. As we’re aware, we store data in memory, either in variables or constants

linus-barry
Download Presentation

Programmer-defined classes, part 2

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. Programmer-defined classes, part 2 More on identifier scope & Parameter passing

  2. Categories of data storage • As we’re aware, we store data in memory, either in variables or constants • Each of these storage units must be declared; the location of the declaration determines the unit identifier’s scope: • Class-wide variables and constants are declared within the class but outside any method • Local variables and constants are declared within the body of a method • The scope of an identifier is defined by the boundaries of the block in which it is declared

  3. One more category • A local variable is one that is declared within the body of a method • Another kind of local variable can be declared in the method’s parameter list • A part of the method’s heading, the parameter list specifies what, how many, and in what order arguments must be passed in a message that calls the method

  4. General syntax for a method • The general syntax for a method definition consists of a heading and body, as follows: • The generic syntax for a method, as we’ve seen before, is: Modifier(s) returnType identifier (parameter(s)) { method body } • The parameter list looks like a list of variable declarations, separated by comma • The scope of a parameter is local to its method

  5. Mutator methods and parameters • Data that is stored in variables that belong to the class are automatically accessible to member methods, as we have already seen • Member methods that are used to change these data values are called mutators; it is this type of method that almost always requires parameters

  6. Setter methods for the BetterTherm class • Consider the example class we looked at in the last lecture, the BetterTherm class • This class featured a constructor that set a default value for the lone class variable, kelvinTemp, and getter methods that gave us access to this value in various forms • What is missing from the class are mutator methods that allow us to change the temperature value

  7. Setter methods for the BetterTherm class • The purpose of a “setter” method is to assign a new value to a class variable • Such a method usually specifies a parameter; the value passed to the parameter is then assigned to the class variable • The methods on the next slide illustrate this concept for the BetterTherm class

  8. Setter methods for the BetterTherm class public void setKtemp (int k) { kelvinTemp = Math.abs(k); } public void setCtemp (int c) { kelvinTemp = c + CFACTOR; } public void setFtemp (int f) { int cTemp = (int)((f - FCONVI) / FCONVD); kelvinTemp = cTemp + CFACTOR; }

  9. General pattern for a setter method • Most setter methods follow this pattern: public void setValue (dataType param) { classVariable = param; } Where: • “dataType” is the same type as the class variable to be set • “classVariable” is the name of the previously-declared class variable • As we have seen, there are variations on the pattern; the three setter methods for BetterTherm use expressions that incorporate the parameter, rather than just the parameter itself, to set the value of variable kelvinTemp

  10. Calling setter methods • A method that includes one or more parameters in its heading must be called with arguments that match its parameters • For example, consider the code below: BetterTherm bt = new BetterTherm(); bt.setCtemp(32); // sets kelvinTemp to 32° Celsius Random rg = new Random(); bt.setCtemp (Math.abs(rg.nextInt()) % 100); // sets kelvinTemp to a random value between 0° and 100° Celsius

  11. Parameter matching • Parameters specify the number, ordering, and data type(s) of the argument(s) that can be passed to a method. • When a parameter is one of the simple numeric types, we can pass as an argument to the parameter any expression that is assignment-compatible with the parameter’s specified data type.

  12. Parameter matching • Recall that expressions can include: • literal values, e.g. -4, 13.02 • initialized variables or constants • messages to value-returning methods (e.g. Integer.parseInt(), Math.sqrt(a)) • arithmetic operators & parentheses • explicit type casts • Assignment compatibility means that the value being passed is only valid if it can be assigned to a variable of the parameter’s type

  13. Examples • The following are valid examples of messages requesting BetterTherm’s setFtemp method: bt.setFtemp(rg.nextInt()); bt.setFtemp(bt.getCtemp() + 5); bt.setFtemp((int)Math.sqrt(rg.nextDouble()));

  14. Examples • The following are invalid examples of calls to BetterTherm’s setFtemp method – what is the matter with each? bt.setFtemp(); bt.setFtemp(“new temperature”); bt.setFtemp(Math.PI); bt.setFtemp(Math.sqrt(rg.nextDouble()));

  15. Parameter matching and identifiers • Parameters, are variables local to the method they are declared in. As we already know, all variables have two properties: a data type and an identifier. • Arguments can have identifiers too. If we pass a variable or constant as an argument, we are passing a named argument. • There is no relationship between the name of a parameter and the name of its corresponding argument

  16. Parameter matching and identifiers • Parameters and arguments can have different names (and usually do) • A parameter and its corresponding argument can have the same name, but: • The two variables (parameter and argument) belong to different methods • They do not have the same scope

  17. Yet another analogy • I had a grandfather named Joe; he had a son named Joe, who was my uncle • My mother had a son named Joe, who is my brother • The three Joes are not the same person; and we can easily distinguish between them, because they exist(ed) in different locations – hence, different scope

  18. Example • Consider the following code: public class SimpleClass { private int x; public void setVar (int y) { x = y; } int getVar () { return x; } } Consider variables x and y; what is the scope of each one? The code on the next slide contains a tester class for the class shown here. What happens when a new variable named x is declared there?

  19. import java.util.*; public class TestSimp { public static void main (String [] args) { SimpleClass simp = new SimpleClass(); int x; Scanner kb = new Scanner (System.in); System.out.print ("Enter a whole number: "); x = kb.nextInt(); simp.setVar(x); System.out.println ("Value of SimpleClass’s x = " + simp.getVar()); x += 10; System.out.println ("Value of TestSimp’s x = " + x); System.out.println ("Value of SimpleClass’s x = " + simp.getVar()); } }

More Related